I run several daemons in config/initializers/ when I start my rails app, but I need a way to detect when the app is shutting down, and stop the daemons.
Are there any hooks / places where I can do this?
I run several daemons in config/initializers/ when I start my rails app, but I need a way to detect when the app is shutting down, and stop the daemons.
Are there any hooks / places where I can do this?
There's no formal way to unwind an application within the Rails framework as far as I know. You could consider installing one or more handlers for the basic Ruby at_exit language facility. However this is only going to deal with an ordered shutdown of the application.
A more general strategy would be to use a server monitoring framework which would catch the ordered and unexpected exit cases for your application. I doubt very much that at_exit
handlers would be called if your rails instance was hosted inside Passenger and the associated Apache or Nginx server crashed for some reason although I've not actually tested this. Similar observations would likely apply for any application container that would exit unexpectedly.
You could consider something like God or Monit or Nagios (depending on how sophisticated your requirements) for implementing a general strategy for handing application shutdown and doing the right thing for each set of circumstances. These frameworks can also monitor those daemons too.