rails--rails/activejob
Jonathan Rochkind dcce260edc Add job_id and exception message to ActiveJob retry/discard logging
In other log messages like perform/performed and ActiveJob error logging, the job ID and exception message were already included, eg:

    Error performing TestFailureJob (Job ID: d70ad13e-e58b-409c-a8cc-e0447fc792b5) from Resque(default) in 1446.56ms: RuntimeError (Error Message):

But log message related to retry/discard behavior from ActiveJob::Exceptions did not include the Job ID or exception message. We now include them, in a consistent format with other existing messages.

Job ID is especially useful because, while other ActiveJob-related log messages get the Job ID via tagged logging, retry-related log messages do not currently end up tagged with Job ID. And it's really useful to use Job ID to be able to collect all the log lines related to a particular job, put the error messages together with the retry and subsequent perform messages.

We also include job.executions in the enqueue_retry log message because it was available and useful, to know if this is the 1st retry or 2nd or whatever. Previously job.executions was included in retry_stopped logging, but not enqueue_retry.
2022-02-08 10:06:21 -05:00
..
bin
lib Add job_id and exception message to ActiveJob retry/discard logging 2022-02-08 10:06:21 -05:00
test Add job_id and exception message to ActiveJob retry/discard logging 2022-02-08 10:06:21 -05:00
CHANGELOG.md Merge PR #43066 2022-01-06 16:53:31 +00:00
MIT-LICENSE Bump license years to 2022 [ci-skip] 2022-01-01 15:22:15 +09:00
README.md Active job basic doc grammer mistakes fixed 2021-07-26 22:41:20 +05:30
Rakefile Use the combined jsbundling-rails gem instead of individual js bundler gems (#43172) 2021-09-06 16:44:44 +02:00
activejob.gemspec Fix gemspec 2021-11-15 21:06:21 +00:00

README.md

Active Job Make work happen later

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.

It also serves as the backend for Action Mailer's #deliver_later functionality that makes it easy to turn any mailing into a job for running later. That's one of the most common jobs in a modern web application: sending emails outside the request-response cycle, so the user doesn't have to wait on it.

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 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.

You can read more about Active Job in the Active Job Basics guide.

Usage

To learn how to use your preferred queuing backend see its adapter documentation at ActiveJob::QueueAdapters.

Declare a job like so:

class MyJob < ActiveJob::Base
  queue_as :my_jobs

  def perform(record)
    record.do_work
  end
end

Enqueue a job like so:

MyJob.perform_later record  # Enqueue a job to be performed as soon as the queuing 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!

GlobalID support

Active Job supports GlobalID serialization 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)
    trashable.cleanup(depth)
  end
end

Now you can simply do:

class TrashableCleanupJob
  def perform(trashable, depth)
    trashable.cleanup(depth)
  end
end

This works with any class that mixes in GlobalID::Identification, which by default has been mixed into Active Record classes.

Supported queuing systems

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.

Please note: We are not accepting pull requests for new adapters. We encourage library authors to provide an ActiveJob adapter as part of their gem, or as a stand-alone gem. For discussion about this see the following PRs: 23311, 21406, and #32285.

Download and installation

The latest version of Active Job can be installed with RubyGems:

  $ gem install activejob

Source code can be downloaded as part of the Rails project on GitHub:

License

Active Job is released under the MIT license:

Support

API documentation is at:

Bug reports for the Ruby on Rails project can be filed here:

Feature requests should be discussed on the rails-core mailing list here: