Module: ActiveJob::Callbacks::ClassMethods
- Defined in:
- lib/active_job/callbacks.rb
Overview
These methods will be included into any Active Job object, adding callbacks for perform
and enqueue
methods.
Instance Method Summary collapse
-
#after_enqueue(*filters, &blk) ⇒ Object
Defines a callback that will get called right after the job is enqueued.
-
#after_perform(*filters, &blk) ⇒ Object
Defines a callback that will get called right after the job’s perform method has finished.
-
#around_enqueue(*filters, &blk) ⇒ Object
Defines a callback that will get called around the enqueuing of the job.
-
#around_perform(*filters, &blk) ⇒ Object
Defines a callback that will get called around the job’s perform method.
-
#before_enqueue(*filters, &blk) ⇒ Object
Defines a callback that will get called right before the job is enqueued.
-
#before_perform(*filters, &blk) ⇒ Object
Defines a callback that will get called right before the job’s perform method is executed.
Instance Method Details
#after_enqueue(*filters, &blk) ⇒ Object
146 147 148 |
# File 'lib/active_job/callbacks.rb', line 146 def after_enqueue(*filters, &blk) set_callback(:enqueue, :after, *filters, &blk) end |
#after_perform(*filters, &blk) ⇒ Object
75 76 77 |
# File 'lib/active_job/callbacks.rb', line 75 def after_perform(*filters, &blk) set_callback(:perform, :after, *filters, &blk) end |
#around_enqueue(*filters, &blk) ⇒ Object
167 168 169 |
# File 'lib/active_job/callbacks.rb', line 167 def around_enqueue(*filters, &blk) set_callback(:enqueue, :around, *filters, &blk) end |
#around_perform(*filters, &blk) ⇒ Object
Defines a callback that will get called around the job’s perform method.
class VideoProcessJob < ActiveJob::Base
queue_as :default
around_perform do |job, block|
UserMailer.notify_video_started_processing(job.arguments.first)
block.call
UserMailer.notify_video_processed(job.arguments.first)
end
def perform(video_id)
Video.find(video_id).process
end
end
You can access the return value of the job only if the execution wasn’t halted.
class VideoProcessJob < ActiveJob::Base
around_perform do |job, block|
value = block.call
puts value # => "Hello World!"
end
def perform
"Hello World!"
end
end
108 109 110 |
# File 'lib/active_job/callbacks.rb', line 108 def around_perform(*filters, &blk) set_callback(:perform, :around, *filters, &blk) end |
#before_enqueue(*filters, &blk) ⇒ Object
127 128 129 |
# File 'lib/active_job/callbacks.rb', line 127 def before_enqueue(*filters, &blk) set_callback(:enqueue, :before, *filters, &blk) end |
#before_perform(*filters, &blk) ⇒ Object
56 57 58 |
# File 'lib/active_job/callbacks.rb', line 56 def before_perform(*filters, &blk) set_callback(:perform, :before, *filters, &blk) end |