active_job_basics.md 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
Active Job Basics
=================

This guide provides you with all you need to get started in creating,
enqueueing and executing background jobs.

After reading this guide, you will know:

* How to create jobs.
* How to enqueue jobs.
* How to run jobs in the background.
* How to send emails from your application async.

--------------------------------------------------------------------------------

Introduction
------------

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.


The Purpose of the Active Job
-----------------------------
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.


Creating a Job
--------------

This section will provide a step-by-step guide to creating a job and enqueue it.

### Create the Job

C
Cristian Bica 已提交
42 43 44
Active Job provides a Rails generator to create jobs. The following will create a
job in app/jobs:

45 46 47 48 49
```bash
$ bin/rails generate job guests_cleanup
create  app/jobs/guests_cleanup_job.rb
```

C
Cristian Bica 已提交
50 51 52 53 54 55 56
You can also create a job that will run on a specific queue:

```bash
$ bin/rails generate job guests_cleanup --queue urgent
create  app/jobs/guests_cleanup_job.rb
```

57 58 59
As you can see, you can generate jobs just like you use other generators with
Rails.

C
Cristian Bica 已提交
60
If you don't want to use a generator, you could create your own file inside of
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
app/jobs, just make sure that it inherits from `ActiveJob::Base`.

Here's how a job looks like:

```ruby
class GuestsCleanupJob < ActiveJob::Base
  queue_as :default

  def perform
    # Do something later
  end
end
```

### Enqueue the Job

Enqueue a job like so:

```ruby
MyJob.enqueue record  # Enqueue a job to be performed as soon the queueing system is free.
```

```ruby
MyJob.enqueue_at Date.tomorrow.noon, record  # Enqueue a job to be performed tomorrow at noon.
```

```ruby
MyJob.enqueue_in 1.week, record # Enqueue a job to be performed 1 week from now.
```

That's it!


Job Execution
-------------

If not adapter is set, the job is immediately executed.

### Backends

Active Job has adapters for the following queueing backends:

* [Backburner](https://github.com/nesquena/backburner)
* [Delayed Job](https://github.com/collectiveidea/delayed_job)
* [Qu](https://github.com/bkeepers/qu)
* [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)

#### Backends Features

<table cellpadding="3" cellspacing="0">
  <tbody>
    <tr>
      <td>&nbsp;</td>
      <td><strong>Async</strong></td>
      <td><strong>Queues</strong></td>
      <td><strong>Delayed</strong></td>
      <td><strong>Priorities</strong></td>
      <td><strong>Timeout</strong></td>
      <td><strong>Retries</strong></td>
    </tr>
    <tr>
      <td><strong>Backburner</strong></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Job</td>
      <td>Global</td>
    </tr>
    <tr>
      <td><strong>Delayed Job</strong></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Job</td>
      <td>Global</td>
      <td>Global</td>
    </tr>
    <tr>
      <td><strong>Que</strong></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Job</td>
      <td>No</td>
      <td>Job</td>
    </tr>
    <tr>
      <td><strong>Queue Classic</strong></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Gem</td>
      <td>No</td>
      <td>No</td>
      <td>No</td>
    </tr>
    <tr>
      <td><strong>Resque</strong></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Gem</td>
      <td>Queue</td>
      <td>Global</td>
      <td>?</td>
    </tr>
    <tr>
      <td><strong>Sidekiq</strong></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Queue</td>
      <td>No</td>
      <td>Job</td>
    </tr>
    <tr>
      <td><strong>Sneakers</strong></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>No</td>
      <td>Queue</td>
      <td>Queue</td>
      <td>No</td>
    </tr>
    <tr>
      <td><strong>Sucker Punch</strong></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>Yes</td>
      <td>No</td>
      <td>No</td>
      <td>No</td>
    </tr>
    <tr>
      <td><strong>Active Job</strong></td>
      <td>Yes</td>
      <td>Yes</td>
      <td>WIP</td>
      <td>No</td>
      <td>No</td>
      <td>No</td>
    </tr>
    <tr>
      <td><strong>Active Job Inline</strong></td>
      <td>No</td>
      <td>Yes</td>
      <td>N/A</td>
      <td>N/A</td>
      <td>N/A</td>
      <td>N/A</td>
    </tr>
  </tbody>
</table>


### Change Backends

C
Cristian Bica 已提交
222
You can easy change your adapter:
223 224 225 226

```ruby
# be sure to have the adapter gem in your Gemfile and follow the adapter specific
# installation and deployment instructions
227
YourApp::Application.config.active_job.queue_adapter = :sidekiq
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
```

Queues
------

Most of the adapters supports multiple queues. With Active Job you can schedule the job
to run on a specific queue:

```ruby
class GuestsCleanupJob < ActiveJob::Base
  queue_as :low_prio
  #....
end
```

NOTE: Make sure your queueing backend "listens" on your queue name. For some backends
you need to specify the queues to listen to.


Callbacks
---------

Active Job provides hooks during the lifecycle of a job. Callbacks allows you to trigger
logic during the lifecycle of a job.

### Available callbacks

* before_enqueue
* around_enqueue
* after_enqueue
* before_perform
* around_perform
* after_perform

### Usage

```ruby
class GuestsCleanupJob < ActiveJob::Base
  queue_as :default

  before_enqueue do |job|
    # do somthing with the job instance
  end

  around_perform do |job, block|
    # do something before perform
    block.call
    # do something after perform
  end

  def perform
    # Do something later
  end
end

```

GlobalID
--------
Active Job supports GlobalID 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:

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

This works with any class that mixes in ActiveModel::GlobalIdentification, which
by default has been mixed into Active Model classes.


Exceptions
----------
Active Job provides a way to catch exceptions raised during the execution of the
job:

```ruby

class GuestsCleanupJob < ActiveJob::Base
  queue_as :default

  rescue_from(ActiveRecord:NotFound) do |exception|
   # do something with the exception
  end

  def perform
    # Do something later
  end
end
```