mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Allow for default number of retry attempts to be set for RetryJobs middleware.
* Adds `max_retries` option to RetryJobs middleware. Default is still 25. This way you can still retry jobs, but not have it spread out over such a long period. Modify `Sidekiq::Middleware::Chain#add` semantics * If middleware class already exists in the chain, remove the existing class and add it with possibly new arguments.
This commit is contained in:
parent
7689b0c1b8
commit
3f0d7e32a4
4 changed files with 43 additions and 4 deletions
|
@ -78,7 +78,8 @@ module Sidekiq
|
|||
end
|
||||
|
||||
def add(klass, *args)
|
||||
entries << Entry.new(klass, *args) unless exists?(klass)
|
||||
remove(klass) if exists?(klass)
|
||||
entries << Entry.new(klass, *args)
|
||||
end
|
||||
|
||||
def insert_before(oldklass, newklass, *args)
|
||||
|
@ -122,6 +123,7 @@ module Sidekiq
|
|||
|
||||
class Entry
|
||||
attr_reader :klass
|
||||
|
||||
def initialize(klass, *args)
|
||||
@klass = klass
|
||||
@args = args
|
||||
|
|
|
@ -40,11 +40,24 @@ module Sidekiq
|
|||
#
|
||||
# We don't store the backtrace as that can add a lot of overhead
|
||||
# to the message and everyone is using Airbrake, right?
|
||||
#
|
||||
# The default number of retry attempts is 25. You can pass a value for the
|
||||
# number of retry attempts when adding the middleware using the options hash:
|
||||
#
|
||||
# Sidekiq.configure_server do |config|
|
||||
# config.server_middleware do |chain|
|
||||
# chain.add Middleware::Server::RetryJobs, {:max_retries => 7}
|
||||
# end
|
||||
# end
|
||||
class RetryJobs
|
||||
include Sidekiq::Util
|
||||
|
||||
DEFAULT_MAX_RETRY_ATTEMPTS = 25
|
||||
|
||||
def initialize(options = {})
|
||||
@max_retries = options.fetch(:max_retries, DEFAULT_MAX_RETRY_ATTEMPTS)
|
||||
end
|
||||
|
||||
def call(worker, msg, queue)
|
||||
yield
|
||||
rescue Sidekiq::Shutdown
|
||||
|
@ -52,7 +65,7 @@ module Sidekiq
|
|||
raise
|
||||
rescue Exception => e
|
||||
raise e unless msg['retry']
|
||||
max_retry_attempts = retry_attempts_from(msg['retry'], DEFAULT_MAX_RETRY_ATTEMPTS)
|
||||
max_retry_attempts = retry_attempts_from(msg['retry'], @max_retries)
|
||||
|
||||
msg['queue'] = if msg['retry_queue']
|
||||
msg['retry_queue']
|
||||
|
|
|
@ -72,7 +72,7 @@ class TestMiddleware < Minitest::Test
|
|||
msg = Sidekiq.dump_json({ 'class' => CustomWorker.to_s, 'args' => [$recorder] })
|
||||
|
||||
Sidekiq.server_middleware do |chain|
|
||||
# should only add once, second should be ignored
|
||||
# should only add once, second should replace the first
|
||||
2.times { |i| chain.add CustomMiddleware, i.to_s, $recorder }
|
||||
chain.insert_before CustomMiddleware, AnotherCustomMiddleware, '2', $recorder
|
||||
chain.insert_after AnotherCustomMiddleware, YetAnotherCustomMiddleware, '3', $recorder
|
||||
|
@ -86,7 +86,14 @@ class TestMiddleware < Minitest::Test
|
|||
boss.expect(:async, actor, [])
|
||||
boss.expect(:async, actor, [])
|
||||
processor.process(Sidekiq::BasicFetch::UnitOfWork.new('queue:default', msg))
|
||||
assert_equal %w(2 before 3 before 0 before work_performed 0 after 3 after 2 after), $recorder.flatten
|
||||
assert_equal %w(2 before 3 before 1 before work_performed 1 after 3 after 2 after), $recorder.flatten
|
||||
end
|
||||
|
||||
it 'correctly replaces middleware when using middleware with options in the initializer' do
|
||||
chain = Sidekiq::Middleware::Chain.new
|
||||
chain.add Sidekiq::Middleware::Server::RetryJobs
|
||||
chain.add Sidekiq::Middleware::Server::RetryJobs, {:max_retries => 5}
|
||||
assert_equal 1, chain.count
|
||||
end
|
||||
|
||||
it 'allows middleware to abruptly stop processing rest of chain' do
|
||||
|
|
|
@ -45,6 +45,23 @@ class TestRetry < Minitest::Test
|
|||
@redis.verify
|
||||
end
|
||||
|
||||
it 'allows a max_retries option in initializer' do
|
||||
max_retries = 7
|
||||
1.upto(max_retries) do
|
||||
@redis.expect :zadd, 1, ['retry', String, String]
|
||||
end
|
||||
msg = { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => true }
|
||||
handler = Sidekiq::Middleware::Server::RetryJobs.new({:max_retries => max_retries})
|
||||
1.upto(max_retries + 1) do
|
||||
assert_raises RuntimeError do
|
||||
handler.call(worker, msg, 'default') do
|
||||
raise "kerblammo!"
|
||||
end
|
||||
end
|
||||
end
|
||||
@redis.verify
|
||||
end
|
||||
|
||||
it 'saves backtraces' do
|
||||
@redis.expect :zadd, 1, ['retry', String, String]
|
||||
msg = { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => true, 'backtrace' => true }
|
||||
|
|
Loading…
Add table
Reference in a new issue