FEATURE: Discoruse.handle_exception

to report exception via sidekiq helper, adds extra context
This commit is contained in:
Sam Saffron 2014-02-21 14:30:25 +11:00
parent d95887c57d
commit 2ab76f60d1
4 changed files with 39 additions and 6 deletions

View file

@ -35,7 +35,7 @@ if Sidekiq.server?
manager.tick manager.tick
rescue => e rescue => e
# the show must go on # the show must go on
Scheduler::Manager.handle_exception(e) Discourse.handle_exception(e)
end end
sleep 1 sleep 1
end end

View file

@ -4,6 +4,21 @@ require_dependency 'auth/default_current_user_provider'
module Discourse 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 # Expected less matches than what we got in a find
class TooManyMatches < Exception; end class TooManyMatches < Exception; end

View file

@ -6,7 +6,6 @@
module Scheduler module Scheduler
class Manager class Manager
extend Sidekiq::ExceptionHandler
attr_accessor :random_ratio, :redis attr_accessor :random_ratio, :redis
@ -41,13 +40,13 @@ module Scheduler
def keep_alive def keep_alive
@manager.keep_alive @manager.keep_alive
rescue => ex rescue => ex
Scheduler::Manager.handle_exception(ex) Discourse.handle_exception(ex)
end end
def reschedule_orphans def reschedule_orphans
@manager.reschedule_orphans! @manager.reschedule_orphans!
rescue => ex rescue => ex
Scheduler::Manager.handle_exception(ex) Discourse.handle_exception(ex)
end end
def process_queue def process_queue
@ -62,7 +61,7 @@ module Scheduler
@mutex.synchronize { info.write! } @mutex.synchronize { info.write! }
klass.new.perform klass.new.perform
rescue => e rescue => e
Scheduler::Manager.handle_exception(e) Discourse.handle_exception(e)
failed = true failed = true
end end
duration = ((Time.now.to_f - start) * 1000).to_i duration = ((Time.now.to_f - start) * 1000).to_i
@ -73,7 +72,7 @@ module Scheduler
@mutex.synchronize { info.write! } @mutex.synchronize { info.write! }
end end
rescue => ex rescue => ex
Scheduler::Manager.handle_exception(ex) Discourse.handle_exception(ex)
ensure ensure
@running = false @running = false
end end

View file

@ -116,5 +116,24 @@ describe Discourse do
end 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 end