简体   繁体   中英

How to run an asynchronous Ruby script with Hubot?

I want to make one of my Ruby script available to my teammates that are not developers (read "reluctant to setup and maintain a Ruby environment").

We're also using a Hubot within our team.

By now, I'm sure you've guessed my question: "how can i write a Hubot script (CoffeeScript, ie JS) that can call my Ruby script?"

ps: my script takes a while to complete, if you guys have an idea on how i could make my hubot give a quick feedback ("i heard you, i'm gonna run your script") and then notify my when the script is done ("your script completed successfully"), it would just be awesome.

Can you just use exec to run your script? Something like:

module.exports = (robot) ->
  robot.hear /run my command/i, (msg) ->
    exec "cd /path/to/ruby/script && ruby yourscript.rb"
    msg.send "i heard you, i'm gonna run your script."

Hopefully this gets you going on the right path. I'm not sure what kinds of hooks you would need to put in to have it wait until after the exec finishes successfully to notify if the script ran correctly but hopefully google can help with that :)

I am sure, you have probably figured it out by now, but since this question helped me, I can go off of Sean's answer and complete the puzzle.

module.exports = (robot) ->
    robot.respond /your regex/i, (msg) ->
    cp = require "child_process" 
    cp.exec "./path/from/root/to/ruby script", (error, stdout, stderr) ->  
        if error
            msg.send "Sorry I encounted this error \n" + stderr
        else
            msg.send "Done. The output: \n" + stdout

I hope this helps.

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