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
|
2015-06-15 10:36:47 +08:00
|
|
|
def self.demons
|
|
|
|
@demons
|
|
|
|
end
|
|
|
|
|
2024-06-03 12:51:12 +08:00
|
|
|
def self.start(count = 1, verbose: false, logger: nil)
|
2013-11-02 06:57:50 +08:00
|
|
|
@demons ||= {}
|
2024-06-03 12:51:12 +08:00
|
|
|
count.times { |i| (@demons["#{prefix}_#{i}"] ||= new(i, verbose:, logger:)).start }
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.stop
|
|
|
|
return unless @demons
|
|
|
|
@demons.values.each { |demon| demon.stop }
|
|
|
|
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 { |demon| demon.ensure_running }
|
|
|
|
end
|
|
|
|
|
2020-04-16 19:13:13 +08:00
|
|
|
def self.kill(signal)
|
|
|
|
return unless @demons
|
|
|
|
@demons.values.each { |demon| demon.kill(signal) }
|
|
|
|
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
|
|
|
|
|
2024-06-03 12:51:12 +08:00
|
|
|
def initialize(index, rails_root: nil, parent_pid: nil, verbose: false, logger: nil)
|
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
|
2024-06-03 12:51:12 +08:00
|
|
|
@logger = logger || Logger.new(STDERR)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log(message, level: :info)
|
|
|
|
@logger.public_send(level, message)
|
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
|
|
|
|
|
2015-06-16 09:16:33 +08:00
|
|
|
def alive?(pid = nil)
|
|
|
|
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
|
2024-06-03 12:51:12 +08:00
|
|
|
|
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 do
|
|
|
|
timeout = @stop_timeout
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2015-06-15 10:36:47 +08:00
|
|
|
while alive? && timeout > 0
|
|
|
|
timeout -= (@stop_timeout / 10.0)
|
|
|
|
sleep(@stop_timeout / 10.0)
|
2023-01-09 20:10:19 +08:00
|
|
|
begin
|
2015-06-15 10:36:47 +08:00
|
|
|
Process.waitpid(@pid, Process::WNOHANG)
|
2014-04-17 13:57:17 +08:00
|
|
|
rescue StandardError
|
2023-01-09 20:10:19 +08:00
|
|
|
-1
|
|
|
|
end
|
|
|
|
end
|
2015-06-15 10:36:47 +08:00
|
|
|
|
2023-01-09 20:10:19 +08:00
|
|
|
begin
|
2015-06-15 10:36:47 +08:00
|
|
|
Process.waitpid(@pid, Process::WNOHANG)
|
|
|
|
rescue StandardError
|
|
|
|
-1
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2015-06-15 10:36:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
wait_for_stop.call
|
|
|
|
|
|
|
|
if alive?
|
2024-06-03 12:51:12 +08:00
|
|
|
log("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 =
|
2023-01-09 20:10:19 +08:00
|
|
|
begin
|
2014-04-17 13:57:17 +08:00
|
|
|
Process.waitpid(@pid, Process::WNOHANG)
|
|
|
|
rescue StandardError
|
|
|
|
-1
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2024-06-03 12:51:12 +08:00
|
|
|
|
2014-04-17 13:57:17 +08:00
|
|
|
if dead
|
2024-06-03 12:51:12 +08:00
|
|
|
log("Detected dead worker #{@pid}, restarting...")
|
2014-04-17 13:57:17 +08:00
|
|
|
@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
|
2024-06-03 12:51:12 +08:00
|
|
|
log("Attempting to kill pid #{existing}")
|
2013-11-02 06:57:50 +08:00
|
|
|
Process.kill("TERM", existing)
|
|
|
|
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
|
2020-12-16 17:43:39 +08:00
|
|
|
@pid =
|
|
|
|
fork do
|
|
|
|
Process.setproctitle("discourse #{self.class.prefix}")
|
|
|
|
monitor_parent
|
|
|
|
establish_app
|
|
|
|
after_fork
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
2020-12-16 17:43:39 +08:00
|
|
|
write_pid_file
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def already_running?
|
2022-01-06 01:45:08 +08:00
|
|
|
if File.exist? pid_file
|
2013-11-02 06:57:50 +08:00
|
|
|
pid = File.read(pid_file).to_i
|
2019-10-02 12:54:08 +08:00
|
|
|
return pid if Demon::Base.alive?(pid)
|
2013-11-02 06:57:50 +08:00
|
|
|
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 StandardError
|
|
|
|
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)
|
|
|
|
puts msg if @verbose
|
|
|
|
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")
|
2013-11-02 06:57:50 +08:00
|
|
|
File.open(pid_file, "w") { |f| f.write(@pid) }
|
|
|
|
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
|
2024-06-03 12:51:12 +08:00
|
|
|
log("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
|