2019-03-09 16:47:01 -05:00
|
|
|
|
# Active Job – Make work happen later
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
|
|
|
|
Active Job is a framework for declaring jobs and making them run on a variety
|
2018-11-18 23:36:44 -05:00
|
|
|
|
of queuing backends. These jobs can be everything from regularly scheduled
|
2014-08-20 15:36:58 -04:00
|
|
|
|
clean-ups, to billing charges, to mailings. Anything that can be chopped up into
|
2014-08-12 05:17:19 -04:00
|
|
|
|
small units of work and run in parallel, really.
|
|
|
|
|
|
2014-11-10 08:56:07 -05:00
|
|
|
|
It also serves as the backend for Action Mailer's #deliver_later functionality
|
2014-08-12 05:17:19 -04:00
|
|
|
|
that makes it easy to turn any mailing into a job for running later. That's
|
2015-02-28 20:19:23 -05:00
|
|
|
|
one of the most common jobs in a modern web application: sending emails outside
|
2014-08-12 05:17:19 -04:00
|
|
|
|
of 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.
|
|
|
|
|
|
2019-03-09 16:47:01 -05:00
|
|
|
|
You can read more about Active Job in the [Active Job Basics](https://edgeguides.rubyonrails.org/active_job_basics.html) guide.
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
2018-11-18 23:36:44 -05:00
|
|
|
|
To learn how to use your preferred queuing backend see its adapter
|
2014-11-10 08:56:07 -05:00
|
|
|
|
documentation at
|
2019-03-08 06:36:16 -05:00
|
|
|
|
[ActiveJob::QueueAdapters](https://api.rubyonrails.org/classes/ActiveJob/QueueAdapters.html).
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
|
|
|
|
Declare a job like so:
|
|
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
|
class MyJob < ActiveJob::Base
|
|
|
|
|
queue_as :my_jobs
|
|
|
|
|
|
|
|
|
|
def perform(record)
|
|
|
|
|
record.do_work
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Enqueue a job like so:
|
|
|
|
|
|
|
|
|
|
```ruby
|
2018-11-18 23:36:44 -05:00
|
|
|
|
MyJob.perform_later record # Enqueue a job to be performed as soon as the queuing system is free.
|
2014-08-12 05:17:19 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```ruby
|
2014-08-25 10:34:50 -04:00
|
|
|
|
MyJob.set(wait_until: Date.tomorrow.noon).perform_later(record) # Enqueue a job to be performed tomorrow at noon.
|
2014-08-12 05:17:19 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```ruby
|
2014-08-25 10:34:50 -04:00
|
|
|
|
MyJob.set(wait: 1.week).perform_later(record) # Enqueue a job to be performed 1 week from now.
|
2014-08-12 05:17:19 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
That's it!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## GlobalID support
|
|
|
|
|
|
2014-08-16 21:06:30 -04:00
|
|
|
|
Active Job supports [GlobalID serialization](https://github.com/rails/globalid/) for parameters. This makes it possible
|
2014-08-12 05:17:19 -04:00
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
|
class TrashableCleanupJob
|
|
|
|
|
def perform(trashable, depth)
|
|
|
|
|
trashable.cleanup(depth)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
2014-08-16 21:06:30 -04:00
|
|
|
|
This works with any class that mixes in GlobalID::Identification, which
|
2014-08-12 05:17:19 -04:00
|
|
|
|
by default has been mixed into Active Record classes.
|
|
|
|
|
|
|
|
|
|
|
2018-11-18 23:36:44 -05:00
|
|
|
|
## Supported queuing systems
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
2018-11-18 23:36:44 -05:00
|
|
|
|
Active Job has built-in adapters for multiple queuing backends (Sidekiq,
|
2014-09-21 16:20:23 -04:00
|
|
|
|
Resque, Delayed Job and others). To get an up-to-date list of the adapters
|
2019-03-08 06:36:16 -05:00
|
|
|
|
see the API Documentation for [ActiveJob::QueueAdapters](https://api.rubyonrails.org/classes/ActiveJob/QueueAdapters.html).
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
2018-03-19 14:46:07 -04:00
|
|
|
|
**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](https://github.com/rails/rails/issues/23311#issuecomment-176275718),
|
|
|
|
|
[21406](https://github.com/rails/rails/pull/21406#issuecomment-138813484), and [#32285](https://github.com/rails/rails/pull/32285).
|
|
|
|
|
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
2014-08-12 08:43:43 -04:00
|
|
|
|
## Download and installation
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
2014-08-12 08:43:43 -04:00
|
|
|
|
The latest version of Active Job can be installed with RubyGems:
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
2014-08-12 08:43:43 -04:00
|
|
|
|
```
|
2015-12-06 13:16:26 -05:00
|
|
|
|
$ gem install activejob
|
2014-08-12 08:43:43 -04:00
|
|
|
|
```
|
|
|
|
|
|
2017-11-28 13:27:43 -05:00
|
|
|
|
Source code can be downloaded as part of the Rails project on GitHub:
|
2014-08-12 08:43:43 -04:00
|
|
|
|
|
2021-01-19 15:43:24 -05:00
|
|
|
|
* https://github.com/rails/rails/tree/main/activejob
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
2020-04-25 01:04:16 -04:00
|
|
|
|
|
2014-08-12 05:17:19 -04:00
|
|
|
|
## License
|
|
|
|
|
|
2014-11-10 08:56:07 -05:00
|
|
|
|
Active Job is released under the MIT license:
|
2014-08-12 05:17:19 -04:00
|
|
|
|
|
2017-08-21 19:46:02 -04:00
|
|
|
|
* https://opensource.org/licenses/MIT
|
2014-08-12 08:43:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Support
|
|
|
|
|
|
2015-02-28 20:19:23 -05:00
|
|
|
|
API documentation is at:
|
2014-08-12 08:43:43 -04:00
|
|
|
|
|
2019-03-08 06:36:16 -05:00
|
|
|
|
* https://api.rubyonrails.org
|
2014-08-12 08:43:43 -04:00
|
|
|
|
|
2017-11-28 13:27:43 -05:00
|
|
|
|
Bug reports for the Ruby on Rails project can be filed here:
|
2014-08-12 08:43:43 -04:00
|
|
|
|
|
|
|
|
|
* https://github.com/rails/rails/issues
|
|
|
|
|
|
|
|
|
|
Feature requests should be discussed on the rails-core mailing list here:
|
|
|
|
|
|
2020-03-26 02:21:37 -04:00
|
|
|
|
* https://discuss.rubyonrails.org/c/rubyonrails-core
|