From 8fbf2e197f0a327ab95bc85b3d3d683e68e09cf3 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 3 May 2012 00:15:50 -0300 Subject: [PATCH] Allow overriding exception handling in threaded consumer --- railties/lib/rails/queueing.rb | 6 +++++- .../test/queueing/threaded_consumer_test.rb | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/railties/lib/rails/queueing.rb b/railties/lib/rails/queueing.rb index 2e187b8555..896dd91034 100644 --- a/railties/lib/rails/queueing.rb +++ b/railties/lib/rails/queueing.rb @@ -53,7 +53,7 @@ module Rails begin job.run rescue Exception => e - Rails.logger.error "Job Error: #{e.message}\n#{e.backtrace.join("\n")}" + handle_exception e end end end @@ -64,6 +64,10 @@ module Rails @queue.push nil @thread.join end + + def handle_exception(e) + Rails.logger.error "Job Error: #{e.message}\n#{e.backtrace.join("\n")}" + end end end end diff --git a/railties/test/queueing/threaded_consumer_test.rb b/railties/test/queueing/threaded_consumer_test.rb index 559de2a82d..c34a966d6e 100644 --- a/railties/test/queueing/threaded_consumer_test.rb +++ b/railties/test/queueing/threaded_consumer_test.rb @@ -78,4 +78,23 @@ class TestThreadConsumer < ActiveSupport::TestCase assert_equal 1, logger.logged(:error).size assert_match(/Job Error: RuntimeError: Error!/, logger.logged(:error).last) end + + test "test overriding exception handling" do + @consumer.shutdown + @consumer = Class.new(Rails::Queueing::ThreadedConsumer) do + attr_reader :last_error + def handle_exception(e) + @last_error = e.message + end + end.start(@queue) + + job = Job.new(1) do + raise "RuntimeError: Error!" + end + + @queue.push job + sleep 0.1 + + assert_equal "RuntimeError: Error!", @consumer.last_error + end end