I have the following code, where a WEBrick instance is forked, and I want to wait till the webrick is started up, before continuing with the rest of the code:
require 'webrick'
pid = fork do
server = WEBrick::HTTPServer.new({:Port => 3333, :BindAddress => "localhost"})
trap("INT") { server.shutdown }
sleep 10 # here is code that take some time to setup
server.start
end
# here I want to wait till the fork is complete or the WEBrick server is started and accepts connections
puts `curl localhost:3333 --max-time 1` # then I can talk to the webrick
Process.kill('INT', pid) # finally the webrick should be killed
So how can I wait till the fork is complete, or even better till the WEBrick is ready to accept connections? I found a piece of code where they deal with a IO.pipe
and a reader and a writer. But that doesn't wait for webrick to load.
Unfortunately I haven't found anything for this specific case. Hope someone can help.
rd.read(1); rd.close
withraise "Server not started" unless rd.wait(10)
so that I can specify a timeout and raise an error. Thanks for the answer! – Athiste