2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
module Demon; end
|
|
|
|
|
|
|
|
# intelligent fork based demonizer
|
2019-10-02 00:54:08 -04:00
|
|
|
class Demon::Base
|
2013-11-01 18:57:50 -04:00
|
|
|
|
2015-06-14 22:36:47 -04:00
|
|
|
def self.demons
|
|
|
|
@demons
|
|
|
|
end
|
|
|
|
|
2018-01-10 21:51:52 -05:00
|
|
|
def self.start(count = 1, verbose: false)
|
2013-11-01 18:57:50 -04:00
|
|
|
@demons ||= {}
|
|
|
|
count.times do |i|
|
2018-01-10 21:51:52 -05:00
|
|
|
(@demons["#{prefix}_#{i}"] ||= new(i, verbose: verbose)).start
|
2013-11-01 18:57:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stop
|
|
|
|
return unless @demons
|
|
|
|
@demons.values.each do |demon|
|
|
|
|
demon.stop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-22 23:13:18 -04:00
|
|
|
def self.restart
|
|
|
|
return unless @demons
|
|
|
|
@demons.values.each do |demon|
|
|
|
|
demon.stop
|
|
|
|
demon.start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-17 01:57:17 -04:00
|
|
|
def self.ensure_running
|
|
|
|
@demons.values.each do |demon|
|
|
|
|
demon.ensure_running
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-16 07:13:13 -04:00
|
|
|
def self.kill(signal)
|
|
|
|
return unless @demons
|
|
|
|
@demons.values.each do |demon|
|
|
|
|
demon.kill(signal)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-30 06:26:16 -04:00
|
|
|
attr_reader :pid, :parent_pid, :started, :index
|
2015-06-14 22:36:47 -04:00
|
|
|
attr_accessor :stop_timeout
|
|
|
|
|
2018-01-10 21:51:52 -05:00
|
|
|
def initialize(index, rails_root: nil, parent_pid: nil, verbose: false)
|
2013-11-01 18:57:50 -04:00
|
|
|
@index = index
|
|
|
|
@pid = nil
|
2018-01-10 21:51:52 -05:00
|
|
|
@parent_pid = parent_pid || Process.pid
|
2014-04-17 01:57:17 -04:00
|
|
|
@started = false
|
2015-06-14 22:36:47 -04:00
|
|
|
@stop_timeout = 10
|
2018-01-10 21:51:52 -05:00
|
|
|
@rails_root = rails_root || Rails.root
|
|
|
|
@verbose = verbose
|
2013-11-01 18:57:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pid_file
|
2018-01-10 21:51:52 -05:00
|
|
|
"#{@rails_root}/tmp/pids/#{self.class.prefix}_#{@index}.pid"
|
2013-11-01 18:57:50 -04:00
|
|
|
end
|
|
|
|
|
2015-06-15 21:16:33 -04:00
|
|
|
def alive?(pid = nil)
|
|
|
|
pid ||= @pid
|
|
|
|
if pid
|
2019-10-02 00:54:08 -04:00
|
|
|
Demon::Base.alive?(pid)
|
2015-06-14 22:36:47 -04:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-16 07:13:13 -04:00
|
|
|
def kill(signal)
|
|
|
|
Process.kill(signal, @pid)
|
|
|
|
end
|
|
|
|
|
2018-06-17 23:20:23 -04:00
|
|
|
def stop_signal
|
|
|
|
"HUP"
|
|
|
|
end
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
def stop
|
2014-04-17 01:57:17 -04:00
|
|
|
@started = false
|
2013-11-01 18:57:50 -04:00
|
|
|
if @pid
|
2018-06-17 23:20:23 -04:00
|
|
|
Process.kill(stop_signal, @pid)
|
2015-06-14 22:36:47 -04:00
|
|
|
|
|
|
|
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?
|
2018-06-17 23:20:23 -04:00
|
|
|
STDERR.puts "Process would not terminate cleanly, force quitting. pid: #{@pid} #{self.class}"
|
2015-06-14 22:36:47 -04:00
|
|
|
Process.kill("KILL", @pid)
|
|
|
|
end
|
|
|
|
|
|
|
|
wait_for_stop.call
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
@pid = nil
|
2014-04-22 23:13:18 -04:00
|
|
|
@started = false
|
2013-11-01 18:57:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-17 01:57:17 -04: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-01 18:57:50 -04:00
|
|
|
def start
|
2014-04-17 01:57:17 -04:00
|
|
|
return if @pid || @started
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
if existing = already_running?
|
|
|
|
# should not happen ... so kill violently
|
2014-04-17 01:57:17 -04:00
|
|
|
STDERR.puts "Attempting to kill pid #{existing}"
|
2013-11-01 18:57:50 -04:00
|
|
|
Process.kill("TERM", existing)
|
|
|
|
end
|
|
|
|
|
2014-04-17 01:57:17 -04:00
|
|
|
@started = true
|
|
|
|
run
|
|
|
|
end
|
2013-11-01 18:57:50 -04:00
|
|
|
|
2014-04-17 01:57:17 -04:00
|
|
|
def run
|
2020-12-16 04:43:39 -05:00
|
|
|
@pid = fork do
|
|
|
|
Process.setproctitle("discourse #{self.class.prefix}")
|
|
|
|
monitor_parent
|
|
|
|
establish_app
|
|
|
|
after_fork
|
2013-11-01 18:57:50 -04:00
|
|
|
end
|
2020-12-16 04:43:39 -05:00
|
|
|
write_pid_file
|
2013-11-01 18:57:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def already_running?
|
2022-01-05 12:45:08 -05:00
|
|
|
if File.exist? pid_file
|
2013-11-01 18:57:50 -04:00
|
|
|
pid = File.read(pid_file).to_i
|
2019-10-02 00:54:08 -04:00
|
|
|
if Demon::Base.alive?(pid)
|
2013-11-01 18:57:50 -04:00
|
|
|
return pid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2015-06-14 22:36:47 -04:00
|
|
|
def self.alive?(pid)
|
2015-06-15 21:16:33 -04:00
|
|
|
Process.kill(0, pid)
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
2015-06-14 22:36:47 -04:00
|
|
|
end
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
private
|
|
|
|
|
2018-01-10 21:51:52 -05:00
|
|
|
def verbose(msg)
|
|
|
|
if @verbose
|
|
|
|
puts msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
def write_pid_file
|
2018-01-10 21:51:52 -05:00
|
|
|
verbose("writing pid file #{pid_file} for #{@pid}")
|
|
|
|
FileUtils.mkdir_p(@rails_root + "tmp/pids")
|
2013-11-01 18:57:50 -04:00
|
|
|
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
|
2015-06-15 21:16:33 -04: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-01 18:57:50 -04:00
|
|
|
end
|
|
|
|
sleep 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-17 01:57:17 -04:00
|
|
|
def suppress_stdout
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def suppress_stderr
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
def establish_app
|
2018-01-10 21:51:52 -05:00
|
|
|
Discourse.after_fork if defined?(Discourse)
|
2013-11-01 18:57:50 -04:00
|
|
|
|
|
|
|
Signal.trap("HUP") do
|
|
|
|
begin
|
|
|
|
delete_pid_file
|
|
|
|
ensure
|
2015-03-27 00:44:52 -04:00
|
|
|
# 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
|
2014-04-17 01:57:17 -04:00
|
|
|
$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
|