aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-08-11 17:42:49 -0400
committerGitHub <noreply@github.com>2017-08-11 17:42:49 -0400
commit3090f8f0975bfcc7d0aeba178818091e482423d9 (patch)
tree0f67371937dc9ebc9bcbc63bc6bc7f119a25f476 /guides
parentfb849b7b7e0cb843f3211d083ff2eff2cbba3f7d (diff)
parent2f96912229b430ff2c44506b962164df1b66088a (diff)
downloadrails-3090f8f0975bfcc7d0aeba178818091e482423d9.tar.gz
rails-3090f8f0975bfcc7d0aeba178818091e482423d9.tar.bz2
rails-3090f8f0975bfcc7d0aeba178818091e482423d9.zip
Merge pull request #30060 from bdewater/aj-callback-docs
Improve callback examples in Active Job guide
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_job_basics.md52
1 files changed, 30 insertions, 22 deletions
diff --git a/guides/source/active_job_basics.md b/guides/source/active_job_basics.md
index 443be77934..2606bfe280 100644
--- a/guides/source/active_job_basics.md
+++ b/guides/source/active_job_basics.md
@@ -260,40 +260,48 @@ 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 { |job| $statsd.increment "#{job.name.underscore}.enqueue" }
+end
+```
+
+### Available callbacks
+
+* `before_enqueue`
+* `around_enqueue`
+* `after_enqueue`
+* `before_perform`
+* `around_perform`
+* `after_perform`
+
Action Mailer
------------