1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/README.md

103 lines
2.8 KiB
Markdown
Raw Normal View History

2014-05-19 06:08:03 -04:00
# Active Job -- Make work happen later
2014-05-18 05:44:28 -04:00
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, billing charges, or mailings. Anything that can be chopped up into
small units of work and run in parallel, really.
2014-05-19 07:02:32 -04:00
It also serves as the backend for [ActionMailer's #deliver_later functionality](https://github.com/rails/activejob/issues/13)
2014-05-18 05:44:28 -04:00
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
of the request-response cycle, so the user doesn't have to wait on it.
2014-05-19 07:13:30 -04:00
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
2014-05-19 17:01:56 -04:00
about API differences between Delayed Job and Resque. Picking your queuing
2014-05-19 07:13:30 -04:00
backend becomes more of an operational concern, then. And you'll be able to
switch between them without having to rewrite your jobs.
2014-05-18 05:44:28 -04:00
## Usage
2014-05-19 12:58:19 -04:00
Set the queue adapter for Active Job:
``` ruby
ActiveJob::Base.queue_adapter = :inline # default queue adapter
# Adapters currently supported: :resque, :sidekiq, :sucker_punch, :delayed_job, :queue_classic, :sneakers
2014-05-19 12:58:19 -04:00
```
Declare a job like so:
```ruby
class MyJob < ActiveJob::Base
def perform(record)
record.do_work
end
end
```
Enqueue a job like so:
```ruby
MyJob.enqueue record
```
That's it!
2014-05-19 06:08:03 -04:00
## GlobalID support
2014-05-19 06:06:09 -04:00
2014-05-19 07:02:32 -04:00
Active Job supports [GlobalID serialization](https://github.com/rails/activemodel-globalid/) for parameters. This makes it possible
2014-05-19 06:06:09 -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
2014-05-19 07:23:35 -04:00
def perform(trashable_class, trashable_id, depth)
2014-05-19 06:06:09 -04:00
trashable = trashable_class.constantize.find(trashable_id)
trashable.cleanup(depth)
end
end
```
Now you can simply do:
```ruby
class TrashableCleanupJob
2014-05-19 07:23:35 -04:00
def perform(trashable, depth)
2014-05-19 06:06:09 -04:00
trashable.cleanup(depth)
end
end
```
This works with any class that mixes in ActiveModel::GlobalIdentification, which
by default has been mixed into Active Record classes.
2014-05-19 06:08:03 -04:00
## Supported queueing systems
2014-05-18 15:37:16 -04:00
We currently have adapters for:
* Resque 1.x
* Sidekiq
* Sucker Punch
2014-05-19 04:47:12 -04:00
* Delayed Job
2014-05-19 17:01:56 -04:00
* QueueClassic
* Sneakers
2014-05-18 15:37:16 -04:00
2014-05-19 06:08:03 -04:00
## Under development as a gem, targeted for Rails inclusion
2014-05-18 05:44:28 -04:00
Active Job is currently being developed in a separate repository until it's
ready to be merged in with Rails. The current plan is to have Active Job
be part of the Rails 4.2 release, but plans may change depending on when
this framework stabilizes and feels ready.
2014-05-19 06:08:03 -04:00
## License
2014-05-18 05:44:28 -04:00
Active Job is released under the MIT license:
* http://www.opensource.org/licenses/MIT