class SidekiqUniqueJobs::LockConfig

Gathers all configuration for a lock

which helps reduce the amount of instance variables

@author Mikael Henriksson <mikael@mhenrixon.com>

Attributes

errors[R]

@!attribute [r] errors

@return [Array<Hash<Symbol, Array<String>] a collection of configuration errors
limit[R]

@!attribute [r] limit

@return [Integer] the number of simultaneous locks
lock_info[R]

@!attribute [r] lock_info

@return [Boolean] indicate wether to use lock_info or not
on_conflict[R]

@!attribute [r] on_conflict

@return [Symbol, Hash<Symbol, Symbol>] the strategies to use as conflict resolution
pttl[R]

@!attribute [r] ttl

@return [Integer, nil] the time (in milliseconds) to live after successful
timeout[R]

@!attribute [r] timeout

@return [Integer, nil] the time to wait for a lock
ttl[R]

@!attribute [r] ttl

@return [Integer, nil] the time (in seconds) to live after successful
type[R]

@!attribute [r] type

@return [Symbol] the type of lock
worker[R]

@!attribute [r] worker

@return [Symbol] the worker class

Public Class Methods

from_worker(options) click to toggle source

Instantiate a new lock_config based on sidekiq options in worker

@param [Hash] options sidekiq_options for worker

@return [LockConfig]

# File lib/sidekiq_unique_jobs/lock_config.rb, line 55
def self.from_worker(options)
  new(options.deep_stringify_keys)
end
new(job_hash = {}) click to toggle source
# File lib/sidekiq_unique_jobs/lock_config.rb, line 59
def initialize(job_hash = {})
  @type        = job_hash[LOCK]&.to_sym
  @worker      = SidekiqUniqueJobs.safe_constantize(job_hash[CLASS])
  @limit       = job_hash.fetch(LOCK_LIMIT, 1)&.to_i
  @timeout     = job_hash.fetch(LOCK_TIMEOUT, 0)&.to_i
  @ttl         = job_hash.fetch(LOCK_TTL) { job_hash.fetch(LOCK_EXPIRATION, nil) }.to_i
  @pttl        = ttl * 1_000
  @lock_info   = job_hash.fetch(LOCK_INFO) { SidekiqUniqueJobs.config.lock_info }
  @on_conflict = job_hash.fetch(ON_CONFLICT, nil)
  @errors      = job_hash.fetch(ERRORS) { {} }

  @on_client_conflict = job_hash[ON_CLIENT_CONFLICT]
  @on_server_conflict = job_hash[ON_SERVER_CONFLICT]
end

Public Instance Methods

errors_as_string() click to toggle source

Return a nice descriptive message with all validation errors

@return [String]

# File lib/sidekiq_unique_jobs/lock_config.rb, line 104
def errors_as_string
  return if valid?

  @errors_as_string ||= begin
    error_msg = +"\t"
    error_msg << errors.map { |key, val| "#{key}: :#{val}" }.join("\n\t")
    error_msg
  end
end
lock_info?() click to toggle source
# File lib/sidekiq_unique_jobs/lock_config.rb, line 74
def lock_info?
  lock_info
end
on_client_conflict() click to toggle source

the strategy to use as conflict resolution from sidekiq client

# File lib/sidekiq_unique_jobs/lock_config.rb, line 115
def on_client_conflict
  @on_client_conflict ||= on_conflict["client"] if on_conflict.is_a?(Hash)
  @on_client_conflict ||= on_conflict
end
on_server_conflict() click to toggle source

the strategy to use as conflict resolution from sidekiq server

# File lib/sidekiq_unique_jobs/lock_config.rb, line 121
def on_server_conflict
  @on_server_conflict ||= on_conflict["server"] if on_conflict.is_a?(Hash)
  @on_server_conflict ||= on_conflict
end
valid?() click to toggle source

Is the configuration valid?

@return [true,false]

# File lib/sidekiq_unique_jobs/lock_config.rb, line 94
def valid?
  errors.empty?
end
wait_for_lock?() click to toggle source

Indicate if timeout was set

@return [true,false]

# File lib/sidekiq_unique_jobs/lock_config.rb, line 84
def wait_for_lock?
  timeout.nil? || timeout.positive?
end