Question

In the configuration file example for Puma, it says the following for the on_restart function:

Code to run before doing a restart. This code should close log files, database connections, etc.

Do I need to implement this for a Rails app, to close connections to the db and the logfile, or is that taken care of automatically? If not, how do I actually do all that?

Was it helpful?

Solution

No you don't, Rails takes care of reloading your code automatically. But this code reloading support is limited. For example changes to application.rb are not applied until you restart the app server.

But I would recommend Phusion Passenger over Puma. Phusion Passenger is a lot easier to setup, especially when you hit production. Phusion Passenger integrates into Apache and Nginx directly and provides advanced features like dynamic worker management. Phusion Passenger is very mature, stable and performant and used by the likes of New York Times, Symantec, AirBnB, etc.

OTHER TIPS

I've found that using Redis as my Rails.cache provider causes an error page upon the first request every time my Rails/Puma server is restarted. The error I got was:

Redis::InheritedError (Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.)

To get around this error, I didn't add anything to on_restart, but did have to add code to on_worker_boot ( I am running Puma with workers=4 ):

puma-config.rb

on_worker_boot do
    puts "Reconnecting Rails.cache"
    Rails.cache.reconnect
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top