Skip to content

Instantly share code, notes, and snippets.

@maxlapshin
Created September 1, 2009 11:50
Show Gist options
  • Save maxlapshin/179065 to your computer and use it in GitHub Desktop.
Save maxlapshin/179065 to your computer and use it in GitHub Desktop.
require 'daemons'
module Daemons
class Monitor
def stop
pid = @pid.pid
begin; Process.kill('TERM', pid); rescue ::Exception; end
sleep(0.5)
begin; Process.kill('KILL', pid); rescue ::Exception; end
# We try to remove the pid-files by ourselves, in case the application
# didn't clean it up.
begin; @pid.cleanup; rescue ::Exception; end
end
end
class Application
def stop
if options[:force] and not running?
self.zap
return
end
# Catch errors when trying to kill a process that doesn't
# exist. This happens when the process quits and hasn't been
# restarted by the monitor yet. By catching the error, we allow the
# pid file clean-up to occur.
pid = @pid.pid
begin
Process.kill('TERM', pid)
sleep(0.5)
Process.kill('KILL', pid) if running?
rescue Errno::ESRCH => e
puts "#{e} #{pid}"
puts "deleting pid-file."
end
# We try to remove the pid-files by ourselves, in case the application
# didn't clean it up.
begin; @pid.cleanup; rescue ::Exception; end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment