Class: SidekiqUniqueJobs::UniqueArgs

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

Overview

Handles uniqueness of sidekiq arguments

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • item (Hash)

    a Sidekiq job hash



26
27
28
29
30
31
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 26

def initialize(item)
  @item         = item
  @worker_class = item[CLASS_KEY]

  add_uniqueness_to_item
end

Instance Attribute Details

#itemHash (readonly)

The sidekiq job hash

Returns:

  • (Hash)

    the Sidekiq job hash



23
24
25
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 23

def item
  @item
end

Class Method Details

.digest(item) ⇒ String

Convenience method for returning a digest

Parameters:

  • item (Hash)

    a Sidekiq job hash

Returns:

  • (String)

    a unique digest



17
18
19
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 17

def self.digest(item)
  new(item).unique_digest
end

Instance Method Details

#add_uniqueness_to_itemvoid

This method returns an undefined value.

Appends the keys unique_prefix, unique_args and #unique_digest to the sidekiq job hash #item



35
36
37
38
39
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 35

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_digestString

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

Returns:

  • (String)

    a unique digest



49
50
51
52
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 49

def create_digest
  digest = Digest::MD5.hexdigest(Sidekiq.dump_json(digestable_hash))
  "#{unique_prefix}:#{digest}"
end

#default_unique_args_methodObject

The global worker options defined in Sidekiq directly



145
146
147
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 145

def default_unique_args_method
  Sidekiq.default_worker_options.stringify_keys[UNIQUE_ARGS_KEY]
end

#digestable_hashHash

Filter a hash to use for digest

Returns:

  • (Hash)

    to use for digest



62
63
64
65
66
67
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 62

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

Parameters:

  • args (Array)

    the arguments passed to the sidekiq worker

Returns:

  • (Array)

    with the filtered arguments



120
121
122
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 120

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

Parameters:

  • args (Array)

    the arguments passed to the sidekiq worker

Returns:



128
129
130
131
132
133
134
135
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 128

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

Parameters:

  • args (Array)

    the arguments passed to the sidekiq worker

Returns:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 101

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

Returns:

  • (true, false)


79
80
81
82
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 79

def unique_across_queues?
  item[UNIQUE_ACROSS_QUEUES_KEY] || worker_options[UNIQUE_ACROSS_QUEUES_KEY] ||
    item[UNIQUE_ON_ALL_QUEUES_KEY] || worker_options[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

Returns:

  • (true, false)


86
87
88
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 86

def unique_across_workers?
  item[UNIQUE_ACROSS_WORKERS_KEY] || worker_options[UNIQUE_ACROSS_WORKERS_KEY]
end

#unique_args(args) ⇒ Array

The unique arguments to use for creating a lock

Returns:



71
72
73
74
75
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 71

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?

Returns:

  • (true, false)


92
93
94
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 92

def unique_args_enabled?
  unique_args_method # && !unique_args_method.is_a?(Boolean)
end

#unique_args_methodObject

The method to use for filtering unique arguments



138
139
140
141
142
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 138

def unique_args_method
  @unique_args_method ||= worker_options[UNIQUE_ARGS_KEY]
  @unique_args_method ||= :unique_args if worker_method_defined?(:unique_args)
  @unique_args_method ||= default_unique_args_method
end

#unique_digestString

Memoized unique_digest

Returns:

  • (String)

    a unique digest



43
44
45
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 43

def unique_digest
  @unique_digest ||= create_digest
end

#unique_prefixString

A prefix to use as namespace for the #unique_digest

Returns:

  • (String)

    a unique digest



56
57
58
# File 'lib/sidekiq_unique_jobs/unique_args.rb', line 56

def unique_prefix
  worker_options[UNIQUE_PREFIX_KEY] || SidekiqUniqueJobs.config.unique_prefix
end