This blog is part of our Rails 5 series.
Before Rails 5 we had ability to configure Active Job queue_adapter at an application level. If we want to use sidekiq as our backend queue adapter we would configure it as following.
1config.active_job.queue_adapter = :sidekiq
This queue_adapter would be applicable to all jobs.
Rails 5 provides ability to configure queue_adapter on a per job basis. It means queue_adapter for one job can be different to that of the other job.
Let's suppose we have two jobs in our brand new Rails 5 application. EmailJob is responsible for processing basic emails and NewsletterJob sends out news letters.
1 2class EmailJob < ActiveJob::Base 3 self.queue_adapter = :sidekiq 4end 5 6class NewletterJob < ActiveJob::Base 7end 8 9EmailJob.queue_adapter 10 => #<ActiveJob::QueueAdapters::SidekiqAdapter:0x007fb3d0b2e4a0> 11 12NewletterJob.queue_adapter 13 => #<ActiveJob::QueueAdapters::AsyncAdapter:0x007fb3d0c61b88> 14
We are now able to configure sidekiq queue adapter for EmailJob. In case of NewsletterJob we fallback to the global default adapter which in case of a new Rails 5 app is async.
Moreover, in Rails 5, when one job inherits other job, then queue adapter of the parent job gets persisted in the child job unless child job has configuration to change queue adapter.
Since news letters are email jobs we can make NewsLetterJob inherit from EmailJob.
Below is an example where EmailJob is using rescue while NewsLetterJob is using sidekiq.
1 2class EmailJob < ActiveJob::Base 3 self.queue_adapter = :resque 4end 5 6class NewsletterJob < EmailJob 7end 8 9EmailJob.queue_adapter 10 => #<ActiveJob::QueueAdapters::ResqueAdapter:0x007fe137ede2a0> 11 12NewsletterJob.queue_adapter 13 => #<ActiveJob::QueueAdapters::ResqueAdapter:0x007fe137ede2a0> 14 15class NewsletterJob < EmailJob 16 self.queue_adapter = :sidekiq 17end 18 19NewsletterJob.queue_adapter 20 => #<ActiveJob::QueueAdapters::SidekiqAdapter:0x007fb3d0b2e4a0> 21