librelist archives

« back to archive

Using separate ActiveRecord connection for particular worker

Using separate ActiveRecord connection for particular worker

From:
Eric Kurzenberger
Date:
2014-07-11 @ 15:38
In my Rails app's database.yml, I have set up a separate database 
connection for Sidekiq, and am establishing the ActiveRecord connection 
using this code in my sidekiq.rb initializer:

    Sidekiq.configure_server do |config|
        if defined?(ActiveRecord::Base)
      spec = "#{Rails.env}_sidekiq"
      if Rails.application.config.database_configuration.key?(spec)
      ActiveRecord::Base.establish_connection(spec.to_sym)
      else
          ActiveRecord::Base.establish_connection
      end
end
    end

My question is, is it possible to only have specific workers use this 
separate connection, rather than having it be used by all workers?  In 
this particular case, I want to have my Elasticsearch indexing workers do 
their indexing jobs off a DB replica, while the other workers use the 
primary datastore.  Can Sidekiq workers use different database 
connections?

Thanks for any help!

Eric
________________________________
________________________________
CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is
for the sole use of the intended recipient(s) and may contain confidential
and privileged information or otherwise be protected by law. Any 
unauthorized review, use, disclosure or distribution is prohibited. If you
are not the intended recipient, please contact the sender by reply e-mail 
and destroy all copies of the original message.