简体   繁体   中英

hubot response callback called twice

Background

My team and I have been working on an automation project using hubot and vmware pyvmomi. One of the core functions I have been trying to fix is the create vm function. As a team, we determined that we want hubot to ask the user a series of questions then send a packet off to the REST API we wrote. The branch of hubot script I'm working on is found here . The specific issue I'm working on is the create function is generating more responses as more vms are being created and then sending multiple packets. I've logged info about the problem in the issue tab of the repo. See the issue related to sending multiple packets.

Looking through the issues logged with hubot's source code, I've found that other folks are also curious if hubot can be programmed to have a dialog (issue 950). So I decided to refactor the create function to allow me to contribute the dialog code back once I'm finished.

The problem

After the re-write (see the branch labeled refactor-create-vm-function), I am running into a duplicate call problem. I've posted a screenshot of the output when hubot responds to a create vm message.

What I think is happening is that the function is being executed once when it's being passed and then again when it is actually responding. Not sure how to prevent the first call from happening, or if I even can prevent it. If it can't be prevented, what are suggestions about writing a hubot dialog? I'm open to both javascript and coffeescript suggestions, since hubot scripts can be either.

Pretty new to javascript (and coffeescript for that matter), so any help would be greatly appreciated!

The issue here isn't that the method is getting called repeatedly, but that it attempts to recurse without having a definition to itself. Adding a reference to the other function scope where you're making the recursion call should do the trick.

(eg _this.askQuestion(...) instead of askQuestion(...)

After a little more debugging code (thanks @heckj), we discovered that the issue was caused by ill-formatted regex being passed to .respond . Since it wasn't formatted correctly, it was always passing true and therefore immediately executing its callback. Hence why the function was being called before and after input.

I also learned that regex are first-class citizens in javascript so you don't need to have single quotes surrounding it.

With that said, here is the regex before and after:

Before

{'regex': '(memory\\s|mem\\s)(\\d{1,4})(.*)?'}

After

{'regex': /(memory\\s|mem\\s)(\\d{1,4})(.*)?/i}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM