class SidekiqUniqueJobs::LockDigest
Handles uniqueness of sidekiq arguments
@author Mikael Henriksson <mikael@mhenrixon.com>
Attributes
The sidekiq job hash @return [Hash] the Sidekiq
job hash
@!attribute [r] args
@return [Array<Objet>] the arguments passed to `perform_async`
@!attribute [r] args
@return [String] the prefix for the unique key
Public Class Methods
Generates a new digest
@param [Hash] item a sidekiq job hash
@return [String] a unique digest for the given arguments
# File lib/sidekiq_unique_jobs/lock_digest.rb, line 21 def self.call(item) new(item).lock_digest end
@param [Hash] item a Sidekiq
job hash
# File lib/sidekiq_unique_jobs/lock_digest.rb, line 38 def initialize(item) @item = item @worker_class = item[CLASS] @lock_args = item.slice(LOCK_ARGS, UNIQUE_ARGS).values.first # TODO: Deprecate UNIQUE_ARGS @lock_prefix = item.slice(LOCK_PREFIX, UNIQUE_PREFIX).values.first # TODO: Deprecate UNIQUE_PREFIX end
Public Instance Methods
Creates a namespaced unique digest based on the {#digestable_hash} and the {#lock_prefix} @return [String] a unique digest
# File lib/sidekiq_unique_jobs/lock_digest.rb, line 53 def create_digest digest = OpenSSL::Digest::MD5.hexdigest(dump_json(digestable_hash)) "#{lock_prefix}:#{digest}" end
Filter a hash to use for digest @return [Hash] to use for digest
# 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
Memoized lock_digest
@return [String] a unique digest
# File lib/sidekiq_unique_jobs/lock_digest.rb, line 47 def lock_digest @lock_digest ||= create_digest end
Checks if we should disregard the queue when creating the unique digest @return [true, false]
# File lib/sidekiq_unique_jobs/lock_digest.rb, line 69 def unique_across_queues? item[UNIQUE_ACROSS_QUEUES] || worker_options[UNIQUE_ACROSS_QUEUES] end
Checks if we should disregard the worker when creating the unique digest @return [true, false]
# File lib/sidekiq_unique_jobs/lock_digest.rb, line 75 def unique_across_workers? item[UNIQUE_ACROSS_WORKERS] || worker_options[UNIQUE_ACROSS_WORKERS] end