Active Job is a framework for declaring jobs and making them run on a variety of queuing 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 Active Job
The main point is to ensure that all Rails apps will have a job infrastructure in place. 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.
Rails by default comes with an "immediate runner" queuing implementation. That means that each job that has been enqueued will run immediately.
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
app/jobs (with an attached test case under
$ bin/rails generate job guests_cleanup invoke test_unit create test/jobs/guests_cleanup_job_test.rb 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
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
Here's what a job looks like:
class GuestsCleanupJob < ActiveJob::Base queue_as :default def perform(*guests) # Do something later end end
Note that you can define
perform with as many arguments as you want.
3.2 Enqueue the Job
Enqueue a job like so:
# Enqueue a job to be performed as soon the queuing system is # free. GuestsCleanupJob.perform_later guest
# Enqueue a job to be performed tomorrow at noon. GuestsCleanupJob.set(wait_until: Date.tomorrow.noon).perform_later(guest)
# Enqueue a job to be performed 1 week from now. GuestsCleanupJob.set(wait: 1.week).perform_later(guest)
# `perform_now` and `perform_later` will call `perform` under the hood so # you can pass as many arguments as defined in the latter. GuestsCleanupJob.perform_later(guest1, guest2, filter: 'some_filter')
4 Job Execution
For enqueuing and executing jobs you need to set up a queuing backend, that is to say you need to decide for a 3rd-party queuing library that Rails should use. Rails itself does not provide a sophisticated queuing system and just executes the job immediately if no adapter is set.
Active Job has built-in adapters for multiple queuing backends (Sidekiq, Resque, Delayed Job and others). To get an up-to-date list of the adapters see the API Documentation for ActiveJob::QueueAdapters.
4.2 Setting the Backend
You can easily set your queuing backend:
# config/application.rb module YourApp class Application < Rails::Application # Be sure to have the adapter's gem in your Gemfile # and follow the adapter's specific installation # and deployment instructions. config.active_job.queue_adapter = :sidekiq end end
Since jobs run in parallel to your Rails application, most queuing libraries require that you start a library-specific queuing service (in addition to starting your Rails app) for the job processing to work. For information on how to do that refer to the documentation of your respective library.
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 #.... end
You can prefix the queue name for all your jobs using
# config/application.rb module YourApp class Application < Rails::Application config.active_job.queue_name_prefix = Rails.env end end # app/jobs/guests_cleanup.rb class GuestsCleanupJob < ActiveJob::Base queue_as :low_priority #.... end # Now your job will run on queue production_low_priority on your # production environment and on staging_low_priority # on your staging environment
The default queue name prefix delimiter is '_'. This can be changed by setting
# config/application.rb module YourApp class Application < Rails::Application config.active_job.queue_name_prefix = Rails.env config.active_job.queue_name_delimiter = '.' end end # app/jobs/guests_cleanup.rb class GuestsCleanupJob < ActiveJob::Base queue_as :low_priority #.... end # Now your job will run on queue production.low_priority on your # production environment and on staging.low_priority # on your staging environment
If you want more control on what queue a job will be run you can pass a
To control the queue from the job level you can pass a block to
block will be executed in the job context (so you can access
and you must return the queue name:
class ProcessVideoJob < ActiveJob::Base queue_as do video = self.arguments.first if video.owner.premium? :premium_videojobs else :videojobs end end def perform(video) # Do process video end end ProcessVideoJob.perform_later(Video.last)
Make sure your queuing backend "listens" on your queue name. For some backends you need to specify the queues to listen to.
Active Job provides hooks during the life cycle of a job. Callbacks allow you to trigger logic during the life cycle of a job.
6.1 Available callbacks
class GuestsCleanupJob < ActiveJob::Base queue_as :default before_enqueue do |job| # Do something with the job instance end around_perform do |job, block| # Do something before perform block.call # Do something after perform end def perform # Do something later end end
7 Action Mailer
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 UserMailer.welcome(@user).deliver_now # If you want to send the email through Active Job use #deliver_later UserMailer.welcome(@user).deliver_later
Each job uses the
I18n.locale set when the job was created. Useful if you send
I18n.locale = :eo UserMailer.welcome(@user).deliver_later # Email will be localized to Esparanto.
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 < ActiveJob::Base def perform(trashable_class, trashable_id, depth) trashable = trashable_class.constantize.find(trashable_id) trashable.cleanup(depth) end end
Now you can simply do:
class TrashableCleanupJob < ActiveJob::Base def perform(trashable, depth) trashable.cleanup(depth) end end
This works with any class that mixes in
by default has been mixed into Active Record classes.
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 end def perform # Do something later end end
GlobalID allows serializing full Active Record objects passed to
If a passed record is deleted after the job is enqueued but before the
method is called Active Job will raise an
11 Job Testing
You can find detailed instructions on how to test your jobs in the testing guide.
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.