-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setup custom exception handlers for failing jobs (#742)
* Allow setup custom exception handlers for failing jobs * Allow setup custom exception handlers for failing jobs * Fix specs --------- Co-authored-by: Pablo Cantero <pablohstc@gmail.com>
- Loading branch information
Showing
6 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Shoryuken | ||
class DefaultExceptionHandler | ||
extend Util | ||
|
||
def self.call(exception, _queue, _sqs_msg) | ||
logger.error { "Processor failed: #{exception.message}" } | ||
logger.error { exception.backtrace.join("\n") } if exception.backtrace | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'spec_helper' | ||
|
||
# rubocop:disable Metrics/BlockLength | ||
RSpec.describe Shoryuken::DefaultExceptionHandler do | ||
class CustomErrorHandler | ||
extend Shoryuken::Util | ||
|
||
def self.call(_ex, queue, _msg) | ||
logger.error("#{queue.to_s} failed to process the message") | ||
end | ||
end | ||
|
||
before do | ||
Shoryuken.worker_executor = Shoryuken::Worker::InlineExecutor | ||
allow(manager).to receive(:async).and_return(manager) | ||
allow(manager).to receive(:real_thread) | ||
allow(Shoryuken::Client).to receive(:queues).with(queue).and_return(sqs_queue) | ||
end | ||
|
||
after do | ||
Shoryuken.worker_executor = Shoryuken::Worker::DefaultExecutor | ||
end | ||
|
||
let(:manager) { double Shoryuken::Manager } | ||
let(:sqs_queue) { double Shoryuken::Queue, visibility_timeout: 30 } | ||
let(:queue) { 'default' } | ||
|
||
let(:sqs_msg) do | ||
double( | ||
Shoryuken::Message, | ||
queue_url: queue, | ||
body: 'test', | ||
message_attributes: {}, | ||
message_id: SecureRandom.uuid, | ||
receipt_handle: SecureRandom.uuid | ||
) | ||
end | ||
|
||
subject { Shoryuken::Processor.new(queue, sqs_msg) } | ||
|
||
context "with default handler" do | ||
before do | ||
Shoryuken.exception_handlers = described_class | ||
end | ||
|
||
it "logs an error message" do | ||
expect(Shoryuken::Logging.logger).to receive(:error).twice | ||
|
||
allow_any_instance_of(TestWorker).to receive(:perform).and_raise(StandardError, "error") | ||
allow(sqs_msg).to receive(:body) | ||
|
||
expect { subject.process }.to raise_error(StandardError) | ||
end | ||
end | ||
|
||
context "with custom handler" do | ||
before do | ||
Shoryuken.exception_handlers = [described_class, CustomErrorHandler] | ||
end | ||
|
||
it "logs default and custom error messages" do | ||
expect(Shoryuken::Logging.logger).to receive(:error).twice | ||
expect(Shoryuken::Logging.logger).to receive(:error).with("default failed to process the message").once | ||
|
||
allow_any_instance_of(TestWorker).to receive(:perform).and_raise(StandardError, "error") | ||
allow(sqs_msg).to receive(:body) | ||
|
||
expect { subject.process }.to raise_error(StandardError) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters