How can I get delayed_job to use a specific db connection?

I have Rails 3 applications that use different databases depending on the subdomain. I do this using "install_connection" in ApplicationController.

Now I am trying to use delayed_job attenuation to do some background processing, however it uses the database connection that it is currently active. It connects to the subdomain database.

I would like to force it to use a "shared" database. I did this for some models called "establish_connection" in the model as follows:

class Customer < ActiveRecord::Base establish_connection ActiveRecord::Base.configurations["#{Rails.env}"] ... end 

Any idea how I can do this?

+8
ruby-on-rails-3 delayed-job
source share
1 answer

Here is what you need to know. When you add a DelayedJob stone to your application, you create a migration for it to create a table in which tasks are stored, but you do not create a model. This is because DelayedJob already has a model included in the gem (i.e. Delayed::Job ). You need to fix this model a bit, as well as your own models. You can do this in the initializer.

You may already have an initializer for configuring DelayedJob if you can do it there if you don't need to create one. So, create your initializer (in config/initializers ), if you don’t have one, call it delayed_job_config.rb , add the following to it:

 Delayed::Job.class_eval do establish_connection ActiveRecord::Base.configurations["#{Rails.env}"] end 

We did the same thing with the DelayedJob model that you did with your own models. Now DelayedJob will use this connection to place tasks in the database.

+17
source share

All Articles