aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorBart de Water <bartdewater@gmail.com>2017-08-03 10:32:18 -0400
committerBart de Water <bartdewater@gmail.com>2017-08-03 10:37:34 -0400
commit2013dede296979172d6cae9e5e85045c3f0e9623 (patch)
tree41e5aa4af4a4fcdc2cad11a5926a59c96b1441b7 /guides
parent5fd50c387bdfc2761859ec7a57477bcd56e93210 (diff)
downloadrails-2013dede296979172d6cae9e5e85045c3f0e9623.tar.gz
rails-2013dede296979172d6cae9e5e85045c3f0e9623.tar.bz2
rails-2013dede296979172d6cae9e5e85045c3f0e9623.zip
Improve callback examples in Active Job guide [ci skip]
The advice for symbol/block form is taken from the Active Record guides.
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_job_basics.md54
1 files changed, 32 insertions, 22 deletions
diff --git a/guides/source/active_job_basics.md b/guides/source/active_job_basics.md
index 443be77934..bde234bf84 100644
--- a/guides/source/active_job_basics.md
+++ b/guides/source/active_job_basics.md
@@ -260,40 +260,50 @@ backends you need to specify the queues to listen to.
Callbacks
---------
-Active Job provides hooks during the life cycle of a job. Callbacks allow you to
-trigger logic during the life cycle of a job.
-
-### Available callbacks
-
-* `before_enqueue`
-* `around_enqueue`
-* `after_enqueue`
-* `before_perform`
-* `around_perform`
-* `after_perform`
-
-### Usage
+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:
```ruby
class GuestsCleanupJob < ApplicationJob
queue_as :default
- before_enqueue do |job|
- # Do something with the job instance
- end
-
- around_perform do |job, block|
- # Do something before perform
- block.call
- # Do something after perform
- end
+ around_perform :around_cleanup
def perform
# Do something later
end
+
+ private
+ def around_cleanup(job)
+ # Do something before perform
+ yield
+ # Do something after perform
+ end
+end
+```
+
+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:
+
+```ruby
+class ApplicationJob
+ before_enqueue do |job|
+ $statsd.increment "#{job.name.underscore}.enqueue"
+ end
end
```
+### Available callbacks
+
+* `before_enqueue`
+* `around_enqueue`
+* `after_enqueue`
+* `before_perform`
+* `around_perform`
+* `after_perform`
+
Action Mailer
------------