-
Notifications
You must be signed in to change notification settings - Fork 17
launcher
Pierre-Alexandre Meyer edited this page Feb 22, 2011
·
1 revision
Example of launcher script:
#!/usr/bin/env ruby
begin
require 'galaxy/properties'
rescue LoadError
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
retry
end
ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
=begin
launcher command
Commands:
start
stop
restart
=end
require 'timeout'
PID_FILE = File.join(ROOT, "launcher.pid")
LOG_FILE = File.join(ROOT, "launcher.log")
def is_alive pid
begin
Process.kill(0, pid) == 1
rescue Errno::ESRCH
false
end
end
def get_pid
pid = nil
begin
File.open PID_FILE do |f|
pid = f.read.to_i
end
pid = nil unless is_alive pid
rescue Errno::ENOENT
end
pid
end
def save_pid pid
File.open PID_FILE, "w" do |f|
f.puts pid
end
end
def wait_for pid
loop do
break unless is_alive pid
sleep 0.1
end
end
def start
pid = get_pid
unless pid.nil?
puts "Already running as #{pid}"
return -1
end
pid = fork do
STDOUT.reopen LOG_FILE, "a"
STDERR.reopen STDOUT
child = fork do
Dir.chdir ROOT
exec "bin/run"
end
Process.detach child
trap "TERM" do
begin
Process.kill Signal.list["TERM"], child
Timeout.timeout(60 * 2) do
wait_for child
end
rescue TimeoutError
puts "Polite stop failed, sending wombats to eat process!"
Process.kill Signal.list["KILL"], child
end
end
wait_for child
File.delete PID_FILE
end
Process.detach pid
save_pid pid
puts "Started as #{pid}"
0
end
def stop
pid = get_pid
if pid.nil?
puts "Not running"
return 0
end
Process.kill Signal.list["TERM"], pid
loop do
break unless is_alive pid
end
0
end
def status
pid = get_pid
case pid
when nil
puts "Not running"
0
else
puts "Running as #{pid}"
1
end
end
def restart
stop
start
end
exit case ARGV[0]
when "start"
start
when "stop"
stop
when "restart"
restart
else
status
end