More at rubyonrails.org: More Ruby on Rails

Active Job Basics

This guide provides you with all you need to get started in creating, enqueueing and executing background jobs.

After reading this guide, you will know:

1 Introduction

Active Job is a framework for declaring jobs and making them run on a variety of queueing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really.

2 The Purpose of the Active Job

The main point is to ensure that all Rails apps will have a job infrastructure in place, even if it's in the form of an "immediate runner". We can then have framework features and other gems build on top of that, without having to worry about API differences between various job runners such as Delayed Job and Resque. Picking your queuing backend becomes more of an operational concern, then. And you'll be able to switch between them without having to rewrite your jobs.

3 Creating a Job

This section will provide a step-by-step guide to creating a job and enqueuing it.

3.1 Create the Job

Active Job provides a Rails generator to create jobs. The following will create a job in app/jobs:

$ bin/rails generate job guests_cleanup
create  app/jobs/guests_cleanup_job.rb

You can also create a job that will run on a specific queue:

$ bin/rails generate job guests_cleanup --queue urgent
create  app/jobs/guests_cleanup_job.rb

As you can see, you can generate jobs just like you use other generators with Rails.

If you don't want to use a generator, you could create your own file inside of app/jobs, just make sure that it inherits from ActiveJob::Base.

Here's what a job looks like:

class GuestsCleanupJob < ActiveJob::Base
  queue_as :default

  def perform(*args)
    # Do something later

3.2 Enqueue the Job

Enqueue a job like so:

MyJob.perform_later record  # Enqueue a job to be performed as soon the queueing system is free.

MyJob.set(wait_until: Date.tomorrow.noon).perform_later(record)  # Enqueue a job to be performed tomorrow at noon.

MyJob.set(wait: 1.week).perform_later(record) # Enqueue a job to be performed 1 week from now.

That's it!

4 Job Execution

If no adapter is set, the job is immediately executed.

4.1 Backends

Active Job has adapters for the following queueing backends:

4.1.1 Backends Features
Async Queues Delayed Priorities Timeout Retries
Backburner Yes Yes Yes Yes Job Global
Delayed Job Yes Yes Yes Job Global Global
Que Yes Yes Yes Job No Job
Queue Classic Yes Yes No* No No No
Resque Yes Yes Yes (Gem) Queue Global Yes
Sidekiq Yes Yes Yes Queue No Job
Sneakers Yes Yes No Queue Queue No
Sucker Punch Yes Yes No No No No
Active Job Inline No Yes N/A N/A N/A N/A
Active Job Yes Yes Yes No No No

* Queue Classic does not support Job scheduling. However you can implement this yourself or you can use the queue_classic-later gem. See the documentation for ActiveJob::QueueAdapters::QueueClassicAdapter.

4.2 Change Backends

You can easily change your adapter:

# be sure to have the adapter gem in your Gemfile and follow the adapter specific
# installation and deployment instructions
YourApp::Application.config.active_job.queue_adapter = :sidekiq

5 Queues

Most of the adapters support multiple queues. With Active Job you can schedule the job to run on a specific queue:

class GuestsCleanupJob < ActiveJob::Base
  queue_as :low_priority

You can prefix the queue name for all your jobs using config.active_job.queue_name_prefix in application.rb:

# config/application.rb
module YourApp
  class Application < Rails::Application
    config.active_job.queue_name_prefix = Rails.env

# app/jobs/guests_cleanup.rb
class GuestsCleanupJob < ActiveJob::Base
  queue_as :low_priority

# Now your job will run on queue production_low_priority on your
# production environment and on beta_low_priority on your beta
# environment

If you want more control on what queue a job will be run you can pass a :queue option to #set:

MyJob.set(queue: :another_queue).perform_later(record)

To control the queue from the job level you can pass a block to queue_as. The block will be executed in the job context (so you can access self.arguments) and you must return the queue name:

class ProcessVideoJob < ActiveJob::Base
  queue_as do
    video = self.arguments.first
    if video.owner.premium?

  def perform(video)
    # do process video


Make sure your queueing backend "listens" on your queue name. For some backends you need to specify the queues to listen to.

6 Callbacks

Active Job provides hooks during the lifecycle of a job. Callbacks allow you to trigger logic during the lifecycle of a job.

6.1 Available callbacks

  • before_enqueue
  • around_enqueue
  • after_enqueue
  • before_perform
  • around_perform
  • after_perform

6.2 Usage

class GuestsCleanupJob < ActiveJob::Base
  queue_as :default

  before_enqueue do |job|
    # do something with the job instance

  around_perform do |job, block|
    # do something before perform
    # do something after perform

  def perform
    # Do something later

7 ActionMailer

One of the most common jobs in a modern web application is sending emails outside of the request-response cycle, so the user doesn't have to wait on it. Active Job is integrated with Action Mailer so you can easily send emails asynchronously:

# If you want to send the email now use #deliver_now

# If you want to send the email through Active Job use #deliver_later

8 GlobalID

Active Job supports GlobalID for parameters. This makes it possible to pass live Active Record objects to your job instead of class/id pairs, which you then have to manually deserialize. Before, jobs would look like this:

class TrashableCleanupJob
  def perform(trashable_class, trashable_id, depth)
    trashable = trashable_class.constantize.find(trashable_id)

Now you can simply do:

class TrashableCleanupJob
  def perform(trashable, depth)

This works with any class that mixes in ActiveModel::GlobalIdentification, which by default has been mixed into Active Model classes.

9 Exceptions

Active Job provides a way to catch exceptions raised during the execution of the job:

class GuestsCleanupJob < ActiveJob::Base
  queue_as :default

  rescue_from(ActiveRecord::RecordNotFound) do |exception|
   # do something with the exception

  def perform
    # Do something later


You're encouraged to help improve the quality of this guide.

Please contribute if you see any typos or factual errors. To get started, you can read our documentation contributions section.

You may also find incomplete content, or stuff that is not up to date. Please do add any missing documentation for master. Make sure to check Edge Guides first to verify if the issues are already fixed or not on the master branch. Check the Ruby on Rails Guides Guidelines for style and conventions.

If for whatever reason you spot something to fix but cannot patch it yourself, please open an issue.

And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome in the rubyonrails-docs mailing list.