-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive_job_basics.html
628 lines (571 loc) · 28.5 KB
/
active_job_basics.html
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
222
223
224
225
226
227
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Active Job Basics — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" data-turbolinks-track="reload">
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<script src="javascripts/syntaxhighlighter.js" data-turbolinks-track="reload"></script>
<script src="javascripts/turbolinks.js" data-turbolinks-track="reload"></script>
<script src="javascripts/guides.js" data-turbolinks-track="reload"></script>
<script src="javascripts/responsive-tables.js" data-turbolinks-track="reload"></script>
</head>
<body class="guide">
<div>
<img src="images/edge_badge.png" alt="edge-badge" id="edge-badge" />
</div>
<div id="topNav">
<div class="wrapper">
<strong class="more-info-label">Veja mais em <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
<span class="red-button more-info-button">
Mais Ruby on Rails
</span>
<ul class="more-info-links s-hidden">
<li class="more-info"><a href="https://weblog.rubyonrails.org/">Blog</a></li>
<li class="more-info"><a href="https://guides.rubyonrails.org/">Guia</a></li>
<li class="more-info"><a href="http://api.rubyonrails.org/">API</a></li>
<li class="more-info"><a href="https://stackoverflow.com/questions/tagged/ruby-on-rails">Pedir ajuda</a></li>
<li class="more-info"><a href="https://github.com/rails/rails">Contribuir no GitHub</a></li>
</ul>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
<ul class="nav">
<li><a class="nav-item" href="index.html">Home</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guias</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<div class="guides-section-container">
<div class="guides-section">
<dt>Comece Aqui</dt>
</div>
<div class="guides-section">
<dt>Models</dt>
</div>
<div class="guides-section">
<dt>Views</dt>
</div>
<div class="guides-section">
<dt>Controllers</dt>
</div>
<div class="guides-section">
<dt>Other Components</dt>
</div>
<div class="guides-section">
<dt>Digging Deeper</dt>
</div>
<div class="guides-section">
<dt>Extending Rails</dt>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">Creating and Customizing Rails Generators & Templates</a></dd>
</div>
<div class="guides-section">
<dt>Contributions</dt>
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">Guides Guidelines</a></dd>
</div>
<div class="guides-section">
<dt>Policies</dt>
<dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
</div>
<div class="guides-section">
<dt>Release Notes</dt>
<dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
<dd><a href="5_2_release_notes.html">Version 5.2 - April 2018</a></dd>
<dd><a href="5_1_release_notes.html">Version 5.1 - April 2017</a></dd>
<dd><a href="5_0_release_notes.html">Version 5.0 - June 2016</a></dd>
<dd><a href="4_2_release_notes.html">Version 4.2 - December 2014</a></dd>
<dd><a href="4_1_release_notes.html">Version 4.1 - April 2014</a></dd>
<dd><a href="4_0_release_notes.html">Version 4.0 - June 2013</a></dd>
<dd><a href="3_2_release_notes.html">Version 3.2 - January 2012</a></dd>
<dd><a href="3_1_release_notes.html">Version 3.1 - August 2011</a></dd>
<dd><a href="3_0_release_notes.html">Version 3.0 - August 2010</a></dd>
<dd><a href="2_3_release_notes.html">Version 2.3 - March 2009</a></dd>
<dd><a href="2_2_release_notes.html">Version 2.2 - November 2008</a></dd>
</div>
</div>
</div>
</li>
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribua</a></li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">Guias</option>
<optgroup label="Comece Aqui">
</optgroup>
<optgroup label="Models">
</optgroup>
<optgroup label="Views">
</optgroup>
<optgroup label="Controllers">
</optgroup>
<optgroup label="Other Components">
</optgroup>
<optgroup label="Digging Deeper">
</optgroup>
<optgroup label="Extending Rails">
<option value="rails_on_rack.html">Rails on Rack</option>
<option value="generators.html">Creating and Customizing Rails Generators & Templates</option>
</optgroup>
<optgroup label="Contributions">
<option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
<option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
<option value="ruby_on_rails_guides_guidelines.html">Guides Guidelines</option>
</optgroup>
<optgroup label="Policies">
<option value="maintenance_policy.html">Maintenance Policy</option>
</optgroup>
<optgroup label="Release Notes">
<option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
<option value="5_2_release_notes.html">Version 5.2 - April 2018</option>
<option value="5_1_release_notes.html">Version 5.1 - April 2017</option>
<option value="5_0_release_notes.html">Version 5.0 - June 2016</option>
<option value="4_2_release_notes.html">Version 4.2 - December 2014</option>
<option value="4_1_release_notes.html">Version 4.1 - April 2014</option>
<option value="4_0_release_notes.html">Version 4.0 - June 2013</option>
<option value="3_2_release_notes.html">Version 3.2 - January 2012</option>
<option value="3_1_release_notes.html">Version 3.1 - August 2011</option>
<option value="3_0_release_notes.html">Version 3.0 - August 2010</option>
<option value="2_3_release_notes.html">Version 2.3 - March 2009</option>
<option value="2_2_release_notes.html">Version 2.2 - November 2008</option>
</optgroup>
</select>
</li>
</ul>
</div>
</div>
<hr class="hide" />
<div id="feature">
<div class="wrapper">
<h2>Active Job Basics</h2><p>This guide provides you with all you need to get started in creating,
enqueuing and executing background jobs.</p><p>After reading this guide, you will know:</p>
<ul>
<li>How to create jobs.</li>
<li>How to enqueue jobs.</li>
<li>How to run jobs in the background.</li>
<li>How to send emails from your application asynchronously.</li>
</ul>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li><a href="#introduction">Introduction</a></li>
<li><a href="#the-purpose-of-active-job">The Purpose of Active Job</a></li>
<li>
<a href="#creating-a-job">Creating a Job</a>
<ul>
<li><a href="#create-the-job">Create the Job</a></li>
<li><a href="#enqueue-the-job">Enqueue the Job</a></li>
</ul>
</li>
<li>
<a href="#job-execution">Job Execution</a>
<ul>
<li><a href="#backends">Backends</a></li>
<li><a href="#setting-the-backend">Setting the Backend</a></li>
<li><a href="#starting-the-backend">Starting the Backend</a></li>
</ul>
</li>
<li><a href="#queues">Queues</a></li>
<li>
<a href="#callbacks">Callbacks</a>
<ul>
<li><a href="#available-callbacks">Available callbacks</a></li>
</ul>
</li>
<li><a href="#action-mailer">Action Mailer</a></li>
<li><a href="#internationalization">Internationalization</a></li>
<li>
<a href="#supported-types-for-arguments">Supported types for arguments</a>
<ul>
<li><a href="#globalid">GlobalID</a></li>
<li><a href="#serializers">Serializers</a></li>
</ul>
</li>
<li>
<a href="#exceptions">Exceptions</a>
<ul>
<li><a href="#retrying-or-discarding-failed-jobs">Retrying or Discarding failed jobs</a></li>
<li><a href="#deserialization">Deserialization</a></li>
</ul>
</li>
<li><a href="#job-testing">Job Testing</a></li>
</ol>
</div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="introduction"><a class="anchorlink" href="#introduction">1 Introduction</a></h3><p>Active Job is a framework for declaring jobs and making them run on a variety
of queuing backends. These jobs can be everything from regularly scheduled
clean-ups, to billing charges, to mailings. Anything that can be chopped up
into small units of work and run in parallel, really.</p><h3 id="the-purpose-of-active-job"><a class="anchorlink" href="#the-purpose-of-active-job">2 The Purpose of Active Job</a></h3><p>The main point is to ensure that all Rails apps will have a job infrastructure
in place. We can then have framework features and other gems build on top of that,
without having to worry about API differences between various job runners such as
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.</p><div class="note"><p>Rails by default comes with an asynchronous queuing implementation that
runs jobs with an in-process thread pool. Jobs will run asynchronously, but any
jobs in the queue will be dropped upon restart.</p></div><h3 id="creating-a-job"><a class="anchorlink" href="#creating-a-job">3 Creating a Job</a></h3><p>This section will provide a step-by-step guide to creating a job and enqueuing it.</p><h4 id="create-the-job"><a class="anchorlink" href="#create-the-job">3.1 Create the Job</a></h4><p>Active Job provides a Rails generator to create jobs. The following will create a
job in <code>app/jobs</code> (with an attached test case under <code>test/jobs</code>):</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails generate job guests_cleanup
invoke test_unit
create test/jobs/guests_cleanup_job_test.rb
create app/jobs/guests_cleanup_job.rb
</pre>
</div>
<p>You can also create a job that will run on a specific queue:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails generate job guests_cleanup --queue urgent
</pre>
</div>
<p>If you don't want to use a generator, you could create your own file inside of
<code>app/jobs</code>, just make sure that it inherits from <code>ApplicationJob</code>.</p><p>Here's what a job looks like:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class GuestsCleanupJob < ApplicationJob
queue_as :default
def perform(*guests)
# Do something later
end
end
</pre>
</div>
<p>Note that you can define <code>perform</code> with as many arguments as you want.</p><h4 id="enqueue-the-job"><a class="anchorlink" href="#enqueue-the-job">3.2 Enqueue the Job</a></h4><p>Enqueue a job like so:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Enqueue a job to be performed as soon as the queuing system is
# free.
GuestsCleanupJob.perform_later guest
</pre>
</div>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Enqueue a job to be performed tomorrow at noon.
GuestsCleanupJob.set(wait_until: Date.tomorrow.noon).perform_later(guest)
</pre>
</div>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Enqueue a job to be performed 1 week from now.
GuestsCleanupJob.set(wait: 1.week).perform_later(guest)
</pre>
</div>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# `perform_now` and `perform_later` will call `perform` under the hood so
# you can pass as many arguments as defined in the latter.
GuestsCleanupJob.perform_later(guest1, guest2, filter: 'some_filter')
</pre>
</div>
<p>That's it!</p><h3 id="job-execution"><a class="anchorlink" href="#job-execution">4 Job Execution</a></h3><p>For enqueuing and executing jobs in production you need to set up a queuing backend,
that is to say you need to decide for a 3rd-party queuing library that Rails should use.
Rails itself only provides an in-process queuing system, which only keeps the jobs in RAM.
If the process crashes or the machine is reset, then all outstanding jobs are lost with the
default async backend. This may be fine for smaller apps or non-critical jobs, but most
production apps will need to pick a persistent backend.</p><h4 id="backends"><a class="anchorlink" href="#backends">4.1 Backends</a></h4><p>Active Job has built-in adapters for multiple queuing backends (Sidekiq,
Resque, Delayed Job, and others). To get an up-to-date list of the adapters
see the API Documentation for <a href="http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html">ActiveJob::QueueAdapters</a>.</p><h4 id="setting-the-backend"><a class="anchorlink" href="#setting-the-backend">4.2 Setting the Backend</a></h4><p>You can easily set your queuing backend:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# config/application.rb
module YourApp
class Application < Rails::Application
# Be sure to have the adapter's gem in your Gemfile
# and follow the adapter's specific installation
# and deployment instructions.
config.active_job.queue_adapter = :sidekiq
end
end
</pre>
</div>
<p>You can also configure your backend on a per job basis.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class GuestsCleanupJob < ApplicationJob
self.queue_adapter = :resque
#....
end
# Now your job will use `resque` as its backend queue adapter overriding what
# was configured in `config.active_job.queue_adapter`.
</pre>
</div>
<h4 id="starting-the-backend"><a class="anchorlink" href="#starting-the-backend">4.3 Starting the Backend</a></h4><p>Since jobs run in parallel to your Rails application, most queuing libraries
require that you start a library-specific queuing service (in addition to
starting your Rails app) for the job processing to work. Refer to library
documentation for instructions on starting your queue backend.</p><p>Here is a noncomprehensive list of documentation:</p>
<ul>
<li><a href="https://github.com/mperham/sidekiq/wiki/Active-Job">Sidekiq</a></li>
<li><a href="https://github.com/resque/resque/wiki/ActiveJob">Resque</a></li>
<li><a href="https://github.com/jondot/sneakers/wiki/How-To:-Rails-Background-Jobs-with-ActiveJob">Sneakers</a></li>
<li><a href="https://github.com/brandonhilkert/sucker_punch#active-job">Sucker Punch</a></li>
<li><a href="https://github.com/QueueClassic/queue_classic#active-job">Queue Classic</a></li>
<li><a href="https://github.com/collectiveidea/delayed_job#active-job">Delayed Job</a></li>
</ul>
<h3 id="queues"><a class="anchorlink" href="#queues">5 Queues</a></h3><p>Most of the adapters support multiple queues. With Active Job you can schedule
the job to run on a specific queue:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class GuestsCleanupJob < ApplicationJob
queue_as :low_priority
#....
end
</pre>
</div>
<p>You can prefix the queue name for all your jobs using
<code>config.active_job.queue_name_prefix</code> in <code>application.rb</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# config/application.rb
module YourApp
class Application < Rails::Application
config.active_job.queue_name_prefix = Rails.env
end
end
# app/jobs/guests_cleanup_job.rb
class GuestsCleanupJob < ApplicationJob
queue_as :low_priority
#....
end
# Now your job will run on queue production_low_priority on your
# production environment and on staging_low_priority
# on your staging environment
</pre>
</div>
<p>The default queue name prefix delimiter is '_'. This can be changed by setting
<code>config.active_job.queue_name_delimiter</code> in <code>application.rb</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# config/application.rb
module YourApp
class Application < Rails::Application
config.active_job.queue_name_prefix = Rails.env
config.active_job.queue_name_delimiter = '.'
end
end
# app/jobs/guests_cleanup_job.rb
class GuestsCleanupJob < ApplicationJob
queue_as :low_priority
#....
end
# Now your job will run on queue production.low_priority on your
# production environment and on staging.low_priority
# on your staging environment
</pre>
</div>
<p>If you want more control on what queue a job will be run you can pass a <code>:queue</code>
option to <code>#set</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
MyJob.set(queue: :another_queue).perform_later(record)
</pre>
</div>
<p>To control the queue from the job level you can pass a block to <code>#queue_as</code>. The
block will be executed in the job context (so you can access <code>self.arguments</code>)
and you must return the queue name:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class ProcessVideoJob < ApplicationJob
queue_as do
video = self.arguments.first
if video.owner.premium?
:premium_videojobs
else
:videojobs
end
end
def perform(video)
# Do process video
end
end
ProcessVideoJob.perform_later(Video.last)
</pre>
</div>
<div class="note"><p>Make sure your queuing backend "listens" on your queue name. For some
backends you need to specify the queues to listen to.</p></div><h3 id="callbacks"><a class="anchorlink" href="#callbacks">6 Callbacks</a></h3><p>Active Job provides hooks to trigger logic during the life cycle of a job. Like
other callbacks in Rails, you can implement the callbacks as ordinary methods
and use a macro-style class method to register them as callbacks:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class GuestsCleanupJob < ApplicationJob
queue_as :default
around_perform :around_cleanup
def perform
# Do something later
end
private
def around_cleanup
# Do something before perform
yield
# Do something after perform
end
end
</pre>
</div>
<p>The macro-style class methods can also receive a block. Consider using this
style if the code inside your block is so short that it fits in a single line.
For example, you could send metrics for every job enqueued:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class ApplicationJob < ActiveJob::Base
before_enqueue { |job| $statsd.increment "#{job.class.name.underscore}.enqueue" }
end
</pre>
</div>
<h4 id="available-callbacks"><a class="anchorlink" href="#available-callbacks">6.1 Available callbacks</a></h4>
<ul>
<li><code>before_enqueue</code></li>
<li><code>around_enqueue</code></li>
<li><code>after_enqueue</code></li>
<li><code>before_perform</code></li>
<li><code>around_perform</code></li>
<li><code>after_perform</code></li>
</ul>
<h3 id="action-mailer"><a class="anchorlink" href="#action-mailer">7 Action Mailer</a></h3><p>One of the most common jobs in a modern web application is sending emails outside
of the request-response cycle, so the user doesn't have to wait on it. Active Job
is integrated with Action Mailer so you can easily send emails asynchronously:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# If you want to send the email now use #deliver_now
UserMailer.welcome(@user).deliver_now
# If you want to send the email through Active Job use #deliver_later
UserMailer.welcome(@user).deliver_later
</pre>
</div>
<div class="note"><p>Using the asynchronous queue from a Rake task (for example, to
send an email using <code>.deliver_later</code>) will generally not work because Rake will
likely end, causing the in-process thread pool to be deleted, before any/all
of the <code>.deliver_later</code> emails are processed. To avoid this problem, use
<code>.deliver_now</code> or run a persistent queue in development.</p></div><h3 id="internationalization"><a class="anchorlink" href="#internationalization">8 Internationalization</a></h3><p>Each job uses the <code>I18n.locale</code> set when the job was created. Useful if you send
emails asynchronously:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
I18n.locale = :eo
UserMailer.welcome(@user).deliver_later # Email will be localized to Esperanto.
</pre>
</div>
<h3 id="supported-types-for-arguments"><a class="anchorlink" href="#supported-types-for-arguments">9 Supported types for arguments</a></h3><p>ActiveJob supports the following types of arguments by default:</p>
<ul>
<li>Basic types (<code>NilClass</code>, <code>String</code>, <code>Integer</code>, <code>Float</code>, <code>BigDecimal</code>, <code>TrueClass</code>, <code>FalseClass</code>)</li>
<li><code>Symbol</code></li>
<li><code>Date</code></li>
<li><code>Time</code></li>
<li><code>DateTime</code></li>
<li><code>ActiveSupport::TimeWithZone</code></li>
<li><code>ActiveSupport::Duration</code></li>
<li>
<code>Hash</code> (Keys should be of <code>String</code> or <code>Symbol</code> type)</li>
<li><code>ActiveSupport::HashWithIndifferentAccess</code></li>
<li><code>Array</code></li>
</ul>
<h4 id="globalid"><a class="anchorlink" href="#globalid">9.1 GlobalID</a></h4><p>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:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class TrashableCleanupJob < ApplicationJob
def perform(trashable_class, trashable_id, depth)
trashable = trashable_class.constantize.find(trashable_id)
trashable.cleanup(depth)
end
end
</pre>
</div>
<p>Now you can simply do:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class TrashableCleanupJob < ApplicationJob
def perform(trashable, depth)
trashable.cleanup(depth)
end
end
</pre>
</div>
<p>This works with any class that mixes in <code>GlobalID::Identification</code>, which
by default has been mixed into Active Record classes.</p><h4 id="serializers"><a class="anchorlink" href="#serializers">9.2 Serializers</a></h4><p>You can extend the list of supported argument types. You just need to define your own serializer:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
# Checks if an argument should be serialized by this serializer.
def serialize?(argument)
argument.is_a? Money
end
# Converts an object to a simpler representative using supported object types.
# The recommended representative is a Hash with a specific key. Keys can be of basic types only.
# You should call `super` to add the custom serializer type to the hash.
def serialize(money)
super(
"amount" => money.amount,
"currency" => money.currency
)
end
# Converts serialized value into a proper object.
def deserialize(hash)
Money.new(hash["amount"], hash["currency"])
end
end
</pre>
</div>
<p>and add this serializer to the list:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Rails.application.config.active_job.custom_serializers << MoneySerializer
</pre>
</div>
<h3 id="exceptions"><a class="anchorlink" href="#exceptions">10 Exceptions</a></h3><p>Active Job provides a way to catch exceptions raised during the execution of the
job:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class GuestsCleanupJob < ApplicationJob
queue_as :default
rescue_from(ActiveRecord::RecordNotFound) do |exception|
# Do something with the exception
end
def perform
# Do something later
end
end
</pre>
</div>
<h4 id="retrying-or-discarding-failed-jobs"><a class="anchorlink" href="#retrying-or-discarding-failed-jobs">10.1 Retrying or Discarding failed jobs</a></h4><p>It's also possible to retry or discard a job if an exception is raised during execution.
For example:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class RemoteServiceJob < ApplicationJob
retry_on CustomAppException # defaults to 3s wait, 5 attempts
discard_on ActiveJob::DeserializationError
def perform(*args)
# Might raise CustomAppException or ActiveJob::DeserializationError
end
end
</pre>
</div>
<p>To get more details see the API Documentation for <a href="http://edgeapi.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html">ActiveJob::Exceptions</a>.</p><h4 id="deserialization"><a class="anchorlink" href="#deserialization">10.2 Deserialization</a></h4><p>GlobalID allows serializing full Active Record objects passed to <code>#perform</code>.</p><p>If a passed record is deleted after the job is enqueued but before the <code>#perform</code>
method is called Active Job will raise an <code>ActiveJob::DeserializationError</code>
exception.</p><h3 id="job-testing"><a class="anchorlink" href="#job-testing">11 Job Testing</a></h3><p>You can find detailed instructions on how to test your jobs in the
<a href="testing.html#testing-jobs">testing guide</a>.</p>
<h3>Feedback</h3>
<p>
You're encouraged to help improve the quality of this guide.
</p>
<p>
Please contribute if you see any typos or factual errors.
To get started, you can read our <a href="https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
</p>
<p>
You may also find incomplete content or stuff that is not up to date.
Please do add any missing documentation for master. Make sure to check
<a href="https://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
if the issues are already fixed or not on the master branch.
Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
for style and conventions.
</p>
<p>
If for whatever reason you spot something to fix but cannot patch it yourself, please
<a href="https://github.com/rails/rails/issues">open an issue</a>.
</p>
<p>And last but not least, any kind of discussion regarding Ruby on Rails
documentation is very welcome on the <a href="https://groups.google.com/forum/#!forum/rubyonrails-docs">rubyonrails-docs mailing list</a>.
</p>
</div>
</div>
</div>
<hr class="hide" />
<div id="footer">
<div class="wrapper">
<p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
</div>
</div>
</body>
</html>