Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Hack for retry status

Harold.Luo edited this page Sep 1, 2013 · 1 revision

add file: lib/sidekiq-status/retry_status_middleware.rb

require 'sidekiq-status'
require 'sidekiq'

# NOTE: extend sidekiq-status gem
module Sidekiq::Status
  class RetryStatusMiddleware
    include Storage

    # Parameterized initialization, use it when adding middleware to server chain
    # chain.add Sidekiq::Status::ServerMiddleware, :expiration => 60 * 5
    # @param [Hash] opts middleware initialization options
    # @option opts [Fixnum] :expiration ttl for complete jobs
    def initialize(opts = {})
      @expiration = opts[:expiration]
    end

    def call(worker, msg, queue)
      # a way of overriding default expiration time,
      # so worker wouldn't lose its data
      # worker.expiration = @expiration  if worker.respond_to? :expiration
      yield
    rescue
      if msg['retry']
        default_max = Sidekiq::Middleware::Server::RetryJobs::DEFAULT_MAX_RETRY_ATTEMPTS
        retry_count = msg['retry_count'] || 0
        if retry_count < retry_attempts_from(msg['retry'], default_max)
          store_status worker.jid, :retring, @expiration
        end
      end
      raise
    end

    def retry_attempts_from(msg_retry, default)
      msg_retry.is_a?(Fixnum) ? msg_retry : default
    end

  end

  # monkey path for status method
  class << self
    def retring?(job_id)
      status(job_id) == :retring
    end
  end
end

And then, you should add this middleware before sidekiq's retry job:

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add Sidekiq::Status::ServerMiddleware, expiration: 7.days
    # NOTE: while thowning exception, the first one execute later. wired!
    chain.insert_before Sidekiq::Middleware::Server::RetryJobs, Sidekiq::Status::RetryStatusMiddleware, expiration: 7.days
  end
end

now, You get the retring status.

Hope this help. :)

Clone this wiki locally