这些方法将被包含到任何 Active Job 对象中,为 `perform` 和 `enqueue` 方法添加回调。
方法
实例公共方法
after_enqueue(*filters, &blk) 链接
定义一个在作业排队后立即被调用的回调。
class VideoProcessJob < ActiveJob::Base
queue_as :default
after_enqueue do |job|
result = job.successfully_enqueued? ? "success" : "failure"
$statsd.increment "enqueue-video-job.#{result}"
end
def perform(video_id)
Video.find(video_id).process
end
end
源代码: 显示 | 在 GitHub 上
# File activejob/lib/active_job/callbacks.rb, line 141 def after_enqueue(*filters, &blk) set_callback(:enqueue, :after, *filters, &blk) end
after_perform(*filters, &blk) 链接
定义一个在作业的 `perform` 方法执行完毕后立即被调用的回调。
class VideoProcessJob < ActiveJob::Base
queue_as :default
after_perform do |job|
UserMailer.notify_video_processed(job.arguments.first)
end
def perform(video_id)
Video.find(video_id).process
end
end
源代码: 显示 | 在 GitHub 上
# File activejob/lib/active_job/callbacks.rb, line 69 def after_perform(*filters, &blk) set_callback(:perform, :after, *filters, &blk) end
around_enqueue(*filters, &blk) 链接
定义一个在作业排队前后被调用的回调。
class VideoProcessJob < ActiveJob::Base
queue_as :default
around_enqueue do |job, block|
$statsd.time "video-job.process" do
block.call
end
end
def perform(video_id)
Video.find(video_id).process
end
end
源代码: 显示 | 在 GitHub 上
# File activejob/lib/active_job/callbacks.rb, line 162 def around_enqueue(*filters, &blk) set_callback(:enqueue, :around, *filters, &blk) end
around_perform(*filters, &blk) 链接
定义一个在作业的 `perform` 方法执行前后被调用的回调。
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
只有在执行没有被中止的情况下,才能访问作业的返回值。
class VideoProcessJob < ActiveJob::Base
around_perform do |job, block|
value = block.call
puts value # => "Hello World!"
end
def perform
"Hello World!"
end
end
源代码: 显示 | 在 GitHub 上
# File activejob/lib/active_job/callbacks.rb, line 102 def around_perform(*filters, &blk) set_callback(:perform, :around, *filters, &blk) end
before_enqueue(*filters, &blk) 链接
定义一个在作业排队前立即被调用的回调。
class VideoProcessJob < ActiveJob::Base
queue_as :default
before_enqueue do |job|
$statsd.increment "enqueue-video-job.try"
end
def perform(video_id)
Video.find(video_id).process
end
end
源代码: 显示 | 在 GitHub 上
# File activejob/lib/active_job/callbacks.rb, line 121 def before_enqueue(*filters, &blk) set_callback(:enqueue, :before, *filters, &blk) end
before_perform(*filters, &blk) 链接
定义一个在作业的 `perform` 方法执行前立即被调用的回调。
class VideoProcessJob < ActiveJob::Base
queue_as :default
before_perform do |job|
UserMailer.notify_video_started_processing(job.arguments.first)
end
def perform(video_id)
Video.find(video_id).process
end
end
源代码: 显示 | 在 GitHub 上
# File activejob/lib/active_job/callbacks.rb, line 50 def before_perform(*filters, &blk) set_callback(:perform, :before, *filters, &blk) end