John Quicksall
John Quicksall

Reputation: 11

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!

Upvotes: 1

Views: 452

Answers (3)

Yuliana Poliakova
Yuliana Poliakova

Reputation: 31

I had a similar issue with Hubot while using WebStorm 2023.1.3. In my case, the problem was caused by WebStorm automatically generating a second .js file from my CoffeeScript code. This resulted in the robot.hear command being executed twice, leading to unexpected behavior.

To resolve this issue, I found that removing either the CoffeeScript or the generated JavaScript file helped. By having only one of these files present in the project, the duplicate execution of the robot.hear command was eliminated, and the problem was resolved.

Upvotes: 0

John Quicksall
John Quicksall

Reputation: 11

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}

Upvotes: 0

heckj
heckj

Reputation: 7367

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.

(e.g. _this.askQuestion(...) instead of askQuestion(...)

Upvotes: 0

Related Questions