Class: SidekiqUniqueJobs::UniqueArgs
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::UniqueArgs
- Includes:
- Logging, SidekiqWorkerMethods
- Defined in:
- lib/sidekiq_unique_jobs/unique_args.rb
Overview
Handles uniqueness of sidekiq arguments
Instance Attribute Summary collapse
-
#item ⇒ Hash
readonly
The sidekiq job hash.
Class Method Summary collapse
-
.digest(item) ⇒ String
Convenience method for returning a digest.
Instance Method Summary collapse
-
#add_uniqueness_to_item ⇒ void
Appends the keys unique_prefix, unique_args and #unique_digest to the sidekiq job hash #item.
-
#create_digest ⇒ String
Creates a namespaced unique digest based on the #digestable_hash and the #unique_prefix.
-
#default_unique_args_method ⇒ Object
The global worker options defined in Sidekiq directly.
-
#digestable_hash ⇒ Hash
Filter a hash to use for digest.
-
#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(args) ⇒ Array
Filters unique arguments by proc or symbol.
-
#initialize(item) ⇒ UniqueArgs
constructor
A new instance of UniqueArgs.
-
#unique_across_queues? ⇒ true, false
Checks if we should disregard the queue when creating the unique digest.
-
#unique_across_workers? ⇒ true, false
Checks if we should disregard the worker when creating the unique digest.
-
#unique_args(args) ⇒ Array
The unique arguments to use for creating a lock.
-
#unique_args_enabled? ⇒ true, false
Checks if the worker class has been enabled for unique_args?.
-
#unique_args_method ⇒ Object
The method to use for filtering unique arguments.
-
#unique_digest ⇒ String
Memoized unique_digest.
-
#unique_prefix ⇒ String
A prefix to use as namespace for the #unique_digest.
Methods included from SidekiqWorkerMethods
#after_unlock_hook, #default_worker_options, #sidekiq_worker_class?, #worker_class, #worker_class_constantize, #worker_method_defined?, #worker_options
Methods included from Logging
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context
Constructor Details
#initialize(item) ⇒ UniqueArgs
Returns a new instance of UniqueArgs.
27 28 29 30 31 32 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 27 def initialize(item) @item = item @worker_class = item[CLASS_KEY] add_uniqueness_to_item end |
Instance Attribute Details
#item ⇒ Hash (readonly)
The sidekiq job hash
24 25 26 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 24 def item @item end |
Class Method Details
.digest(item) ⇒ String
Convenience method for returning a digest
18 19 20 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 18 def self.digest(item) new(item).unique_digest end |
Instance Method Details
#add_uniqueness_to_item ⇒ void
This method returns an undefined value.
Appends the keys unique_prefix, unique_args and #unique_digest to the sidekiq job hash #item
36 37 38 39 40 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 36 def add_uniqueness_to_item item[UNIQUE_PREFIX_KEY] ||= unique_prefix item[UNIQUE_ARGS_KEY] = unique_args(item[ARGS_KEY]) item[UNIQUE_DIGEST_KEY] = unique_digest end |
#create_digest ⇒ String
Creates a namespaced unique digest based on the #digestable_hash and the #unique_prefix
50 51 52 53 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 50 def create_digest digest = OpenSSL::Digest::MD5.hexdigest(Sidekiq.dump_json(digestable_hash)) "#{unique_prefix}:#{digest}" end |
#default_unique_args_method ⇒ Object
The global worker options defined in Sidekiq directly
146 147 148 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 146 def default_unique_args_method Sidekiq..stringify_keys[UNIQUE_ARGS_KEY] end |
#digestable_hash ⇒ Hash
Filter a hash to use for digest
63 64 65 66 67 68 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 63 def digestable_hash @item.slice(CLASS_KEY, QUEUE_KEY, UNIQUE_ARGS_KEY).tap do |hash| hash.delete(QUEUE_KEY) if unique_across_queues? hash.delete(CLASS_KEY) if unique_across_workers? end end |
#filter_by_proc(args) ⇒ Array
Filters unique arguments by proc configured in the sidekiq worker
121 122 123 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 121 def filter_by_proc(args) unique_args_method.call(args) end |
#filter_by_symbol(args) ⇒ Array
Filters unique arguments by method configured in the sidekiq worker
129 130 131 132 133 134 135 136 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 129 def filter_by_symbol(args) return args unless worker_method_defined?(unique_args_method) worker_class.send(unique_args_method, args) rescue ArgumentError => ex log_fatal(ex) args end |
#filtered_args(args) ⇒ Array
Filters unique arguments by proc or symbol
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 102 def filtered_args(args) return args if args.empty? json_args = Normalizer.jsonify(args) case unique_args_method when Proc filter_by_proc(json_args) when Symbol filter_by_symbol(json_args) else log_debug("#{__method__} arguments not filtered (using all arguments for uniqueness)") json_args end end |
#unique_across_queues? ⇒ true, false
Checks if we should disregard the queue when creating the unique digest
80 81 82 83 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 80 def unique_across_queues? item[UNIQUE_ACROSS_QUEUES_KEY] || [UNIQUE_ACROSS_QUEUES_KEY] || item[UNIQUE_ON_ALL_QUEUES_KEY] || [UNIQUE_ON_ALL_QUEUES_KEY] # TODO: Remove in v 6.1 end |
#unique_across_workers? ⇒ true, false
Checks if we should disregard the worker when creating the unique digest
87 88 89 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 87 def unique_across_workers? item[UNIQUE_ACROSS_WORKERS_KEY] || [UNIQUE_ACROSS_WORKERS_KEY] end |
#unique_args(args) ⇒ Array
The unique arguments to use for creating a lock
72 73 74 75 76 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 72 def unique_args(args) return filtered_args(args) if unique_args_enabled? args end |
#unique_args_enabled? ⇒ true, false
Checks if the worker class has been enabled for unique_args?
93 94 95 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 93 def unique_args_enabled? unique_args_method # && !unique_args_method.is_a?(Boolean) end |
#unique_args_method ⇒ Object
The method to use for filtering unique arguments
139 140 141 142 143 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 139 def unique_args_method @unique_args_method ||= [UNIQUE_ARGS_KEY] @unique_args_method ||= :unique_args if worker_method_defined?(:unique_args) @unique_args_method ||= default_unique_args_method end |
#unique_digest ⇒ String
Memoized unique_digest
44 45 46 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 44 def unique_digest @unique_digest ||= create_digest end |
#unique_prefix ⇒ String
A prefix to use as namespace for the #unique_digest
57 58 59 |
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 57 def unique_prefix [UNIQUE_PREFIX_KEY] || SidekiqUniqueJobs.config.unique_prefix end |