Skip to content

Commit baffada

Browse files
committed
Extract instrument method.
Similar to Action View's and Action Controller's instrument helpers.
1 parent 609b1ec commit baffada

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

activejob/lib/active_job/exceptions.rb

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,12 @@ def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: ni
4949
if executions < attempts
5050
retry_job wait: determine_delay(wait), queue: queue, priority: priority, error: error
5151
else
52-
payload = {
53-
job: self,
54-
adapter: self.class.queue_adapter,
55-
error: error
56-
}
57-
5852
if block_given?
59-
ActiveSupport::Notifications.instrument("retry_stopped.active_job", payload) do
53+
instrument :retry_stopped, error: error do
6054
yield self, error
6155
end
6256
else
63-
ActiveSupport::Notifications.instrument("retry_stopped.active_job", payload)
57+
instrument :retry_stopped, error: error
6458
raise error
6559
end
6660
end
@@ -87,16 +81,8 @@ def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: ni
8781
# end
8882
def discard_on(*exceptions)
8983
rescue_from(*exceptions) do |error|
90-
payload = {
91-
job: self,
92-
adapter: self.class.queue_adapter,
93-
error: error
94-
}
95-
96-
ActiveSupport::Notifications.instrument("discard.active_job", payload) do
97-
if block_given?
98-
yield self, error
99-
end
84+
instrument :discard, error: error do
85+
yield self, error if block_given?
10086
end
10187
end
10288
end
@@ -124,14 +110,7 @@ def discard_on(*exceptions)
124110
# end
125111
# end
126112
def retry_job(options = {})
127-
payload = {
128-
job: self,
129-
adapter: self.class.queue_adapter,
130-
error: options[:error],
131-
wait: options[:wait]
132-
}
133-
134-
ActiveSupport::Notifications.instrument("enqueue_retry.active_job", payload) do
113+
instrument :enqueue_retry, options.slice(:error, :wait) do
135114
enqueue options
136115
end
137116
end
@@ -154,5 +133,11 @@ def determine_delay(seconds_or_duration_or_algorithm)
154133
raise "Couldn't determine a delay based on #{seconds_or_duration_or_algorithm.inspect}"
155134
end
156135
end
136+
137+
def instrument(name, error: nil, wait: nil, &block)
138+
payload = { job: self, adapter: self.class.queue_adapter, error: error, wait: wait }
139+
140+
ActiveSupport::Notifications.instrument("#{name}.active_job", payload, &block)
141+
end
157142
end
158143
end

0 commit comments

Comments
 (0)