Permalink
Browse files

Merge pull request #15 from kbaum/master

added failures default mode
  • Loading branch information...
2 parents 6b2724d + 88e55d5 commit 121f82ad7db9ee28bd7e68c5bdc9e27bcc4bd224 @mhfs committed Dec 24, 2012
Showing with 69 additions and 1 deletion.
  1. +1 −0 lib/sidekiq-failures.rb
  2. +15 −0 lib/sidekiq/failures.rb
  3. +9 −1 lib/sidekiq/failures/middleware.rb
  4. +44 −0 test/middleware_test.rb
View
@@ -1 +1,2 @@
require "sidekiq/failures"
+
View
@@ -4,6 +4,21 @@
require "sidekiq/failures/web_extension"
module Sidekiq
+
+ SIDEKIQ_FAILURES_MODES = ['all', 'exhausted'].freeze
+
+ def self.failures_default_mode=(mode)
+ unless SIDEKIQ_FAILURES_MODES.include?(mode.to_s)
+ raise ArgumentError, "Sidekiq#failures_default_mode valid options: #{SIDEKIQ_FAILURES_MODES}"
+ end
+ @failures_default_mode = mode
+ end
+
+ def self.failures_default_mode
+ @failures_default_mode || 'all'
+ end
+
+
module Failures
end
end
@@ -31,7 +31,15 @@ def skip_failure?
end
def not_exhausted?
- msg['failures'] == 'exhausted' && !last_try?
+ exhausted_mode? && !last_try?
+ end
+
+ def exhausted_mode?
+ if msg['failures']
+ msg['failures'] == 'exhausted'
+ else
+ Sidekiq.failures_default_mode.to_s == 'exhausted'
+ end
end
def last_try?
View
@@ -10,6 +10,7 @@ module Failures
Sidekiq.server_middleware {|chain| chain.add Sidekiq::Failures::Middleware }
Sidekiq.redis = REDIS
Sidekiq.redis { |c| c.flushdb }
+ Sidekiq.instance_eval { @failures_default_mode = nil }
end
TestException = Class.new(StandardError)
@@ -23,6 +24,16 @@ def perform(args)
end
end
+ it 'raises an error when failures_default_mode is configured incorrectly' do
+ assert_raises ArgumentError do
+ Sidekiq.failures_default_mode = 'exhaustion'
+ end
+ end
+
+ it 'defaults failures_default_mode to all' do
+ assert_equal 'all', Sidekiq.failures_default_mode
+ end
+
it 'records all failures by default' do
msg = create_message('class' => MockWorker.to_s, 'args' => ['myarg'])
@@ -62,6 +73,22 @@ def perform(args)
assert_equal 1, $invokes
end
+ it "doesn't record failure if going to be retired again and configured to track exhaustion by default" do
+ Sidekiq.failures_default_mode = :exhausted
+
+ msg = create_message('class' => MockWorker.to_s, 'args' => ['myarg'] )
+
+ assert_equal 0, failures_count
+
+ assert_raises TestException do
+ @processor.process(msg, 'default')
+ end
+
+ assert_equal 0, failures_count
+ assert_equal 1, $invokes
+ end
+
+
it "doesn't record failure if going to be retired again and configured to track exhaustion" do
msg = create_message('class' => MockWorker.to_s, 'args' => ['myarg'], 'failures' => 'exhausted')
@@ -88,6 +115,23 @@ def perform(args)
assert_equal 1, $invokes
end
+ it "records failure if failing last retry and configured to track exhaustion by default" do
+ Sidekiq.failures_default_mode = 'exhausted'
+
+ msg = create_message('class' => MockWorker.to_s, 'args' => ['myarg'], 'retry_count' => 24)
+
+ assert_equal 0, failures_count
+
+ assert_raises TestException do
+ @processor.process(msg, 'default')
+ end
+
+ assert_equal 1, failures_count
+ assert_equal 1, $invokes
+ end
+
+
+
def failures_count
Sidekiq.redis { |conn|conn.llen('failed') } || 0
end

0 comments on commit 121f82a

Please sign in to comment.