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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/4c5bf870-25f6-4328-8675-81ea59cf4439%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to