zombocom / puma_worker_killer

Automatically restart Puma cluster workers based on max RAM available
747 stars 77 forks source link

is it proper to call ActiveRecord::Base.establish_connection when on_worker_boot #73

Closed wenbo closed 5 years ago

wenbo commented 5 years ago

as you can see, below is my condition. and puma will be rolling restart by the code PumaWorkerKiller.enable_rolling_restart.

I just wondering whether it is proper to call ActiveRecord::Base.establish_connection when on_worker_boot. I mean since on_worker_boot block will be executed every time rolling restart, I'm afraid that it may be run out of database's connection pool.

workers ENV.fetch("WEB_CONCURRENCY") { 2 }
before_fork do
  require 'puma_worker_killer'
  PumaWorkerKiller.enable_rolling_restart  # Default is every 6 hours

  # If you're preloading your application and using ActiveRecord, it's recommended that you close any connections to the database here to prevent connection leakage:
  ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
end

preload_app!

# If you're preloading your application and using ActiveRecord, it's recommended that you setup your connection pool here:
on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end
wenbo commented 5 years ago

@schneems could you please have a look at above question.

schneems commented 5 years ago

It’s not required. AR will auto reconnect if it detects that the connection no longer works due to a fork. This behavior exists in Rails for a long time.