Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hubot sends output to the wrong channel #326

Open
sinteur opened this issue Sep 17, 2019 · 3 comments
Open

Hubot sends output to the wrong channel #326

sinteur opened this issue Sep 17, 2019 · 3 comments

Comments

@sinteur
Copy link

sinteur commented Sep 17, 2019

Description:

Situation: a hubot script that runs for longer time may send output to the wrong channel

Steps to reproduce:

  1. create a hubot script like this:
    ` module.exports = (robot) => {
    robot.respond(/(some_long_process)/i, (msg) => {

    const { spawn } = require('child_process');
    const cmd = "some_long_process.sh";
    const args = msg.match['input'].split(" ");
    const child = spawn(cmd, args);

    child.stdout.on('data', function(data) {
    outputArr = data.toString().split("\n");
    for (var i = 0; i < outputArr.length; i++) {
    if (outputArr[i].length > 1)
    { msg.send(outputArr[i]);}
    }
    });`

  2. invoke that script in channel 1

  3. while it is running and sending output to channel 1, change to channel 2

  4. type something in channel 2 BEFORE the hubot script finishes running

  5. The output generated by hubot will not start appearing in channel 2.

if you omit step 4, the output will go where it belongs, in channel 1.

Expected behavior:

output should always go to channel 1

Server Setup Information:

  • Version of Rocket.Chat Server: 1.3.2
  • Operating System: debian
  • Deployment Method: docker
  • Number of Running Instances: 2
  • DB Replicaset Oplog: enabled
@geekgonecrazy geekgonecrazy transferred this issue from RocketChat/Rocket.Chat Sep 17, 2019
@geekgonecrazy
Copy link
Contributor

Conversation in #dev starts here: https://open.rocket.chat/channel/dev?msg=rdNWhBrb8AeD8F5QC

But basically cloning the message inside the respond is a workaround

@sinteur
Copy link
Author

sinteur commented Sep 17, 2019

This code would fix the problem:

module.exports = (robot) => {
  robot.respond(/(some_long_process)/i, (msg) => {

    const { spawn } = require('child_process');
    const cmd = "some_long_process.sh";
    const args = msg.match['input'].split(" ");
    const child = spawn(cmd, args);
    var thisMsg = clone(msg);
    
    child.stdout.on('data', function(data) {
      outputArr = data.toString().split("\n");
      for (var i = 0; i < outputArr.length; i++) {
    		if (outputArr[i].length > 1)
    			{ thisMsg.send(outputArr[i]);}
		}
    });

@sinteur
Copy link
Author

sinteur commented Sep 18, 2019

since msg has a TON of functions in it, this is better:

var thisMsg = (msg);
thisMsg.message = JSON.parse(JSON.stringify(msg.message));
thisMsg.envelope = JSON.parse(JSON.stringify(msg.envelope));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants