class SidekiqUniqueJobs::LockDigest

Handles uniqueness of sidekiq arguments

@author Mikael Henriksson <mikael@mhenrixon.com>

Attributes

item[R]

The sidekiq job hash @return [Hash] the Sidekiq job hash

lock_args[R]

@!attribute [r] args

@return [Array<Objet>] the arguments passed to `perform_async`
lock_prefix[R]

@!attribute [r] args

@return [String] the prefix for the unique key

Public Class Methods

call(item) click to toggle source

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
new(item) click to toggle source

@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

create_digest() click to toggle source

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
digestable_hash() click to toggle source

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
lock_digest() click to toggle source

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
unique_across_queues?() click to toggle source

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
unique_across_workers?() click to toggle source

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