From b3c946d26166a2f73f5b19366aec65c9f6ce9fb8 Mon Sep 17 00:00:00 2001 From: Chirag Shah Date: Mon, 29 Jan 2024 22:20:16 +0530 Subject: [PATCH 1/3] Add section for exception handling --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/README.md b/README.md index db03e4e9..a1e5c195 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,9 @@ There are several settings that control how Solid Queue works that you can set a ```ruby -> (exception) { Rails.error.report(exception, handled: false) } ``` + + Note: `on_thread_error` is intended for errors in the thread that is executing the job and not for errors encountered in the job. For errors in the job itself, [refer here](#exceptions) + - `connects_to`: a custom database configuration that will be used in the abstract `SolidQueue::Record` Active Record model. This is required to use a different database than the main app. For example: ```ruby @@ -230,6 +233,42 @@ failed_execution.discard # This will delete the job from the system We're planning to release a dashboard called _Mission Control_, where, among other things, you'll be able to examine and retry/discard failed jobs, one by one, or in bulk. +### Exceptions + +For errors encountered in the job, you could try to hook into [Active Job](https://guides.rubyonrails.org/active_job_basics.html#exceptions) and report the errors to your exception monitoring service. + +Let's see an example implementation to handle exceptions. + +```ruby +# application_job.rb +class ApplicationJob < ActiveJob::Base + around_perform do |job, block| + capture_and_record_errors(job, block) + end + + def capture_and_record_errors(job, block) + block.call + rescue => exception + Rails.error.report(exception) + raise exception + end +end +``` + +Note that, you will have to duplicate the above logic on `ActionMailer::MailDeliveryJob` too. That is because `ActionMailer` doesn't inherit from `ApplicationJob` but instead uses `ActionMailer::MailDeliveryJob` which inherits from `ActiveJob::Base`. + +```ruby +# application_mailer.rb + +class ApplicationMailer < ActionMailer::Base + ActionMailer::MailDeliveryJob.around_perform do |job, block| + block.call + rescue => exception + Rails.error.report(exception) + raise exception + end +end +``` ## Puma plugin We provide a Puma plugin if you want to run the Solid Queue's supervisor together with Puma and have Puma monitor and manage it. You just need to add From 98ca9a851b8c2d45297cd7bc3ebb115d782fd1f5 Mon Sep 17 00:00:00 2001 From: Chirag Shah Date: Wed, 7 Feb 2024 01:44:59 +0530 Subject: [PATCH 2/3] used rescue_from instead of around_perform --- README.md | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a1e5c195..252e7639 100644 --- a/README.md +++ b/README.md @@ -242,15 +242,8 @@ Let's see an example implementation to handle exceptions. ```ruby # application_job.rb class ApplicationJob < ActiveJob::Base - around_perform do |job, block| - capture_and_record_errors(job, block) - end - - def capture_and_record_errors(job, block) - block.call - rescue => exception + rescue_from(Exception) do |exception| Rails.error.report(exception) - raise exception end end ``` @@ -261,11 +254,8 @@ Note that, you will have to duplicate the above logic on `ActionMailer::MailDeli # application_mailer.rb class ApplicationMailer < ActionMailer::Base - ActionMailer::MailDeliveryJob.around_perform do |job, block| - block.call - rescue => exception + ActionMailer::MailDeliveryJob.rescue_from(Exception) do |exception| Rails.error.report(exception) - raise exception end end ``` From 7be043406ad6e927b8db7a4a177cd552d601cb9c Mon Sep 17 00:00:00 2001 From: Chirag Shah Date: Wed, 14 Feb 2024 13:14:27 +0530 Subject: [PATCH 3/3] Update README.md to raise exception after rescue --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 252e7639..ffe77f1b 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,7 @@ Let's see an example implementation to handle exceptions. class ApplicationJob < ActiveJob::Base rescue_from(Exception) do |exception| Rails.error.report(exception) + raise exception end end ``` @@ -256,6 +257,7 @@ Note that, you will have to duplicate the above logic on `ActionMailer::MailDeli class ApplicationMailer < ActionMailer::Base ActionMailer::MailDeliveryJob.rescue_from(Exception) do |exception| Rails.error.report(exception) + raise exception end end ```