lib/process/daemon/controller.rb in process-daemon-0.5.0 vs lib/process/daemon/controller.rb in process-daemon-0.5.1
- old
+ new
@@ -30,10 +30,13 @@
# `options[:output]` specifies where to write textual output describing what is going on.
def initialize(daemon, options = {})
@daemon = daemon
@output = options[:output] || $stdout
+
+ # How long to wait until sending SIGTERM and eventually SIGKILL to the daemon process group when asking it to stop:
+ @stop_timeout = options[:stop_timeout] || 10.0
end
# This function is called from the daemon executable. It processes ARGV and checks whether the user is asking for `start`, `stop`, `restart`, `status`.
def daemonize(argv = ARGV)
case argv.shift.to_sym
@@ -149,10 +152,13 @@
end
return daemon_state
end
+ # How long to wait between checking the daemon process when shutting down:
+ STOP_PERIOD = 0.1
+
# Stops the daemon process.
def stop
@output.puts Rainbow("Stopping daemon...").blue
# Check if the pid file exists...
@@ -173,18 +179,19 @@
end
# Interrupt the process group:
pgid = -Process.getpgid(pid)
Process.kill("INT", pgid)
- sleep 0.1
- sleep 1 if ProcessFile.running(@daemon)
+ (@stop_timeout / STOP_PERIOD).to_i.times do
+ sleep STOP_PERIOD if ProcessFile.running(@daemon)
+ end
# Kill/Term loop - if the @daemon didn't die easily, shoot
# it a few more times.
attempts = 5
while ProcessFile.running(@daemon) and attempts > 0
- sig = (attempts >= 2) ? "KILL" : "TERM"
+ sig = (attempts <= 2) ? "KILL" : "TERM"
@output.puts Rainbow("Sending #{sig} to process group #{pgid}...").red
Process.kill(sig, pgid)
attempts -= 1