README.md 4.1 KB
Newer Older
1
# Active Job – Make work happen later
2 3

Active Job is a framework for declaring jobs and making them run on a variety
4
of queuing backends. These jobs can be everything from regularly scheduled
5
clean-ups, to billing charges, to mailings. Anything that can be chopped up into
6 7
small units of work and run in parallel, really.

8
It also serves as the backend for Action Mailer's #deliver_later functionality
9
that makes it easy to turn any mailing into a job for running later. That's
D
Darío Hereñú 已提交
10
one of the most common jobs in a modern web application: sending emails outside
11 12 13 14 15 16 17 18 19
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.

20
You can read more about Active Job in the [Active Job Basics](https://edgeguides.rubyonrails.org/active_job_basics.html) guide.
21 22 23

## Usage

24
To learn how to use your preferred queuing backend see its adapter
25 26
documentation at
[ActiveJob::QueueAdapters](http://api.rubyonrails.org/classes/ActiveJob/QueueAdapters.html).
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

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
43
MyJob.perform_later record  # Enqueue a job to be performed as soon as the queuing system is free.
44 45 46
```

```ruby
C
Cristian Bica 已提交
47
MyJob.set(wait_until: Date.tomorrow.noon).perform_later(record)  # Enqueue a job to be performed tomorrow at noon.
48 49 50
```

```ruby
C
Cristian Bica 已提交
51
MyJob.set(wait: 1.week).perform_later(record) # Enqueue a job to be performed 1 week from now.
52 53 54 55 56 57 58
```

That's it!


## GlobalID support

A
Abdelkader Boudih 已提交
59
Active Job supports [GlobalID serialization](https://github.com/rails/globalid/) for parameters. This makes it possible
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
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
```

A
Abdelkader Boudih 已提交
82
This works with any class that mixes in GlobalID::Identification, which
83 84 85
by default has been mixed into Active Record classes.


86
## Supported queuing systems
87

88
Active Job has built-in adapters for multiple queuing backends (Sidekiq,
89 90
Resque, Delayed Job and others). To get an up-to-date list of the adapters
see the API Documentation for [ActiveJob::QueueAdapters](http://api.rubyonrails.org/classes/ActiveJob/QueueAdapters.html).
91

92 93 94 95 96 97
**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).

98 99 100 101
## Auxiliary gems

* [activejob-stats](https://github.com/seuros/activejob-stats)

C
Cristian Bica 已提交
102
## Download and installation
103

C
Cristian Bica 已提交
104
The latest version of Active Job can be installed with RubyGems:
105

C
Cristian Bica 已提交
106
```
107
  $ gem install activejob
C
Cristian Bica 已提交
108 109
```

F
Fatos Morina 已提交
110
Source code can be downloaded as part of the Rails project on GitHub:
C
Cristian Bica 已提交
111 112

* https://github.com/rails/rails/tree/master/activejob
113 114 115

## License

116
Active Job is released under the MIT license:
117

118
* https://opensource.org/licenses/MIT
C
Cristian Bica 已提交
119 120 121 122


## Support

D
Darío Hereñú 已提交
123
API documentation is at:
C
Cristian Bica 已提交
124 125 126

* http://api.rubyonrails.org

F
Fatos Morina 已提交
127
Bug reports for the Ruby on Rails project can be filed here:
C
Cristian Bica 已提交
128 129 130 131 132 133

* https://github.com/rails/rails/issues

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

* https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-core