简体   繁体   中英

How to make Hubot on IRC print external script output as it is received instead of all at once?

I have a Hubot instance running on an IRC. Inside my scripts directory, I have several coffeescripts linking to external python scripts. My issue is that I have data that should print out before other data within the external scripts such as "Processing request. Please wait..." etc and Hubot waits for the entire script to completely finish executing and dumps the output the IRC at once.

How do I modify my coffeescripts to send over output from an external script as it is received?

coffeescript example:

# Commands:
#   Hubot jira-add-comment <ticket> "comment" - Add given comment to a JIRA ticket

{spawn} = require 'child_process'
module.exports = (robot) ->

addComment = (msg,ticket,comment) -> 
    output = spawn "/path/to/externalscript.py", [ticket,comment]
    output.stdout.on 'data', (data) ->
        msg.send data.toString()

robot.respond /jira-add-comment (\w+-\d+) (.+)$/i, (msg) ->
    addComment(msg,msg.match[1].trim(),msg.match[2])

Thanks!

I had this same problem. The solution was to pass the "-u" flag to Python when calling the Python script from Hubot.

s = spawn 'python', ['-u', '/your/script/here.py', 'any_other_flags']

https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately

Could it be because of bad identation in your example script?

output.stdout.on 'data', (data) ->
msg.send data.toString()

Should be:

output.stdout.on 'data', (data) ->
    msg.send data.toString()

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