Class: SidekiqUniqueJobs::LockArgs
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::LockArgs
- Includes:
- JSON, Logging, SidekiqWorkerMethods
- Defined in:
- lib/sidekiq_unique_jobs/lock_args.rb
Overview
Handles uniqueness of sidekiq arguments
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#item ⇒ Hash
readonly
The sidekiq job hash.
Attributes included from SidekiqWorkerMethods
Class Method Summary collapse
-
.call(item) ⇒ String
Convenience method for returning a digest.
Instance Method Summary collapse
-
#default_job_options ⇒ Hash<String, Object>
The globally default worker options configured from Sidekiq.
-
#default_lock_args_method ⇒ Object
The global worker options defined in Sidekiq directly.
-
#filter_by_proc(args) ⇒ Array
Filters unique arguments by proc configured in the sidekiq worker.
-
#filter_by_symbol(args) ⇒ Array
Filters unique arguments by method configured in the sidekiq worker.
-
#filtered_args ⇒ Array
Filters unique arguments by proc or symbol.
-
#initialize(item) ⇒ LockArgs
constructor
A new instance of LockArgs.
-
#lock_args ⇒ Array
The unique arguments to use for creating a lock.
-
#lock_args_disabled? ⇒ true, false
Checks if the worker class has disabled lock_args.
-
#lock_args_enabled? ⇒ true, false
Checks if the worker class has enabled lock_args.
-
#lock_args_method ⇒ Object
The method to use for filtering unique arguments.
-
#lock_args_method_valid? ⇒ true, false
Validate that the lock_args_method is acceptable.
Methods included from JSON
dump_json, load_json, safe_load_json
Methods included from SidekiqWorkerMethods
#after_unlock_hook, #job_class_constantize, #job_method_defined?, #job_options, #sidekiq_job_class?
Methods included from Logging
#build_message, included, #log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context, #with_configured_loggers_context, #with_logging_context
Constructor Details
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
25 26 27 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 25 def args @args end |
#item ⇒ Hash (readonly)
The sidekiq job hash
21 22 23 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 21 def item @item end |
Class Method Details
.call(item) ⇒ String
Convenience method for returning a digest
15 16 17 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 15 def self.call(item) new(item).lock_args end |
Instance Method Details
#default_job_options ⇒ Hash<String, Object>
The globally default worker options configured from Sidekiq
119 120 121 122 123 124 125 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 119 def @default_job_options ||= if Sidekiq.respond_to?(:default_job_options) Sidekiq..stringify_keys else Sidekiq..stringify_keys end end |
#default_lock_args_method ⇒ Object
The global worker options defined in Sidekiq directly
108 109 110 111 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 108 def default_lock_args_method [LOCK_ARGS_METHOD] || [UNIQUE_ARGS_METHOD] end |
#filter_by_proc(args) ⇒ Array
Filters unique arguments by proc configured in the sidekiq worker
80 81 82 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 80 def filter_by_proc(args) lock_args_method.call(args) end |
#filter_by_symbol(args) ⇒ Array
Filters unique arguments by method configured in the sidekiq worker
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 88 def filter_by_symbol(args) return args unless job_method_defined?(lock_args_method) job_class.send(lock_args_method, args) rescue ArgumentError raise SidekiqUniqueJobs::InvalidUniqueArguments, given: args, job_class: job_class, lock_args_method: lock_args_method end |
#filtered_args ⇒ Array
Filters unique arguments by proc or symbol
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 64 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 |
#lock_args ⇒ Array
The unique arguments to use for creating a lock
36 37 38 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 36 def lock_args @lock_args ||= filtered_args || [] end |
#lock_args_disabled? ⇒ true, false
Checks if the worker class has disabled lock_args
56 57 58 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 56 def lock_args_disabled? !lock_args_method end |
#lock_args_enabled? ⇒ true, false
Checks if the worker class has enabled lock_args
42 43 44 45 46 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 42 def lock_args_enabled? # return false unless lock_args_method_valid? lock_args_method end |
#lock_args_method ⇒ Object
The method to use for filtering unique arguments
100 101 102 103 104 105 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 100 def lock_args_method @lock_args_method ||= .slice(LOCK_ARGS_METHOD, UNIQUE_ARGS_METHOD).values.first @lock_args_method ||= :lock_args if job_method_defined?(:lock_args) @lock_args_method ||= :unique_args if job_method_defined?(:unique_args) @lock_args_method ||= default_lock_args_method end |
#lock_args_method_valid? ⇒ true, false
Validate that the lock_args_method is acceptable
50 51 52 |
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 50 def lock_args_method_valid? [NilClass, TrueClass, FalseClass].none? { |klass| lock_args_method.is_a?(klass) } end |