Skip to content

Commit

Permalink
Extract instrument method.
Browse files Browse the repository at this point in the history
Similar to Action View's and Action Controller's instrument helpers.
  • Loading branch information
kaspth committed Sep 23, 2018
1 parent 609b1ec commit baffada
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions activejob/lib/active_job/exceptions.rb
Expand Up @@ -49,18 +49,12 @@ def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: ni
if executions < attempts
retry_job wait: determine_delay(wait), queue: queue, priority: priority, error: error
else
payload = {
job: self,
adapter: self.class.queue_adapter,
error: error
}

if block_given?
ActiveSupport::Notifications.instrument("retry_stopped.active_job", payload) do
instrument :retry_stopped, error: error do
yield self, error
end
else
ActiveSupport::Notifications.instrument("retry_stopped.active_job", payload)
instrument :retry_stopped, error: error
raise error
end
end
Expand All @@ -87,16 +81,8 @@ def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: ni
# end
def discard_on(*exceptions)
rescue_from(*exceptions) do |error|
payload = {
job: self,
adapter: self.class.queue_adapter,
error: error
}

ActiveSupport::Notifications.instrument("discard.active_job", payload) do
if block_given?
yield self, error
end
instrument :discard, error: error do
yield self, error if block_given?
end
end
end
Expand Down Expand Up @@ -124,14 +110,7 @@ def discard_on(*exceptions)
# end
# end
def retry_job(options = {})
payload = {
job: self,
adapter: self.class.queue_adapter,
error: options[:error],
wait: options[:wait]
}

ActiveSupport::Notifications.instrument("enqueue_retry.active_job", payload) do
instrument :enqueue_retry, options.slice(:error, :wait) do
enqueue options
end
end
Expand All @@ -154,5 +133,11 @@ def determine_delay(seconds_or_duration_or_algorithm)
raise "Couldn't determine a delay based on #{seconds_or_duration_or_algorithm.inspect}"
end
end

def instrument(name, error: nil, wait: nil, &block)
payload = { job: self, adapter: self.class.queue_adapter, error: error, wait: wait }

ActiveSupport::Notifications.instrument("#{name}.active_job", payload, &block)
end
end
end

0 comments on commit baffada

Please sign in to comment.