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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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_exithandlers 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.