Class: Sidekiq::Middleware::Server::RetryJobs
- Inherits:
-
Object
- Object
- Sidekiq::Middleware::Server::RetryJobs
- Includes:
- Util
- Defined in:
- lib/sidekiq/middleware/server/retry_jobs.rb
Overview
Automatically retry jobs that fail in Sidekiq. Sidekiq’s retry support assumes a typical development lifecycle:
0. Push some code changes with a bug in it.
1. Bug causes job processing to fail, Sidekiq's middleware captures
the job and pushes it onto a retry queue.
2. Sidekiq retries jobs in the retry queue multiple times with
an exponential delay, the job continues to fail.
3. After a few days, a developer deploys a fix. The job is
reprocessed successfully.
4. Once retries are exhausted, Sidekiq will give up and move the
job to the Dead Job Queue (aka morgue) where it must be dealt with
manually in the Web UI.
5. After 6 months on the DJQ, Sidekiq will discard the job.
A job looks like:
{ 'class' => 'HardWorker', 'args' => [1, 2, 'foo'], 'retry' => true }
The ‘retry’ option also accepts a number (in place of ‘true’):
{ 'class' => 'HardWorker', 'args' => [1, 2, 'foo'], 'retry' => 5 }
The job will be retried this number of times before giving up. (If simply ‘true’, Sidekiq retries 25 times)
We’ll add a bit more data to the job to support retries:
* 'queue' - the queue to use
* 'retry_count' - number of times we've retried so far.
* 'error_message' - the message from the exception
* 'error_class' - the exception class
* 'failed_at' - the first time it failed
* 'retried_at' - the last time it was retried
* 'backtrace' - the number of lines of error backtrace to store
We don’t store the backtrace by default as that can add a lot of overhead to the job and everyone is using an error service, right?
The default number of retry attempts is 25 which works out to about 3 weeks of retries. You can pass a value for the max number of retry attempts when adding the middleware using the options hash:
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add Sidekiq::Middleware::Server::RetryJobs, :max_retries => 7
end
end
or limit the number of retries for a particular worker with:
class MyWorker
include Sidekiq::Worker
:retry => 10
end
Constant Summary collapse
- DEFAULT_MAX_RETRY_ATTEMPTS =
25
Constants included from Util
Instance Method Summary collapse
- #call(worker, msg, queue) ⇒ Object
-
#initialize(options = {}) ⇒ RetryJobs
constructor
A new instance of RetryJobs.
Methods included from Util
#fire_event, #hostname, #identity, #logger, #process_nonce, #redis, #safe_thread, #watchdog
Methods included from ExceptionHandler
Constructor Details
#initialize(options = {}) ⇒ RetryJobs
Returns a new instance of RetryJobs.
69 70 71 |
# File 'lib/sidekiq/middleware/server/retry_jobs.rb', line 69 def initialize( = {}) @max_retries = .fetch(:max_retries, DEFAULT_MAX_RETRY_ATTEMPTS) end |
Instance Method Details
#call(worker, msg, queue) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sidekiq/middleware/server/retry_jobs.rb', line 73 def call(worker, msg, queue) yield rescue Sidekiq::Shutdown # ignore, will be pushed back onto queue during hard_shutdown raise rescue Exception => e # ignore, will be pushed back onto queue during hard_shutdown raise Sidekiq::Shutdown if exception_caused_by_shutdown?(e) raise e unless msg['retry'] attempt_retry(worker, msg, queue, e) end |