How send stdout on realtime on ruby? #54

Closed
clevertechru opened this Issue Jan 15, 2014 · 4 comments

Comments

Projects
None yet
2 participants

This construction collect stdout and push all after end execute cmd, but I want push stdout on realtime, how?

require 'open3'
def run_cmd(cmd)
    Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
      pid = wait_thr.pid # pid of the started process.
      while stdout_line = stdout.gets
        push_msg stdout_line
      end
      while stderr_line = stderr.gets
        push_msg stderr_line
      end

      exit_status = wait_thr.value
      unless exit_status.success?
        push_msg "FAILED !!! #{cmd}"
      end
    end
end

private
def push_msg(msg, channel = 'my-channel', event = 'my-event')
    puts msg
    Pusher[channel].trigger(event, {
        message: "#{msg}"
    })
end
Contributor

mloughran commented Jan 15, 2014

This doesn't really sounds like a pusher-gem question, but I suspect you're finding that the pipe is buffered.

@mloughran mloughran closed this Jan 15, 2014

Travis-CI use Pusher for workers stdout. How? ))

Contributor

mloughran commented Jan 15, 2014

You could ask them...

I would construct the pipe in shell and use stdbuf to control the buffering:

stdbuf -oL rake test | ruby pusher_post.rb

pusher_post.rb would just do

STDIN.each { |line| ... }

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment