Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect stop with Ruby Daemons gem

Tags:

ruby

daemon

I'm using the ruby daemon gem. Wondering how I can add some extra steps to the stop action? Was hoping I could detect stop was called, and add some extra code to it. Anyone know how I can accomplish this?

like image 474
gregf Avatar asked Feb 12 '09 19:02

gregf


3 Answers

Looking at the daemon gem code, it doesn't look like it has an obvious extension point for this purpose. However, I wonder if (in the daemonized process) you could trap the KILL/TERM signal that daemons sends when a 'stop' occurs...?

trap("TERM") do
  # execute your extra code here
end

Alternatively you could install an at_exit hook :-

at_exit do
  # execute your extra code here
end
like image 70
James Mead Avatar answered Oct 13 '22 10:10

James Mead


Rapleaf had a pretty good article on their blog about an extension to the Daemons gem that might be what you're looking for.

like image 24
techpeace Avatar answered Oct 13 '22 11:10

techpeace


After reading Daemons docs I found that there is a :stop_proc option in the #run method

:stop_proc A proc that will be called when the daemonized process receives a request to stop (works only for :load and :proc mode)

So basically you can pass it as an option with the #run or #run_proc methods

Original answer's link to rubyforge

like image 45
brutuscat Avatar answered Oct 13 '22 10:10

brutuscat