discourse/lib/demon/base.rb

211 lines
3.5 KiB
Ruby
Raw Normal View History

2013-11-01 18:57:50 -04:00
module Demon; end
# intelligent fork based demonizer
class Demon::Base
def self.demons
@demons
end
def self.start(count=1)
2013-11-01 18:57:50 -04:00
@demons ||= {}
count.times do |i|
(@demons["#{prefix}_#{i}"] ||= new(i)).start
end
end
def self.stop
return unless @demons
@demons.values.each do |demon|
demon.stop
end
end
def self.restart
return unless @demons
@demons.values.each do |demon|
demon.stop
demon.start
end
end
def self.ensure_running
@demons.values.each do |demon|
demon.ensure_running
end
end
attr_reader :pid, :parent_pid, :started, :index
attr_accessor :stop_timeout
2013-11-01 18:57:50 -04:00
def initialize(index)
@index = index
@pid = nil
@parent_pid = Process.pid
@started = false
@stop_timeout = 10
2013-11-01 18:57:50 -04:00
end
def pid_file
"#{Rails.root}/tmp/pids/#{self.class.prefix}_#{@index}.pid"
end
def alive?
if @pid
Demon::Base.alive?(@pid)
else
false
end
end
2013-11-01 18:57:50 -04:00
def stop
@started = false
2013-11-01 18:57:50 -04:00
if @pid
# TODO configurable stop signal
2013-11-01 18:57:50 -04:00
Process.kill("HUP",@pid)
wait_for_stop = lambda {
timeout = @stop_timeout
while alive? && timeout > 0
timeout -= (@stop_timeout/10.0)
sleep(@stop_timeout/10.0)
Process.waitpid(@pid, Process::WNOHANG) rescue -1
end
Process.waitpid(@pid, Process::WNOHANG) rescue -1
}
wait_for_stop.call
if alive?
STDERR.puts "Process would not terminate cleanly, force quitting. pid: #{@pid}"
Process.kill("KILL", @pid)
end
wait_for_stop.call
2013-11-01 18:57:50 -04:00
@pid = nil
@started = false
2013-11-01 18:57:50 -04:00
end
end
def ensure_running
return unless @started
if !@pid
@started = false
start
return
end
dead = Process.waitpid(@pid, Process::WNOHANG) rescue -1
if dead
STDERR.puts "Detected dead worker #{@pid}, restarting..."
@pid = nil
@started = false
start
end
end
2013-11-01 18:57:50 -04:00
def start
return if @pid || @started
2013-11-01 18:57:50 -04:00
if existing = already_running?
# should not happen ... so kill violently
STDERR.puts "Attempting to kill pid #{existing}"
2013-11-01 18:57:50 -04:00
Process.kill("TERM",existing)
end
@started = true
run
end
2013-11-01 18:57:50 -04:00
def run
2013-11-01 18:57:50 -04:00
if @pid = fork
write_pid_file
return
end
monitor_parent
establish_app
after_fork
end
def already_running?
if File.exists? pid_file
pid = File.read(pid_file).to_i
if Demon::Base.alive?(pid)
2013-11-01 18:57:50 -04:00
return pid
end
end
nil
end
def self.alive?(pid)
begin
Process.kill(0, pid)
true
rescue
false
end
end
2013-11-01 18:57:50 -04:00
private
def write_pid_file
FileUtils.mkdir_p(Rails.root + "tmp/pids")
File.open(pid_file,'w') do |f|
f.write(@pid)
end
end
def delete_pid_file
File.delete(pid_file)
end
def monitor_parent
Thread.new do
while true
unless alive?(@parent_pid)
Process.kill "TERM", Process.pid
sleep 10
Process.kill "KILL", Process.pid
2013-11-01 18:57:50 -04:00
end
sleep 1
end
end
end
def suppress_stdout
true
end
def suppress_stderr
true
end
2013-11-01 18:57:50 -04:00
def establish_app
Discourse.after_fork
2013-11-01 18:57:50 -04:00
Signal.trap("HUP") do
begin
delete_pid_file
ensure
# TERM is way cleaner than exit
Process.kill("TERM", Process.pid)
2013-11-01 18:57:50 -04:00
end
end
# keep stuff simple for now
$stdout.reopen("/dev/null", "w") if suppress_stdout
$stderr.reopen("/dev/null", "w") if suppress_stderr
2013-11-01 18:57:50 -04:00
end
def after_fork
end
end