README.md 3.2 KB
Newer Older
D
David Heinemeier Hansson 已提交
1
# Active Job -- Make work happen later
D
David Heinemeier Hansson 已提交
2 3 4 5 6 7

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.

D
David Heinemeier Hansson 已提交
8
It also serves as the backend for [ActionMailer's #deliver_later functionality](https://github.com/rails/activejob/issues/13)
D
David Heinemeier Hansson 已提交
9 10 11 12
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.

D
David Heinemeier Hansson 已提交
13 14 15
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
A
Abdelkader Boudih 已提交
16
about API differences between Delayed Job and Resque. Picking your queuing 
D
David Heinemeier Hansson 已提交
17 18 19
backend becomes more of an operational concern, then. And you'll be able to
switch between them without having to rewrite your jobs.

D
David Heinemeier Hansson 已提交
20

21 22
## Usage

23 24 25 26
Set the queue adapter for Active Job:

``` ruby
ActiveJob::Base.queue_adapter = :inline # default queue adapter
A
Abdelkader Boudih 已提交
27 28
# Adapters currently supported: :backburner, :delayed_job, :que, :queue_classic, 
#                               :resque, :sidekiq, :sneakers, :sucker_punch
29 30
```

31 32 33 34
Declare a job like so:

```ruby
class MyJob < ActiveJob::Base
35 36
  queue_as :my_jobs

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  def perform(record)
    record.do_work
  end
end
```

Enqueue a job like so:

```ruby
MyJob.enqueue record
```

That's it!


D
David Heinemeier Hansson 已提交
52
## GlobalID support
53

D
David Heinemeier Hansson 已提交
54
Active Job supports [GlobalID serialization](https://github.com/rails/activemodel-globalid/) for parameters. This makes it possible
55 56 57 58 59
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
D
Douwe Maan 已提交
60
  def perform(trashable_class, trashable_id, depth)
61 62 63 64 65 66 67 68 69 70
    trashable = trashable_class.constantize.find(trashable_id)
    trashable.cleanup(depth)
  end
end
```

Now you can simply do:

```ruby
class TrashableCleanupJob
D
Douwe Maan 已提交
71
  def perform(trashable, depth)
72 73 74 75 76 77 78 79 80
    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.


D
David Heinemeier Hansson 已提交
81
## Supported queueing systems
82 83 84

We currently have adapters for:

A
Abdelkader Boudih 已提交
85
* [Backburner](https://github.com/nesquena/backburner)
A
Abdelkader Boudih 已提交
86 87 88 89 90 91 92
* [Delayed Job](https://github.com/collectiveidea/delayed_job)
* [Que](https://github.com/chanks/que)
* [QueueClassic](https://github.com/ryandotsmith/queue_classic)
* [Resque 1.x](https://github.com/resque/resque)
* [Sidekiq](https://github.com/mperham/sidekiq)
* [Sneakers](https://github.com/jondot/sneakers)
* [Sucker Punch](https://github.com/brandonhilkert/sucker_punch)
93 94


D
David Heinemeier Hansson 已提交
95
## Under development as a gem, targeted for Rails inclusion
D
David Heinemeier Hansson 已提交
96 97 98 99 100 101 102

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.


D
David Heinemeier Hansson 已提交
103
## License
D
David Heinemeier Hansson 已提交
104 105 106 107

Active Job is released under the MIT license:

* http://www.opensource.org/licenses/MIT