views:

22

answers:

1

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?

+1  A: 

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.

bjg
Thanks for the excellent answer bjg.Quick question: Do God, Monit and Nagios all run in JRuby environments?
rmk
So Monit and Nagios can monitor any kind of process including java processes like Jetty/Tomcat/Glassfish or whatever you're planning to deploy your JRuby app on. God (which is written in Ruby) does not run in JRuby as far as I know. However there's no reason why God and the monitored application need necessarily have to run with the same Rubies (unless you are constrained from doing that for other reasons).
bjg