I wanted to write a script, which starts a rails app, and opens it in the browser.
I'm trying to implement this in a peculiar way though - I expect two things:
- the script must wait until the server is started, then open the browser within a short time
- when I stop the script (Ctrl+C), the server process must stop.
The script I've written actually works, but I was wondering if there is a simplerr/more standard/more correct way of writing it.
The script is written for linux, but it's not meant to be platform specific - or more precisely, anything except Windows.
PROJECT_DIR = '/path/to/app'
POLLING_TIME = 0.1
SERVER_STARTED_REGEX = /WEBrick::HTTPServer#start/
BROWSER_COMMAND = 'browser http://localhost:3000 &'
Dir.chdir( PROJECT_DIR )
thread = Thread.new do
IO.popen( 'script/rails server 2>&1' ) do | io |
Thread.current[ :last_server_line ] = io.gets while true
end
end
sleep POLLING_TIME while thread[ :last_server_line ] !~ SERVER_STARTED_REGEX
`#{ BROWSER_COMMAND }`
thread.join