2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
module Demon; end
|
|
|
|
|
|
|
|
# intelligent fork based demonizer
|
2019-10-02 12:54:08 +08:00
|
|
|
class Demon::Base
|
2013-11-02 06:57:50 +08:00
|
|
|
|
2015-06-15 10:36:47 +08:00
|
|
|
def self.demons
|
|
|
|
@demons
|
|
|
|
end
|
|
|
|
|
2018-01-11 10:51:52 +08:00
|
|
|
def self.start(count = 1, verbose: false)
|
2013-11-02 06:57:50 +08:00
|
|
|
@demons ||= {}
|
|
|
|
count.times do |i|
|
2018-01-11 10:51:52 +08:00
|
|
|
(@demons["#{prefix}_#{i}"] ||= new(i, verbose: verbose)).start
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stop
|
|
|
|
return unless @demons
|
|
|
|
@demons.values.each do |demon|
|
|
|
|
demon.stop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-23 11:13:18 +08:00
|
|
|
def self.restart
|
|
|
|
return unless @demons
|
|
|
|
@demons.values.each do |demon|
|
|
|
|
demon.stop
|
|
|
|
demon.start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-17 13:57:17 +08:00
|
|
|
def self.ensure_running
|
|
|
|
@demons.values.each do |demon|
|
|
|
|
demon.ensure_running
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-16 19:13:13 +08:00
|
|
|
def self.kill(signal)
|
|
|
|
return unless @demons
|
|
|
|
@demons.values.each do |demon|
|
|
|
|
demon.kill(signal)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-30 18:26:16 +08:00
|
|
|
attr_reader :pid, :parent_pid, :started, :index
|
2015-06-15 10:36:47 +08:00
|
|
|
attr_accessor :stop_timeout
|
|
|
|
|
2018-01-11 10:51:52 +08:00
|
|
|
def initialize(index, rails_root: nil, parent_pid: nil, verbose: false)
|
2013-11-02 06:57:50 +08:00
|
|
|
@index = index
|
|
|
|
@pid = nil
|
2018-01-11 10:51:52 +08:00
|
|
|
@parent_pid = parent_pid || Process.pid
|
2014-04-17 13:57:17 +08:00
|
|
|
@started = false
|
2015-06-15 10:36:47 +08:00
|
|
|
@stop_timeout = 10
|
2018-01-11 10:51:52 +08:00
|
|
|
@rails_root = rails_root || Rails.root
|
|
|
|
@verbose = verbose
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def pid_file
|
2018-01-11 10:51:52 +08:00
|
|
|
"#{@rails_root}/tmp/pids/#{self.class.prefix}_#{@index}.pid"
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def alive?(pid = nil)
|
2015-06-16 09:16:33 +08:00
|
|
|
pid ||= @pid
|
|
|
|
if pid
|
2019-10-02 12:54:08 +08:00
|
|
|
Demon::Base.alive?(pid)
|
2015-06-15 10:36:47 +08:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-16 19:13:13 +08:00
|
|
|
def kill(signal)
|
|
|
|
Process.kill(signal, @pid)
|
|
|
|
end
|
|
|
|
|
2018-06-18 11:20:23 +08:00
|
|
|
def stop_signal
|
|
|
|
"HUP"
|
|
|
|
end
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
def stop
|
2014-04-17 13:57:17 +08:00
|
|
|
@started = false
|
2013-11-02 06:57:50 +08:00
|
|
|
if @pid
|
2018-06-18 11:20:23 +08:00
|
|
|
Process.kill(stop_signal, @pid)
|
2015-06-15 10:36:47 +08:00
|
|
|
|
|
|
|
wait_for_stop = lambda {
|
|
|
|
timeout = @stop_timeout
|
|
|
|
|
|
|
|
while alive? && timeout > 0
|
2017-07-28 09:20:09 +08:00
|
|
|
timeout -= (@stop_timeout / 10.0)
|
|
|
|
sleep(@stop_timeout / 10.0)
|
2015-06-15 10:36:47 +08:00
|
|
|
Process.waitpid(@pid, Process::WNOHANG) rescue -1
|
|
|
|
end
|
|
|
|
|
|
|
|
Process.waitpid(@pid, Process::WNOHANG) rescue -1
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_for_stop.call
|
|
|
|
|
|
|
|
if alive?
|
2018-06-18 11:20:23 +08:00
|
|
|
STDERR.puts "Process would not terminate cleanly, force quitting. pid: #{@pid} #{self.class}"
|
2015-06-15 10:36:47 +08:00
|
|
|
Process.kill("KILL", @pid)
|
|
|
|
end
|
|
|
|
|
|
|
|
wait_for_stop.call
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
@pid = nil
|
2014-04-23 11:13:18 +08:00
|
|
|
@started = false
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-17 13:57:17 +08:00
|
|
|
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-02 06:57:50 +08:00
|
|
|
def start
|
2014-04-17 13:57:17 +08:00
|
|
|
return if @pid || @started
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
if existing = already_running?
|
|
|
|
# should not happen ... so kill violently
|
2014-04-17 13:57:17 +08:00
|
|
|
STDERR.puts "Attempting to kill pid #{existing}"
|
2017-07-28 09:20:09 +08:00
|
|
|
Process.kill("TERM", existing)
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
|
2014-04-17 13:57:17 +08:00
|
|
|
@started = true
|
|
|
|
run
|
|
|
|
end
|
2013-11-02 06:57:50 +08:00
|
|
|
|
2014-04-17 13:57:17 +08:00
|
|
|
def run
|
2013-11-02 06:57:50 +08: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
|
2019-10-02 12:54:08 +08:00
|
|
|
if Demon::Base.alive?(pid)
|
2013-11-02 06:57:50 +08:00
|
|
|
return pid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2015-06-15 10:36:47 +08:00
|
|
|
def self.alive?(pid)
|
2015-06-16 09:16:33 +08:00
|
|
|
Process.kill(0, pid)
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
2015-06-15 10:36:47 +08:00
|
|
|
end
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
private
|
|
|
|
|
2018-01-11 10:51:52 +08:00
|
|
|
def verbose(msg)
|
|
|
|
if @verbose
|
|
|
|
puts msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
def write_pid_file
|
2018-01-11 10:51:52 +08:00
|
|
|
verbose("writing pid file #{pid_file} for #{@pid}")
|
|
|
|
FileUtils.mkdir_p(@rails_root + "tmp/pids")
|
2017-07-28 09:20:09 +08:00
|
|
|
File.open(pid_file, 'w') do |f|
|
2013-11-02 06:57:50 +08:00
|
|
|
f.write(@pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_pid_file
|
|
|
|
File.delete(pid_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def monitor_parent
|
|
|
|
Thread.new do
|
|
|
|
while true
|
2015-06-16 09:16:33 +08:00
|
|
|
begin
|
|
|
|
unless alive?(@parent_pid)
|
|
|
|
Process.kill "TERM", Process.pid
|
|
|
|
sleep 10
|
|
|
|
Process.kill "KILL", Process.pid
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
STDERR.puts "URGENT monitoring thread had an exception #{e}"
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
sleep 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-17 13:57:17 +08:00
|
|
|
def suppress_stdout
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def suppress_stderr
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
def establish_app
|
2018-01-11 10:51:52 +08:00
|
|
|
Discourse.after_fork if defined?(Discourse)
|
2013-11-02 06:57:50 +08:00
|
|
|
|
|
|
|
Signal.trap("HUP") do
|
|
|
|
begin
|
|
|
|
delete_pid_file
|
|
|
|
ensure
|
2015-03-27 12:44:52 +08:00
|
|
|
# TERM is way cleaner than exit
|
|
|
|
Process.kill("TERM", Process.pid)
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# keep stuff simple for now
|
2014-04-17 13:57:17 +08:00
|
|
|
$stdout.reopen("/dev/null", "w") if suppress_stdout
|
|
|
|
$stderr.reopen("/dev/null", "w") if suppress_stderr
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def after_fork
|
|
|
|
end
|
|
|
|
end
|