diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 4829dd515..9e511e4e5 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -35,7 +35,7 @@ if Sidekiq.server? manager.tick rescue => e # the show must go on - Scheduler::Manager.handle_exception(e) + Discourse.handle_exception(e) end sleep 1 end diff --git a/lib/discourse.rb b/lib/discourse.rb index f757c5b6f..ae49b4da6 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -4,6 +4,21 @@ require_dependency 'auth/default_current_user_provider' module Discourse + class SidekiqExceptionHandler + extend Sidekiq::ExceptionHandler + end + + def self.handle_exception(ex, context=nil, parent_logger = nil) + context ||= {} + parent_logger ||= SidekiqExceptionHandler + + cm = RailsMultisite::ConnectionManagement + parent_logger.handle_exception(ex, { + current_db: cm.current_db, + current_hostname: cm.current_hostname + }.merge(context)) + end + # Expected less matches than what we got in a find class TooManyMatches < Exception; end diff --git a/lib/scheduler/manager.rb b/lib/scheduler/manager.rb index 838c4db9d..6e74d219a 100644 --- a/lib/scheduler/manager.rb +++ b/lib/scheduler/manager.rb @@ -6,7 +6,6 @@ module Scheduler class Manager - extend Sidekiq::ExceptionHandler attr_accessor :random_ratio, :redis @@ -41,13 +40,13 @@ module Scheduler def keep_alive @manager.keep_alive rescue => ex - Scheduler::Manager.handle_exception(ex) + Discourse.handle_exception(ex) end def reschedule_orphans @manager.reschedule_orphans! rescue => ex - Scheduler::Manager.handle_exception(ex) + Discourse.handle_exception(ex) end def process_queue @@ -62,7 +61,7 @@ module Scheduler @mutex.synchronize { info.write! } klass.new.perform rescue => e - Scheduler::Manager.handle_exception(e) + Discourse.handle_exception(e) failed = true end duration = ((Time.now.to_f - start) * 1000).to_i @@ -73,7 +72,7 @@ module Scheduler @mutex.synchronize { info.write! } end rescue => ex - Scheduler::Manager.handle_exception(ex) + Discourse.handle_exception(ex) ensure @running = false end diff --git a/spec/components/discourse_spec.rb b/spec/components/discourse_spec.rb index 402f4535c..c693b4aa5 100644 --- a/spec/components/discourse_spec.rb +++ b/spec/components/discourse_spec.rb @@ -116,5 +116,24 @@ describe Discourse do end + context "#handle_exception" do + class TempLogger + attr_accessor :exception, :context + def handle_exception(exception, context) + self.exception = exception + self.context = context + end + end + + it "should not fail when called" do + logger = TempLogger.new + exception = StandardError.new + + Discourse.handle_exception(exception, nil, logger) + logger.exception.should == exception + logger.context.keys.should == [:current_db, :current_hostname] + end + end + end