diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2018-09-23 20:43:48 +0200 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2018-09-23 20:44:42 +0200 |
commit | baffadaec9200d6653089f4937bfe53f1c0e127c (patch) | |
tree | 8d72dbeb9cbdddb08e3c8648825fee303f525c6e | |
parent | 609b1ec9b629e60416eb32a4376a054ca0206d66 (diff) | |
download | rails-baffadaec9200d6653089f4937bfe53f1c0e127c.tar.gz rails-baffadaec9200d6653089f4937bfe53f1c0e127c.tar.bz2 rails-baffadaec9200d6653089f4937bfe53f1c0e127c.zip |
Extract `instrument` method.
Similar to Action View's and Action Controller's instrument helpers.
-rw-r--r-- | activejob/lib/active_job/exceptions.rb | 37 |
1 files changed, 11 insertions, 26 deletions
diff --git a/activejob/lib/active_job/exceptions.rb b/activejob/lib/active_job/exceptions.rb index bb25afbca6..bc9e168971 100644 --- a/activejob/lib/active_job/exceptions.rb +++ b/activejob/lib/active_job/exceptions.rb @@ -49,18 +49,12 @@ module ActiveJob 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 @@ -87,16 +81,8 @@ module ActiveJob # 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 @@ -124,14 +110,7 @@ module ActiveJob # 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 @@ -154,5 +133,11 @@ module ActiveJob 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 |