Module: SidekiqUniqueJobs::SidekiqWorkerMethods

Included in:
OnConflict::Reschedule, Timeout::Calculator, UniqueArgs
Defined in:
lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb

Overview

Module with convenience methods for the Sidekiq::Worker class

Author:

Instance Method Summary collapse

Instance Method Details

#after_unlock_hookProc

The hook to call after a successful unlock



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

def after_unlock_hook
  -> { worker_class.after_unlock if worker_method_defined?(:after_unlock) }
end

#default_worker_optionsObject



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

def default_worker_options
  Sidekiq.default_worker_options
end

#sidekiq_worker_class?true, false

Tests that the



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

def sidekiq_worker_class?
  worker_method_defined?(:get_sidekiq_options)
end

#worker_classSidekiq::Worker

The Sidekiq::Worker implementation



30
31
32
# File 'lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb', line 30

def worker_class
  @_worker_class ||= worker_class_constantize # rubocop:disable Naming/MemoizedInstanceVariableName
end

#worker_class_constantize(klazz = @worker_class) ⇒ Sidekiq::Worker

Attempt to constantize a string worker_class argument, always failing back to the original argument when the constant can’t be found



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb', line 44

def worker_class_constantize(klazz = @worker_class)
  return klazz unless klazz.is_a?(String)
  Object.const_get(klazz)
rescue NameError => ex
  case ex.message
  when /uninitialized constant/
    klazz
  else
    raise
  end
end

#worker_method_defined?(method_sym) ⇒ true, false

Avoids duplicating worker_class.respond_to? in multiple places



10
11
12
# File 'lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb', line 10

def worker_method_defined?(method_sym)
  worker_class.respond_to?(method_sym)
end

#worker_optionsHash

Wraps #get_sidekiq_options to always work with a hash



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

def worker_options
  return {} unless sidekiq_worker_class?
  worker_class.get_sidekiq_options.stringify_keys
end