class Sidekiq::Status::ServerMiddleware

Should be in the server middleware chain

Constants

DEFAULT_MAX_RETRY_ATTEMPTS

Public Class Methods

new(opts = {}) click to toggle source

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

# File lib/sidekiq-status/server_middleware.rb, line 17
def initialize(opts = {})
  @expiration = opts[:expiration]
end

Public Instance Methods

call(worker, msg, queue) { || ... } click to toggle source

Uses sidekiq’s internal jid as id puts :working status into Redis hash initializes worker instance with id

Exception handler sets :failed status, re-inserts worker and re-throws the exception Worker::Stopped exception type are processed separately - :stopped status is set, no re-throwing

@param [Worker] worker worker instance, processed here if its class includes Status::Worker @param [Array] msg job args, should have jid format @param [String] queue queue name

# File lib/sidekiq-status/server_middleware.rb, line 31
def call(worker, msg, queue)

  # Initial assignment to prevent SystemExit & co. from excepting
  expiry = @expiration

  # Determine the actual job class
  klass = msg["args"][0]["job_class"] || msg["class"] rescue msg["class"]
  job_class = klass.is_a?(Class) ? klass : Module.const_get(klass)

  # Bypass unless this is a Sidekiq::Status::Worker job
  unless job_class.ancestors.include?(Sidekiq::Status::Worker)
    yield
    return
  end

  begin
    # Determine job expiration
    expiry = job_class.new.expiration || @expiration rescue @expiration

    store_status worker.jid, :working,  expiry
    yield
    store_status worker.jid, :complete, expiry
  rescue Worker::Stopped
    store_status worker.jid, :stopped, expiry
  rescue SystemExit, Interrupt
    store_status worker.jid, :interrupted, expiry
    raise
  rescue Exception
    status = :failed
    if msg['retry']
      if retry_attempt_number(msg) < retry_attempts_from(msg['retry'], DEFAULT_MAX_RETRY_ATTEMPTS)
        status = :retrying
      end
    end
    store_status(worker.jid, status, expiry) if job_class && job_class.ancestors.include?(Sidekiq::Status::Worker)
    raise
  end
end

Private Instance Methods

retry_attempt_number(msg) click to toggle source
# File lib/sidekiq-status/server_middleware.rb, line 72
def retry_attempt_number(msg)
  if msg['retry_count']
    msg['retry_count'] + sidekiq_version_dependent_retry_offset
  else
    0
  end
end
retry_attempts_from(msg_retry, default) click to toggle source
# File lib/sidekiq-status/server_middleware.rb, line 80
def retry_attempts_from(msg_retry, default)
  msg_retry.is_a?(Integer) ? msg_retry : default
end
sidekiq_version_dependent_retry_offset() click to toggle source
# File lib/sidekiq-status/server_middleware.rb, line 84
def sidekiq_version_dependent_retry_offset
  Sidekiq.major_version >= 4 ? 1 : 0
end