class SidekiqUniqueJobs::Config

Shared class for dealing with gem configuration

@author Mauro Berlanda <mauro.berlanda@gmail.com> rubocop:disable Metrics/ClassLength

Constants

DEBUG_LUA

@return [false] by default we don't debug the lua scripts because it is slow

ENABLED

@return [true] by default the gem is enabled

LOCKS

@return [Hash<Symbol, SidekiqUniqueJobs::Lock::BaseLock] all available default locks

LOCKS_FROM_PUSH_TO_PROCESSED

@return [Hash<Symbol, SidekiqUniqueJobs::Lock::BaseLock] all available fulltime locks

LOCKS_WHEN_BUSY

@return [Hash<Symbol, SidekiqUniqueJobs::Lock::BaseLock] all available runtime/client locks

LOCKS_WHILE_ENQUEUED

@return [Hash<Symbol, SidekiqUniqueJobs::Lock::BaseLock] all available queued locks

LOCKS_WITHOUT_UNLOCK

@return [Hash<Symbol, SidekiqUniqueJobs::Lock::BaseLock] all available locks without unlock

LOCK_TIMEOUT

@return [0] by default don't wait for locks

LOCK_TTL

@return [nil]

MAX_HISTORY

@return [1_000] use a changelog history of 1_000 entries by default

PREFIX

@return ['uniquejobs'] by default we use this prefix

RAISE_ON_CONFIG_ERROR

@return [false] by default we don't raise validation errors for workers

REAPER

@return [:ruby] prefer the ruby reaper by default since the lua reaper still has problems

REAPER_COUNT

@return [1_000] reap 1_000 orphaned locks at a time by default

REAPER_INTERVAL

@return [600] reap locks every 10 minutes

REAPER_RESURRECTOR_ENABLED

@return [false] enable reaper resurrector

REAPER_RESURRECTOR_INTERVAL

@return [3600] check if reaper is dead each 3600 seconds

REAPER_TIMEOUT

@return [10] stop reaper after 10 seconds

REDIS_VERSION

@return [0.0.0] default redis version is only to avoid NoMethodError on nil

STRATEGIES

@return [Hash<Symbol, SidekiqUniqueJobs::OnConflict::Strategy] all available default strategies

USE_LOCK_INFO

@return [false] while useful it also adds overhead so disable lock_info by default

Public Class Methods

default() click to toggle source

Returns a default configuration

@example

SidekiqUniqueJobs::Config.default => <concurrent/mutable_struct/thread_safe_config SidekiqUniqueJobs::Config {
default_lock_timeout: 0,
default_lock_ttl: nil,
enabled: true,
lock_prefix: "uniquejobs",
logger: #<Sidekiq::Logger:0x00007f81e096b0e0 @level=1 ...>,
locks: {
  around_perform: SidekiqUniqueJobs::Lock::WhileExecuting,
  while_busy: SidekiqUniqueJobs::Lock::WhileExecuting,
  while_executing: SidekiqUniqueJobs::Lock::WhileExecuting,
  while_working: SidekiqUniqueJobs::Lock::WhileExecuting,
  while_executing_reject: SidekiqUniqueJobs::Lock::WhileExecutingReject,
  until_executing: SidekiqUniqueJobs::Lock::UntilExecuting,
  while_enqueued: SidekiqUniqueJobs::Lock::UntilExecuting,
  until_expired: SidekiqUniqueJobs::Lock::UntilExpired,
  until_completed: SidekiqUniqueJobs::Lock::UntilExecuted,
  until_executed: SidekiqUniqueJobs::Lock::UntilExecuted,
  until_performed: SidekiqUniqueJobs::Lock::UntilExecuted,
  until_processed: SidekiqUniqueJobs::Lock::UntilExecuted,
  until_and_while_executing: SidekiqUniqueJobs::Lock::UntilAndWhileExecuting,
  until_successfully_completed: SidekiqUniqueJobs::Lock::UntilExecuted
},
strategies: {
  log: SidekiqUniqueJobs::OnConflict::Log,
  raise: SidekiqUniqueJobs::OnConflict::Raise,
  reject: SidekiqUniqueJobs::OnConflict::Reject,
  replace: SidekiqUniqueJobs::OnConflict::Replace,
  reschedule: SidekiqUniqueJobs::OnConflict::Reschedule
},
debug_lua: false,
max_history: 1000,
reaper:: ruby,
reaper_count: 1000,
lock_info: false,
raise_on_config_error: false,
}>

@return [SidekiqUniqueJobs::Config] a default configuration

# File lib/sidekiq_unique_jobs/config.rb, line 176
def self.default # rubocop:disable Metrics/MethodLength
  new(
    LOCK_TIMEOUT,
    LOCK_TTL,
    ENABLED,
    PREFIX,
    Sidekiq.logger,
    LOCKS,
    STRATEGIES,
    DEBUG_LUA,
    MAX_HISTORY,
    REAPER,
    REAPER_COUNT,
    REAPER_INTERVAL,
    REAPER_TIMEOUT,
    REAPER_RESURRECTOR_INTERVAL,
    REAPER_RESURRECTOR_ENABLED,
    USE_LOCK_INFO,
    RAISE_ON_CONFIG_ERROR,
    REDIS_VERSION,
  )
end

Public Instance Methods

add_lock(name, klass) click to toggle source

Adds a lock type to the configuration. It will raise if the lock exists already

@example Add a custom lock

add_lock(:my_lock, CustomLocks::MyLock)

@raise DuplicateLock when the name already exists

@param [String, Symbol] name the name of the lock @param [Class] klass the class describing the lock

@return [void]

# File lib/sidekiq_unique_jobs/config.rb, line 275
def add_lock(name, klass)
  lock_sym = name.to_sym
  raise DuplicateLock, ":#{name} already defined, please use another name" if locks.key?(lock_sym)

  new_locks = locks.dup.merge(lock_sym => klass).freeze
  self.locks = new_locks
end
add_strategy(name, klass) click to toggle source

Adds an on_conflict strategy to the configuration.

@example Add a custom strategy

add_lock(:my_strategy, CustomStrategies::MyStrategy)

@raise [DuplicateStrategy] when the name already exists

@param [String] name the name of the custom strategy @param [Class] klass the class describing the strategy

# File lib/sidekiq_unique_jobs/config.rb, line 294
def add_strategy(name, klass)
  strategy_sym = name.to_sym
  raise DuplicateStrategy, ":#{name} already defined, please use another name" if strategies.key?(strategy_sym)

  new_strategies = strategies.dup.merge(strategy_sym => klass).freeze
  self.strategies = new_strategies
end
class_name() click to toggle source

Memoized variable to get the class name

@return [String] name of the class

# File lib/sidekiq_unique_jobs/config.rb, line 258
def class_name
  @class_name ||= self.class.name
end
default_lock_timeout() click to toggle source

Default Lock Timeout @deprecated

@return [nil, Integer] configured value or nil

# File lib/sidekiq_unique_jobs/config.rb, line 246
def default_lock_timeout
  warn "[DEPRECATION] `#{class_name}##{__method__}` is deprecated." \
       " Please use `#{class_name}#lock_timeout` instead."
  lock_timeout
end
default_lock_timeout=(obj) click to toggle source

Set new value for default_lock_timeout @deprecated

@param [Integer] obj value to set (seconds)

@return [Integer]

# File lib/sidekiq_unique_jobs/config.rb, line 221
def default_lock_timeout=(obj)
  warn "[DEPRECATION] `#{class_name}##{__method__}` is deprecated." \
       " Please use `#{class_name}#lock_timeout=` instead."
  self.lock_timeout = obj
end
default_lock_ttl() click to toggle source

Default lock TTL (Time To Live) @deprecated

@return [nil, Integer] configured value or nil

# File lib/sidekiq_unique_jobs/config.rb, line 233
def default_lock_ttl
  warn "[DEPRECATION] `#{class_name}##{__method__}` is deprecated." \
       " Please use `#{class_name}#lock_ttl` instead."
  lock_ttl
end
default_lock_ttl=(obj) click to toggle source

Set the default_lock_ttl @deprecated

@param [Integer] obj value to set (seconds)

@return [<type>] <description>

# File lib/sidekiq_unique_jobs/config.rb, line 207
def default_lock_ttl=(obj)
  warn "[DEPRECATION] `#{class_name}##{__method__}` is deprecated." \
       " Please use `#{class_name}#lock_ttl=` instead."
  self.lock_ttl = obj
end
redis_version() click to toggle source

The current version of redis

@return [String] a version string eg. `5.0.1`

# File lib/sidekiq_unique_jobs/config.rb, line 308
def redis_version
  self.current_redis_version = SidekiqUniqueJobs.fetch_redis_version if current_redis_version == REDIS_VERSION
  current_redis_version
end