I'm using RubyOnRails 6.1 with multiDB setup: primary and follower.
CustomDatabaseResolver
is configured to distribute load between databases.
class CustomDatabaseResolver < ActiveRecord::Middleware::DatabaseSelector::Resolver
def read_from_primary?
super || rand(2).zero?
end
end
I think this configuration creates a problem for pgHero gem because its dashboard is randomly crashing with the error:
ActiveRecord::ConnectionNotEstablished
No connection pool for 'PgHero::Connection::Database337600' found.
I cannot find a way this can be fixed. I have tried to force pgHero controller to use primary, but no luck.
# config/initializers/pghero.rb
Rails.application.reloader.to_prepare do
PgHero::HomeController.send :include, WriterDbRoleExecutor
end
# app/controllers/concerns/writer_db_role_executor.rb
module WriterDbRoleExecutor
extend ActiveSupport::Concern
included do
around_action { |_controller, action| ApplicationRecord.force_writer_db_role(&action) }
end
end
Btw, same approach did helped with another gem - blazer
Rails.application.reloader.to_prepare do
Blazer::BaseController.send :include, WriterDbRoleExecutor
end
This actually got fixed by prepending around action:
There are some controller callbacks in
PgHero::HomeController
that make db connections, and we should ensure our callback got fired first.