Class: SidekiqUniqueJobs::LockDigest

Inherits:
Object
  • Object
show all
Includes:
JSON, Logging, SidekiqWorkerMethods
Defined in:
lib/sidekiq_unique_jobs/lock_digest.rb

Overview

Handles uniqueness of sidekiq arguments

Author:

Instance Attribute Summary collapse

Attributes included from SidekiqWorkerMethods

#job_class

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • item (Hash)

    a Sidekiq job hash



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

#argsString (readonly)

Returns the prefix for the unique key.

Returns:

  • (String)

    the prefix for the unique key



31
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 31

attr_reader :lock_args

#itemHash (readonly)

The sidekiq job hash

Returns:

  • (Hash)

    the Sidekiq job hash



27
28
29
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 27

def item
  @item
end

#lock_argsObject (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_prefixObject (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

Parameters:

  • item (Hash)

    a sidekiq job hash

Returns:

  • (String)

    a unique digest for the given arguments



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_digestString

Creates a namespaced unique digest based on the #digestable_hash and the #lock_prefix

Returns:

  • (String)

    a unique digest



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_hashHash

Filter a hash to use for digest

Returns:

  • (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_digestString

Memoized lock_digest

Returns:

  • (String)

    a unique 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

Returns:

  • (true, false)


69
70
71
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 69

def unique_across_queues?
  item[UNIQUE_ACROSS_QUEUES] || job_options[UNIQUE_ACROSS_QUEUES]
end

#unique_across_workers?true, false

Checks if we should disregard the worker when creating the unique digest

Returns:

  • (true, false)


75
76
77
# File 'lib/sidekiq_unique_jobs/lock_digest.rb', line 75

def unique_across_workers?
  item[UNIQUE_ACROSS_WORKERS] || job_options[UNIQUE_ACROSS_WORKERS]
end