2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2016-08-06 12:40:07 -04:00
|
|
|
require "active_support/callbacks"
|
2019-11-28 07:56:49 -05:00
|
|
|
require "active_support/core_ext/module/attribute_accessors"
|
2014-05-22 13:33:23 -04:00
|
|
|
|
|
|
|
module ActiveJob
|
2014-08-15 18:31:39 -04:00
|
|
|
# = Active Job Callbacks
|
|
|
|
#
|
2014-12-31 05:21:37 -05:00
|
|
|
# Active Job provides hooks during the life cycle of a job. Callbacks allow you
|
2017-05-13 07:56:37 -04:00
|
|
|
# to trigger logic during this cycle. Available callbacks are:
|
2014-08-15 18:31:39 -04:00
|
|
|
#
|
|
|
|
# * <tt>before_enqueue</tt>
|
|
|
|
# * <tt>around_enqueue</tt>
|
|
|
|
# * <tt>after_enqueue</tt>
|
|
|
|
# * <tt>before_perform</tt>
|
|
|
|
# * <tt>around_perform</tt>
|
|
|
|
# * <tt>after_perform</tt>
|
|
|
|
#
|
2017-05-13 07:56:37 -04:00
|
|
|
# NOTE: Calling the same callback multiple times will overwrite previous callback definitions.
|
|
|
|
#
|
2014-05-22 13:33:23 -04:00
|
|
|
module Callbacks
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include ActiveSupport::Callbacks
|
2014-08-15 18:31:39 -04:00
|
|
|
|
2016-02-26 00:47:01 -05:00
|
|
|
class << self
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
define_callbacks :execute
|
|
|
|
end
|
|
|
|
|
2014-05-22 13:33:23 -04:00
|
|
|
included do
|
2019-11-28 07:56:49 -05:00
|
|
|
class_attribute :return_false_on_aborted_enqueue, instance_accessor: false, instance_predicate: false, default: false
|
|
|
|
cattr_accessor :skip_after_callbacks_if_terminated, instance_accessor: false, default: false
|
2021-10-14 18:29:35 -04:00
|
|
|
singleton_class.deprecate :skip_after_callbacks_if_terminated, :skip_after_callbacks_if_terminated=
|
2018-09-26 12:25:20 -04:00
|
|
|
|
2021-10-14 18:29:35 -04:00
|
|
|
define_callbacks :perform, skip_after_callbacks_if_terminated: true
|
|
|
|
define_callbacks :enqueue, skip_after_callbacks_if_terminated: true
|
2014-05-22 13:33:23 -04:00
|
|
|
end
|
2014-08-15 18:31:39 -04:00
|
|
|
|
2014-11-03 22:21:43 -05:00
|
|
|
# These methods will be included into any Active Job object, adding
|
2014-11-03 22:31:31 -05:00
|
|
|
# callbacks for +perform+ and +enqueue+ methods.
|
2014-05-22 13:33:23 -04:00
|
|
|
module ClassMethods
|
2014-08-15 18:31:39 -04:00
|
|
|
# Defines a callback that will get called right before the
|
|
|
|
# job's perform method is executed.
|
|
|
|
#
|
|
|
|
# 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
|
2014-09-17 15:46:53 -04:00
|
|
|
# end
|
2014-08-15 18:31:39 -04:00
|
|
|
#
|
2014-05-22 13:33:23 -04:00
|
|
|
def before_perform(*filters, &blk)
|
|
|
|
set_callback(:perform, :before, *filters, &blk)
|
|
|
|
end
|
|
|
|
|
2014-08-15 18:31:39 -04:00
|
|
|
# Defines a callback that will get called right after the
|
|
|
|
# job's perform method has finished.
|
|
|
|
#
|
|
|
|
# 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
|
2014-09-17 15:46:53 -04:00
|
|
|
# end
|
2014-08-15 18:31:39 -04:00
|
|
|
#
|
2014-05-22 13:33:23 -04:00
|
|
|
def after_perform(*filters, &blk)
|
|
|
|
set_callback(:perform, :after, *filters, &blk)
|
|
|
|
end
|
|
|
|
|
2014-08-15 18:31:39 -04:00
|
|
|
# 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
|
2014-09-17 15:46:53 -04:00
|
|
|
# end
|
2014-08-15 18:31:39 -04:00
|
|
|
#
|
2019-12-28 13:04:38 -05:00
|
|
|
# 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
|
|
|
|
#
|
2014-05-22 13:33:23 -04:00
|
|
|
def around_perform(*filters, &blk)
|
|
|
|
set_callback(:perform, :around, *filters, &blk)
|
|
|
|
end
|
|
|
|
|
2014-08-15 18:31:39 -04:00
|
|
|
# Defines a callback that will get called right before the
|
|
|
|
# job is enqueued.
|
|
|
|
#
|
|
|
|
# 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
|
2014-09-17 15:46:53 -04:00
|
|
|
# end
|
2014-08-15 18:31:39 -04:00
|
|
|
#
|
2014-05-22 13:33:23 -04:00
|
|
|
def before_enqueue(*filters, &blk)
|
|
|
|
set_callback(:enqueue, :before, *filters, &blk)
|
|
|
|
end
|
|
|
|
|
2014-08-15 18:31:39 -04:00
|
|
|
# Defines a callback that will get called right after the
|
|
|
|
# job is enqueued.
|
|
|
|
#
|
|
|
|
# class VideoProcessJob < ActiveJob::Base
|
|
|
|
# queue_as :default
|
|
|
|
#
|
|
|
|
# after_enqueue do |job|
|
|
|
|
# $statsd.increment "enqueue-video-job.success"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def perform(video_id)
|
|
|
|
# Video.find(video_id).process
|
|
|
|
# end
|
2014-09-17 15:46:53 -04:00
|
|
|
# end
|
2014-08-15 18:31:39 -04:00
|
|
|
#
|
2014-05-22 13:33:23 -04:00
|
|
|
def after_enqueue(*filters, &blk)
|
|
|
|
set_callback(:enqueue, :after, *filters, &blk)
|
|
|
|
end
|
|
|
|
|
2018-11-18 23:36:44 -05:00
|
|
|
# Defines a callback that will get called around the enqueuing
|
2016-05-27 18:39:30 -04:00
|
|
|
# of the job.
|
2014-08-15 18:31:39 -04:00
|
|
|
#
|
|
|
|
# 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
|
2014-09-17 15:46:53 -04:00
|
|
|
# end
|
2014-08-15 18:31:39 -04:00
|
|
|
#
|
2014-05-22 13:33:23 -04:00
|
|
|
def around_enqueue(*filters, &blk)
|
|
|
|
set_callback(:enqueue, :around, *filters, &blk)
|
|
|
|
end
|
2019-12-14 21:46:02 -05:00
|
|
|
end
|
2014-05-22 13:33:23 -04:00
|
|
|
end
|
2014-08-15 18:31:39 -04:00
|
|
|
end
|