2019-09-23 12:29:52 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActiveJob
|
|
|
|
module Instrumentation #:nodoc:
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2019-09-23 16:13:54 -04:00
|
|
|
around_enqueue do |_, block|
|
|
|
|
scheduled_at ? instrument(:enqueue_at, &block) : instrument(:enqueue, &block)
|
2019-09-23 12:29:52 -04:00
|
|
|
end
|
|
|
|
|
2019-09-23 16:13:54 -04:00
|
|
|
around_perform do |_, block|
|
2020-12-30 01:06:09 -05:00
|
|
|
ActiveSupport::Notifications.instrument \
|
|
|
|
"perform_start.active_job", { adapter: queue_adapter, job: self }, &block
|
2019-09-23 12:29:52 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-23 16:13:54 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def instrument(operation, payload = {}, &block)
|
2019-11-28 17:40:24 -05:00
|
|
|
enhanced_block = ->(event_payload) do
|
2020-12-28 00:30:30 -05:00
|
|
|
value = block.call if block
|
|
|
|
|
2019-12-28 13:12:33 -05:00
|
|
|
if defined?(@_halted_callback_hook_called) && @_halted_callback_hook_called
|
|
|
|
event_payload[:aborted] = true
|
|
|
|
@_halted_callback_hook_called = nil
|
|
|
|
end
|
2020-12-28 00:30:30 -05:00
|
|
|
|
|
|
|
value
|
2019-11-28 17:40:24 -05:00
|
|
|
end
|
|
|
|
|
2019-09-23 16:13:54 -04:00
|
|
|
ActiveSupport::Notifications.instrument \
|
2019-11-28 17:40:24 -05:00
|
|
|
"#{operation}.active_job", payload.merge(adapter: queue_adapter, job: self), &enhanced_block
|
2019-09-23 16:13:54 -04:00
|
|
|
end
|
2019-12-28 13:12:33 -05:00
|
|
|
|
2020-02-25 21:09:00 -05:00
|
|
|
def halted_callback_hook(*)
|
2019-12-28 13:12:33 -05:00
|
|
|
super
|
|
|
|
@_halted_callback_hook_called = true
|
|
|
|
end
|
2019-09-23 12:29:52 -04:00
|
|
|
end
|
|
|
|
end
|