Read more

How to use Active Job to decouple your background processing from a gem

Judith Roth
July 03, 2020Software engineer at makandra GmbH

In a web application you sometimes have tasks that can not be processed during a request but need to go to the background.
There are several gems that help to you do that, like Sidekiq Show archive.org snapshot or Resque Show archive.org snapshot .

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

With newer Rails you can also use ActiveJob Show archive.org snapshot as interface for a background processing library. See here Show archive.org snapshot for a list of supported queueing adapters.
For development and test it also brings its own queue adapters ( async Show archive.org snapshot , inline Show archive.org snapshot and test Show archive.org snapshot ). Note: For production use they don't recommend using these.

Using ActiveJob brings the advantage that the underlying background processing library can very easily be switched if there is the need to.


Code that all worker classes need can go to a ApplicationJob class, similar to the use of ApplicationRecord in Active Record (this is not enforced):

app/jobs/application_job.rb:

class ApplicationJob < ActiveJob::Base
  # common config goes here  
end

Regular Workers would then look like this (e.g. app/jobs/my_api_fetcher_job.rb):

class MyApiFetcherJob < ApplicationJob
  queue_as :default

  def perform(api_token, options)
    MyApiClient.new(api_token).fetch(options)
  end
end

and could be called with MyApiFetcherJob.perform_later('super-secret', options).

To define which queue adapter is used, set the config in your application.rb (or e.g. production.rb, if you prefer to have different adapters for different environments):

config.active_job.queue_adapter = :sidekiq

To set specific adapters in tests, you can create a spec/support/active_job.rb with:

RSpec.configure do |config|
  config.before(:suite) do
    ActiveJob::Base.queue_adapter = :test
  end
end

and a features/support/active_job.rb with:

# Jobs should be worked off immediately in tests
Rails.application.config.active_job.queue_adapter = :inline
Posted by Judith Roth to makandra dev (2020-07-03 13:30)