D
Daniel Berger
Hi all,
I've got a poor man's init script where I'm firing off a Ruby process
in the background if it's not already running.
If the process fails, no problemo. The process I called dies, I pull
the error from the stderr handle via readlines, and fire off an email
to myself. But if the process succeeds, IO#readlines hangs because
there's no data to be read, so it just hangs there waiting for it.
One solution I came up with was to wrap the IO#readlines in a timeout,
but that feels clunky. Is there a better way to do this?
require 'open3'
require 'timeout'
program = File.join(Dir.pwd, "miniserver.rb")
cmd = "ruby #{program} &"
Open3.popen3(cmd) do |stdin, stdout, stderr|
begin
# Better way?
Timeout.timeout(2){
error = stderr.readlines
}
rescue Timeout::Error
puts "Timeout"
break
end
puts error.join("\n") if error
end
puts "Done"
Where "miniserver.rb" is just a simple loop/print/sleep program. I
thought there was a way to peek ahead on an IO object to see if any
data is available on the handle before attempting to read it, but
perhaps I'm glossing over the appropriate method.
Suggestions?
Thanks,
Dan
I've got a poor man's init script where I'm firing off a Ruby process
in the background if it's not already running.
If the process fails, no problemo. The process I called dies, I pull
the error from the stderr handle via readlines, and fire off an email
to myself. But if the process succeeds, IO#readlines hangs because
there's no data to be read, so it just hangs there waiting for it.
One solution I came up with was to wrap the IO#readlines in a timeout,
but that feels clunky. Is there a better way to do this?
require 'open3'
require 'timeout'
program = File.join(Dir.pwd, "miniserver.rb")
cmd = "ruby #{program} &"
Open3.popen3(cmd) do |stdin, stdout, stderr|
begin
# Better way?
Timeout.timeout(2){
error = stderr.readlines
}
rescue Timeout::Error
puts "Timeout"
break
end
puts error.join("\n") if error
end
puts "Done"
Where "miniserver.rb" is just a simple loop/print/sleep program. I
thought there was a way to peek ahead on an IO object to see if any
data is available on the handle before attempting to read it, but
perhaps I'm glossing over the appropriate method.
Suggestions?
Thanks,
Dan