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



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

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



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

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



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

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



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

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



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

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

#default_unique_args_methodObject

The global worker options defined in Sidekiq directly



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

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



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

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



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

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:



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

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:



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

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)


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

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)


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

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:



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

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)


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

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



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

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



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

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



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

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