Read from follower db in background job

Hi,

We have a Sidekiq background job that is really slow and db intensive, so we want to configure it to read from our Postgres follower database instead of the standard one.

To do this I wrote a small utility class that connects an ActiveRecord model to the follower db if it is not already connected.

class DbConnector

def self.connect_ar_class_to_follower_db(klass)

unless connected_to_follower?(klass)

klass.establish_connection("#{Rails.env}_follower".to_sym)

end

end

Assume connected to follower if not connected to default connection db

def self.connected_to_follower?(klass)

klass.connection_config[:database] != ActiveRecord::Base.connection_config[:database]

end

end

In the Sidekiq background job we use it like this:

DbConnector.connect_ar_class_to_follower_db(SomeARClass)

SomeARClass.where(:some_id => id).some_scope(…)

Is this a good solution or does anyone have other suggestions for how to solve this?

We tried the Octopus gem but ran into a bug (https://github.com/tchandy/octopus/issues/280) with it.

We also have another background job that we would like to switch over to use the follower database but for another ActiveRecord model, is there some way to share the already established connection for another ActiveRecord class?

Any other feedback on this approach?

Thank you,

Mikael Amborn