class Sidekiq::Config

Sidekiq::Config represents the global configuration for an instance of Sidekiq.

Constants

DEFAULTS
ERROR_HANDLER

Attributes

capsules[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/sidekiq/config.rb, line 53
def initialize(options = {})
  @options = DEFAULTS.merge(options)
  @options[:error_handlers] << ERROR_HANDLER if @options[:error_handlers].empty?
  @directory = {}
  @redis_config = {}
  @capsules = {}
end

Public Instance Methods

average_scheduled_poll_interval=(interval) click to toggle source

How frequently Redis should be checked by a random Sidekiq process for scheduled and retriable jobs. Each individual process will take turns by waiting some multiple of this value.

See sidekiq/scheduled.rb for an in-depth explanation of this value

# File lib/sidekiq/config.rb, line 230
def average_scheduled_poll_interval=(interval)
  @options[:average_scheduled_poll_interval] = interval
end
capsule(name) { |cap| ... } click to toggle source

register a new queue processing subsystem

# File lib/sidekiq/config.rb, line 117
def capsule(name)
  nm = name.to_s
  cap = @capsules.fetch(nm) do
    cap = Sidekiq::Capsule.new(nm, self)
    @capsules[nm] = cap
  end
  yield cap if block_given?
  cap
end
client_middleware() { |client_chain| ... } click to toggle source
# File lib/sidekiq/config.rb, line 100
def client_middleware
  @client_chain ||= Sidekiq::Middleware::Chain.new(self)
  yield @client_chain if block_given?
  @client_chain
end
concurrency() click to toggle source
# File lib/sidekiq/config.rb, line 74
def concurrency
  default_capsule.concurrency
end
concurrency=(val) click to toggle source

LEGACY: edits the default capsule config.concurrency = 5

# File lib/sidekiq/config.rb, line 70
def concurrency=(val)
  default_capsule.concurrency = Integer(val)
end
death_handlers() click to toggle source

Death handlers are called when all retries for a job have been exhausted and the job dies. It’s the notification to your application that this job will not succeed without manual intervention.

Sidekiq.configure_server do |config|

config.death_handlers << ->(job, ex) do
end

end

# File lib/sidekiq/config.rb, line 221
def death_handlers
  @options[:death_handlers]
end
default_capsule(&block) click to toggle source
# File lib/sidekiq/config.rb, line 112
def default_capsule(&block)
  capsule("default", &block)
end
error_handlers() click to toggle source

Register a proc to handle any error which occurs within the Sidekiq process.

Sidekiq.configure_server do |config|
  config.error_handlers << proc {|ex,ctx_hash| MyErrorService.notify(ex, ctx_hash) }
end

The default error handler logs errors to @logger.

# File lib/sidekiq/config.rb, line 241
def error_handlers
  @options[:error_handlers]
end
freeze!() click to toggle source
# File lib/sidekiq/config.rb, line 206
def freeze!
  @directory.freeze
  @options.freeze
  true
end
handle_exception(ex, ctx = {}) click to toggle source

INTERNAL USE ONLY

# File lib/sidekiq/config.rb, line 285
def handle_exception(ex, ctx = {})
  if @options[:error_handlers].size == 0
    p ["!!!!!", ex]
  end
  @options[:error_handlers].each do |handler|
    if parameter_size(handler) == 2
      # TODO Remove in 8.0
      logger.info { "DEPRECATION: Sidekiq exception handlers now take three arguments, see #{handler}" }
      handler.call(ex, {_config: self}.merge(ctx))
    else
      handler.call(ex, ctx, self)
    end
  rescue Exception => e
    l = logger
    l.error "!!! ERROR HANDLER THREW AN ERROR !!!"
    l.error e
    l.error e.backtrace.join("\n") unless e.backtrace.nil?
  end
end
logger() click to toggle source
# File lib/sidekiq/config.rb, line 259
def logger
  @logger ||= Sidekiq::Logger.new($stdout, level: :info).tap do |log|
    log.level = Logger::INFO
    log.formatter = if ENV["DYNO"]
      Sidekiq::Logger::Formatters::WithoutTimestamp.new
    else
      Sidekiq::Logger::Formatters::Pretty.new
    end
  end
end
logger=(logger) click to toggle source
# File lib/sidekiq/config.rb, line 270
def logger=(logger)
  if logger.nil?
    self.logger.level = Logger::FATAL
    return
  end

  @logger = logger
end
lookup(name, default_class = nil) click to toggle source

find a singleton

# File lib/sidekiq/config.rb, line 198
def lookup(name, default_class = nil)
  # JNDI is just a fancy name for a hash lookup
  @directory.fetch(name) do |key|
    return nil unless default_class
    register(key, default_class.new(self))
  end
end
new_redis_pool(size, name = "unset") click to toggle source
# File lib/sidekiq/config.rb, line 142
def new_redis_pool(size, name = "unset")
  # connection pool is lazy, it will not create connections unless you actually need them
  # so don't be skimpy!
  RedisConnection.create({size: size, logger: logger, pool_name: name}.merge(@redis_config))
end
on(event, &block) click to toggle source

Register a block to run at a point in the Sidekiq lifecycle. :startup, :quiet or :shutdown are valid events.

Sidekiq.configure_server do |config|
  config.on(:shutdown) do
    puts "Goodbye cruel world!"
  end
end
# File lib/sidekiq/config.rb, line 253
def on(event, &block)
  raise ArgumentError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
  raise ArgumentError, "Invalid event name: #{event}" unless @options[:lifecycle_events].key?(event)
  @options[:lifecycle_events][event] << block
end
queues() click to toggle source
# File lib/sidekiq/config.rb, line 96
def queues
  default_capsule.queues
end
queues=(val) click to toggle source

Edit the default capsule. config.queues = %w( high default low ) # strict config.queues = %w( high,3 default,2 low,1 ) # weighted config.queues = %w( feature1,1 feature2,1 feature3,1 ) # random

With weighted priority, queue will be checked first (weight / total) of the time. high will be checked first (3/6) or 50% of the time. I’d recommend setting weights between 1-10. Weights in the hundreds or thousands are ridiculous and unnecessarily expensive. You can get random queue ordering by explicitly setting all weights to 1.

# File lib/sidekiq/config.rb, line 92
def queues=(val)
  default_capsule.queues = val
end
redis() { |conn| ... } click to toggle source
# File lib/sidekiq/config.rb, line 164
def redis
  raise ArgumentError, "requires a block" unless block_given?
  redis_pool.with do |conn|
    retryable = true
    begin
      yield conn
    rescue RedisClientAdapter::BaseError => ex
      # 2550 Failover can cause the server to become a replica, need
      # to disconnect and reopen the socket to get back to the primary.
      # 4495 Use the same logic if we have a "Not enough replicas" error from the primary
      # 4985 Use the same logic when a blocking command is force-unblocked
      # The same retry logic is also used in client.rb
      if retryable && ex.message =~ /READONLY|NOREPLICAS|UNBLOCKED/
        conn.close
        retryable = false
        retry
      end
      raise
    end
  end
end
redis=(hash) click to toggle source

All capsules must use the same Redis configuration

# File lib/sidekiq/config.rb, line 128
def redis=(hash)
  @redis_config = @redis_config.merge(hash)
end
redis_info() click to toggle source
# File lib/sidekiq/config.rb, line 148
def redis_info
  redis do |conn|
    conn.call("INFO") { |i| i.lines(chomp: true).map { |l| l.split(":", 2) }.select { |l| l.size == 2 }.to_h }
  rescue RedisClientAdapter::CommandError => ex
    # 2850 return fake version when INFO command has (probably) been renamed
    raise unless /unknown command/.match?(ex.message)
    {
      "redis_version" => "9.9.9",
      "uptime_in_days" => "9999",
      "connected_clients" => "9999",
      "used_memory_human" => "9P",
      "used_memory_peak_human" => "9P"
    }.freeze
  end
end
redis_pool() click to toggle source
# File lib/sidekiq/config.rb, line 132
def redis_pool
  Thread.current[:sidekiq_redis_pool] || Thread.current[:sidekiq_capsule]&.redis_pool || local_redis_pool
end
register(name, instance) click to toggle source

register global singletons which can be accessed elsewhere

# File lib/sidekiq/config.rb, line 187
def register(name, instance)
  # logger.debug("register[#{name}] = #{instance}")
  # Sidekiq Enterprise lazy registers a few services so we
  # can't lock down this hash completely.
  hash = @directory.dup
  hash[name] = instance
  @directory = hash.freeze
  instance
end
server_middleware() { |server_chain| ... } click to toggle source
# File lib/sidekiq/config.rb, line 106
def server_middleware
  @server_chain ||= Sidekiq::Middleware::Chain.new(self)
  yield @server_chain if block_given?
  @server_chain
end
to_json(*) click to toggle source
# File lib/sidekiq/config.rb, line 64
def to_json(*)
  Sidekiq.dump_json(@options)
end
total_concurrency() click to toggle source
# File lib/sidekiq/config.rb, line 78
def total_concurrency
  capsules.each_value.sum(&:concurrency)
end

Private Instance Methods

local_redis_pool() click to toggle source
# File lib/sidekiq/config.rb, line 136
        def local_redis_pool
  # this is our internal client/housekeeping pool. each capsule has its
  # own pool for executing threads.
  @redis ||= new_redis_pool(10, "internal")
end
parameter_size(handler) click to toggle source
# File lib/sidekiq/config.rb, line 279
        def parameter_size(handler)
  target = handler.is_a?(Proc) ? handler : handler.method(:call)
  target.parameters.size
end