class SidekiqUniqueJobs::LockArgs
@author Mikael Henriksson <mikael@zoolutions.se>
Handles uniqueness of sidekiq arguments
def self.call(item)
-
(String)
- a unique digest
Parameters:
-
item
(Hash
) -- a Sidekiq job hash
def self.call(item) new(item).lock_args end
def default_lock_args_method
def default_lock_args_method default_worker_options[LOCK_ARGS] || default_worker_options[UNIQUE_ARGS] end
def default_worker_options
-
(Hash
-)
def default_worker_options @default_worker_options ||= Sidekiq.default_worker_options.stringify_keys end
def filter_by_proc(args)
-
(Array)
- with the filtered arguments
Parameters:
-
args
(Array
) -- the arguments passed to the sidekiq worker
def filter_by_proc(args) lock_args_method.call(args) end
def filter_by_symbol(args)
-
(Array)
- with the filtered arguments -
(Array)
- unfiltered unless {#worker_method_defined?}
Parameters:
-
args
(Array
) -- the arguments passed to the sidekiq worker
def filter_by_symbol(args) return args unless worker_method_defined?(lock_args_method) worker_class.send(lock_args_method, args) rescue ArgumentError raise SidekiqUniqueJobs::InvalidUniqueArguments, given: args, worker_class: worker_class, lock_args_method: lock_args_method end
def filtered_args
-
(Array)
- args unfiltered when neither of the above -
(Array)
- {#filter_by_symbol} when {#lock_args_method} is a Symbol -
(Array)
- {#filter_by_proc} when {#lock_args_method} is a Proc
def filtered_args return args if lock_args_disabled? json_args = Normalizer.jsonify(args) case lock_args_method when Proc filter_by_proc(json_args) when Symbol filter_by_symbol(json_args) end end
def initialize(item)
-
item
(Hash
) -- a Sidekiq job hash
def initialize(item) @item = item @worker_class = item[CLASS] @args = item[ARGS] end
def lock_args
-
(Array)
- the arguments filters by the {#filtered_args} method if {#lock_args_enabled?}
def lock_args @lock_args ||= filtered_args end
def lock_args_disabled?
-
(true, false)
-
def lock_args_disabled? !lock_args_method end
def lock_args_enabled?
-
(true, false)
-
def lock_args_enabled? # return false unless lock_args_method_valid? lock_args_method end
def lock_args_method
def lock_args_method @lock_args_method ||= worker_options[LOCK_ARGS] || worker_options[UNIQUE_ARGS] @lock_args_method ||= :lock_args if worker_method_defined?(:lock_args) @lock_args_method ||= :unique_args if worker_method_defined?(:unique_args) @lock_args_method ||= default_lock_args_method end
def lock_args_method_valid?
-
(true, false)
-
def lock_args_method_valid? [NilClass, TrueClass, FalseClass].none? { |klass| lock_args_method.is_a?(klass) } end