Class: SidekiqUniqueJobs::LockArgs

Inherits:
Object
  • Object
show all
Includes:
JSON, Logging, SidekiqWorkerMethods
Defined in:
lib/sidekiq_unique_jobs/lock_args.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 JSON

dump_json, load_json, safe_load_json

Methods included from SidekiqWorkerMethods

#after_unlock_hook, #job_class_constantize, #job_method_defined?, #job_options, #sidekiq_job_class?

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) ⇒ LockArgs

Returns a new instance of LockArgs.

Parameters:

  • item (Hash)

    a Sidekiq job hash



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

def initialize(item)
  @item = item
  @args = item[ARGS]
  self.job_class = item[CLASS]
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



25
26
27
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 25

def args
  @args
end

#itemHash (readonly)

The sidekiq job hash

Returns:

  • (Hash)

    the Sidekiq job hash



21
22
23
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 21

def item
  @item
end

Class Method Details

.call(item) ⇒ String

Convenience method for returning a digest

Parameters:

  • item (Hash)

    a Sidekiq job hash

Returns:

  • (String)

    a unique digest



15
16
17
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 15

def self.call(item)
  new(item).lock_args
end

Instance Method Details

#default_job_optionsHash<String, Object>

The globally default worker options configured from Sidekiq

Returns:

  • (Hash<String, Object>)


119
120
121
122
123
124
125
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 119

def default_job_options
  @default_job_options ||= if Sidekiq.respond_to?(:default_job_options)
    Sidekiq.default_job_options.stringify_keys
  else
    Sidekiq.default_worker_options.stringify_keys
  end
end

#default_lock_args_methodObject

The global worker options defined in Sidekiq directly



108
109
110
111
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 108

def default_lock_args_method
  default_job_options[LOCK_ARGS_METHOD] ||
    default_job_options[UNIQUE_ARGS_METHOD]
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



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

def filter_by_proc(args)
  lock_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:



88
89
90
91
92
93
94
95
96
97
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 88

def filter_by_symbol(args)
  return args unless job_method_defined?(lock_args_method)

  job_class.send(lock_args_method, args)
rescue ArgumentError
  raise SidekiqUniqueJobs::InvalidUniqueArguments,
        given: args,
        job_class: job_class,
        lock_args_method: lock_args_method
end

#filtered_argsArray

Filters unique arguments by proc or symbol

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 64

def filtered_args
  return args if lock_args_disabled?

  json_args = Normalizer.jsonify(args)

  case lock_args_method
  when Proc
    filter_by_proc(json_args)
  when Symbol
    filter_by_symbol(json_args)
  end
end

#lock_argsArray

The unique arguments to use for creating a lock

Returns:



36
37
38
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 36

def lock_args
  @lock_args ||= filtered_args || []
end

#lock_args_disabled?true, false

Checks if the worker class has disabled lock_args

Returns:

  • (true, false)


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

def lock_args_disabled?
  !lock_args_method
end

#lock_args_enabled?true, false

Checks if the worker class has enabled lock_args

Returns:

  • (true, false)


42
43
44
45
46
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 42

def lock_args_enabled?
  # return false unless lock_args_method_valid?

  lock_args_method
end

#lock_args_methodObject

The method to use for filtering unique arguments



100
101
102
103
104
105
# File 'lib/sidekiq_unique_jobs/lock_args.rb', line 100

def lock_args_method
  @lock_args_method ||= job_options.slice(LOCK_ARGS_METHOD, UNIQUE_ARGS_METHOD).values.first
  @lock_args_method ||= :lock_args if job_method_defined?(:lock_args)
  @lock_args_method ||= :unique_args if job_method_defined?(:unique_args)
  @lock_args_method ||= default_lock_args_method
end

#lock_args_method_valid?true, false

Validate that the lock_args_method is acceptable

Returns:

  • (true, false)


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

def lock_args_method_valid?
  [NilClass, TrueClass, FalseClass].none? { |klass| lock_args_method.is_a?(klass) }
end