Class: SidekiqUniqueJobs::LockDigest
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::LockDigest
- Includes:
- JSON, Logging, SidekiqWorkerMethods
- Defined in:
- lib/sidekiq_unique_jobs/lock_digest.rb
Overview
Handles uniqueness of sidekiq arguments
Instance Attribute Summary collapse
-
#args ⇒ String
readonly
The prefix for the unique key.
-
#item ⇒ Hash
readonly
The sidekiq job hash.
-
#lock_args ⇒ Object
readonly
Returns the value of attribute lock_args.
-
#lock_prefix ⇒ Object
readonly
Returns the value of attribute lock_prefix.
Attributes included from SidekiqWorkerMethods
Class Method Summary collapse
-
.call(item) ⇒ String
Generates a new digest.
Instance Method Summary collapse
-
#create_digest ⇒ String
Creates a namespaced unique digest based on the #digestable_hash and the #lock_prefix.
-
#digestable_hash ⇒ Hash
Filter a hash to use for digest.
-
#initialize(item) ⇒ LockDigest
constructor
A new instance of LockDigest.
-
#lock_digest ⇒ String
Memoized lock_digest.
-
#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.
Methods included from SidekiqWorkerMethods
#after_unlock_hook, #default_job_options, #job_class_constantize, #job_method_defined?, #job_options, #sidekiq_job_class?
Methods included from JSON
dump_json, load_json, safe_load_json
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
#initialize(item) ⇒ LockDigest
Returns a new instance of LockDigest.
38 39 40 41 42 43 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 38 def initialize(item) @item = item @lock_args = item[LOCK_ARGS] || item[UNIQUE_ARGS] # TODO: Deprecate UNIQUE_ARGS @lock_prefix = item[LOCK_PREFIX] || item[UNIQUE_PREFIX] # TODO: Deprecate UNIQUE_PREFIX self.job_class = item[CLASS] end |
Instance Attribute Details
#args ⇒ String (readonly)
Returns the prefix for the unique key.
31 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 31 attr_reader :lock_args |
#item ⇒ Hash (readonly)
The sidekiq job hash
27 28 29 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 27 def item @item end |
#lock_args ⇒ Object (readonly)
Returns the value of attribute lock_args.
31 32 33 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 31 def lock_args @lock_args end |
#lock_prefix ⇒ Object (readonly)
Returns the value of attribute lock_prefix.
35 36 37 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 35 def lock_prefix @lock_prefix end |
Class Method Details
.call(item) ⇒ String
Generates a new digest
21 22 23 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 21 def self.call(item) new(item).lock_digest end |
Instance Method Details
#create_digest ⇒ String
Creates a namespaced unique digest based on the #digestable_hash and the #lock_prefix
53 54 55 56 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 53 def create_digest digest = OpenSSL::Digest::MD5.hexdigest(dump_json(digestable_hash.sort)) "#{lock_prefix}:#{digest}" end |
#digestable_hash ⇒ Hash
Filter a hash to use for digest
60 61 62 63 64 65 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 60 def digestable_hash @item.slice(CLASS, QUEUE, LOCK_ARGS, APARTMENT).tap do |hash| hash.delete(QUEUE) if unique_across_queues? hash.delete(CLASS) if unique_across_workers? end end |
#lock_digest ⇒ String
Memoized lock_digest
47 48 49 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 47 def lock_digest @lock_digest ||= create_digest end |
#unique_across_queues? ⇒ true, false
Checks if we should disregard the queue when creating the unique digest
69 70 71 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 69 def unique_across_queues? item[UNIQUE_ACROSS_QUEUES] || [UNIQUE_ACROSS_QUEUES] end |
#unique_across_workers? ⇒ true, false
Checks if we should disregard the worker when creating the unique digest
75 76 77 |
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 75 def unique_across_workers? item[UNIQUE_ACROSS_WORKERS] || [UNIQUE_ACROSS_WORKERS] end |