2012-02-14 11:21:07 -05:00
|
|
|
require 'socket'
|
|
|
|
require 'sys/proctable'
|
|
|
|
|
2012-12-15 06:34:53 -05:00
|
|
|
LAUNCH_TIMEOUT = 5
|
|
|
|
SHUTDOWN_TIMEOUT = 5
|
|
|
|
|
2012-02-14 11:21:07 -05:00
|
|
|
class OSRMLauncher
|
|
|
|
def initialize &block
|
|
|
|
Dir.chdir TEST_FOLDER do
|
2012-12-15 06:34:53 -05:00
|
|
|
begin
|
|
|
|
begin
|
|
|
|
Timeout.timeout(LAUNCH_TIMEOUT) do
|
|
|
|
osrm_up
|
|
|
|
wait_for_connection
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
raise "*** Launching osrm-routed timed out."
|
|
|
|
end
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
begin
|
|
|
|
Timeout.timeout(SHUTDOWN_TIMEOUT) do
|
|
|
|
osrm_down
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
raise "*** Shutting down osrm-routed timed out."
|
|
|
|
end
|
|
|
|
end
|
2012-02-14 11:21:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-15 06:34:53 -05:00
|
|
|
def osrm_up?
|
|
|
|
if @pipe
|
|
|
|
begin
|
|
|
|
Process.getpgid @pipe.pid
|
|
|
|
true
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
false
|
2012-02-14 11:21:07 -05:00
|
|
|
end
|
2012-12-15 06:34:53 -05:00
|
|
|
else
|
|
|
|
false
|
2012-02-14 11:21:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-15 06:34:53 -05:00
|
|
|
def osrm_up
|
|
|
|
return if osrm_up?
|
|
|
|
#exec avoids popen running osrm-routed inside a shell
|
|
|
|
#if the cmd is run inside a shell, popen returns the pid for the shell, and if we try to kill it,
|
|
|
|
#the child process is orphaned, and we can't terminate it.
|
|
|
|
@pipe = IO.popen('exec ../osrm-routed 1>>osrm-routed.log 2>>osrm-routed.log')
|
2012-02-14 11:21:07 -05:00
|
|
|
end
|
|
|
|
|
2012-12-15 06:34:53 -05:00
|
|
|
def osrm_down
|
|
|
|
if @pipe
|
|
|
|
Process.kill 'TERM', @pipe.pid
|
|
|
|
wait_for_shutdown
|
|
|
|
@pipe = nil
|
|
|
|
end
|
2012-02-14 11:21:07 -05:00
|
|
|
end
|
|
|
|
|
2012-12-15 06:34:53 -05:00
|
|
|
def kill
|
|
|
|
if @pipe
|
|
|
|
Process.kill 'KILL', @pipe.pid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_connection
|
|
|
|
while true
|
2012-02-14 11:21:07 -05:00
|
|
|
begin
|
2012-12-15 06:34:53 -05:00
|
|
|
socket = TCPSocket.new('localhost', OSRM_PORT)
|
|
|
|
return
|
2012-02-14 11:21:07 -05:00
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-15 06:34:53 -05:00
|
|
|
def wait_for_shutdown
|
|
|
|
while osrm_up?
|
2012-02-14 11:21:07 -05:00
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
end
|