From 26dc9bc8ee8639b1ad393f4f3f4fa3e1a397b70e Mon Sep 17 00:00:00 2001 From: Steve S Date: Wed, 29 Aug 2018 13:28:55 -0400 Subject: Add hooks to ActiveJob around retries and discards --- activejob/CHANGELOG.md | 4 ++++ activejob/lib/active_job/exceptions.rb | 32 +++++++++++++++++++------ guides/source/active_support_instrumentation.md | 24 +++++++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md index 2417ea3a87..c47465cb43 100644 --- a/activejob/CHANGELOG.md +++ b/activejob/CHANGELOG.md @@ -1,3 +1,7 @@ +* Added `enqueue_retry.active_job`, `retry_stopped.active_job`, and `discard.active_job` hooks. + + *steves* + * Allow `assert_performed_with` to be called without a block. *bogdanvlviv* diff --git a/activejob/lib/active_job/exceptions.rb b/activejob/lib/active_job/exceptions.rb index 1e57dbcb1c..594870c031 100644 --- a/activejob/lib/active_job/exceptions.rb +++ b/activejob/lib/active_job/exceptions.rb @@ -44,12 +44,22 @@ module ActiveJob # end def retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil) rescue_from(*exceptions) do |error| + payload = { + job: self, + adapter: self.class.queue_adapter, + error: error, + } + if executions < attempts - logger.error "Retrying #{self.class} in #{wait} seconds, due to a #{error.class}. The original exception was #{error.cause.inspect}." - retry_job wait: determine_delay(wait), queue: queue, priority: priority + ActiveSupport::Notifications.instrument("enqueue_retry.active_job", payload) do + logger.error "Retrying #{self.class} in #{wait} seconds, due to a #{error.class}. The original exception was #{error.cause.inspect}." + retry_job wait: determine_delay(wait), queue: queue, priority: priority + end else if block_given? - yield self, error + ActiveSupport::Notifications.instrument("retry_stopped.active_job", payload) do + yield self, error + end else logger.error "Stopped retrying #{self.class} due to a #{error.class}, which reoccurred on #{executions} attempts. The original exception was #{error.cause.inspect}." raise error @@ -78,10 +88,18 @@ module ActiveJob # end def discard_on(*exceptions) rescue_from(*exceptions) do |error| - if block_given? - yield self, error - else - logger.error "Discarded #{self.class} due to a #{error.class}. The original exception was #{error.cause.inspect}." + payload = { + job: self, + adapter: self.class.queue_adapter, + error: error, + } + + ActiveSupport::Notifications.instrument("discard.active_job", payload) do + if block_given? + yield self, error + else + logger.error "Discarded #{self.class} due to a #{error.class}. The original exception was #{error.cause.inspect}." + end end end end diff --git a/guides/source/active_support_instrumentation.md b/guides/source/active_support_instrumentation.md index 3568c47dd8..8581817d71 100644 --- a/guides/source/active_support_instrumentation.md +++ b/guides/source/active_support_instrumentation.md @@ -458,6 +458,14 @@ Active Job | `:adapter` | QueueAdapter object processing the job | | `:job` | Job object | +### enqueue_retry.active_job + +| Key | Value | +| ------------ | -------------------------------------- | +| `:job` | Job object | +| `:adapter` | QueueAdapter object processing the job | +| `:error` | The error that caused the retry | + ### perform_start.active_job | Key | Value | @@ -472,6 +480,22 @@ Active Job | `:adapter` | QueueAdapter object processing the job | | `:job` | Job object | +### retry_stopped.active_job + +| Key | Value | +| ------------ | -------------------------------------- | +| `:adapter` | QueueAdapter object processing the job | +| `:job` | Job object | +| `:error` | The error that caused the retry | + +### discard.active_job + +| Key | Value | +| ------------ | -------------------------------------- | +| `:adapter` | QueueAdapter object processing the job | +| `:job` | Job object | +| `:error` | The error that caused the discard | + Action Cable ------------ -- cgit v1.2.3