Module: SidekiqUniqueJobs::SidekiqWorkerMethods

Included in:
LockArgs, LockDigest, LockTTL, LockTimeout, LockType, OnConflict::Reschedule
Defined in:
lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb

Overview

Module with convenience methods for the Sidekiq::Worker class

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#job_classObject

Returns the value of attribute job_class.



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

def job_class
  @job_class
end

Instance Method Details

#after_unlock_hookProc

The hook to call after a successful unlock

Returns:

  • (Proc)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb', line 42

def after_unlock_hook # rubocop:disable Metrics/MethodLength
  lambda do
    if @original_job_class.respond_to?(:after_unlock)
      # instance method in sidekiq v6
      if @original_job_class.method(:after_unlock).arity.positive? # arity check to maintain backwards compatibility
        @original_job_class.after_unlock(item)
      else
        @original_job_class.after_unlock
      end
    elsif job_class.respond_to?(:after_unlock)
      # class method regardless of sidekiq version
      if job_class.method(:after_unlock).arity.positive? # arity check to maintain backwards compatibility
        job_class.after_unlock(item)
      else
        job_class.after_unlock
      end
    end
  end
end

#default_job_optionsHash<Symbol, Object>

Returns the default worker options from Sidekiq

Returns:

  • (Hash<Symbol, Object>)


76
77
78
79
80
81
82
# File 'lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb', line 76

def default_job_options
  if Sidekiq.respond_to?(:default_job_options)
    Sidekiq.default_job_options
  else
    Sidekiq.default_worker_options
  end
end

#job_class_constantize(klazz = @job_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

Returns:



66
67
68
# File 'lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb', line 66

def job_class_constantize(klazz = @job_class)
  SidekiqUniqueJobs.safe_constantize(klazz)
end

#job_method_defined?(method_sym) ⇒ true, false

Avoids duplicating worker_class.respond_to? in multiple places

Returns:

  • (true, false)


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

def job_method_defined?(method_sym)
  job_class.respond_to?(method_sym)
end

#job_optionsHash

Wraps #get_sidekiq_options to always work with a hash

Returns:

  • (Hash)

    of the worker class sidekiq options



21
22
23
24
25
# File 'lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb', line 21

def job_options
  return {} unless sidekiq_job_class?

  job_class.get_sidekiq_options.deep_stringify_keys
end

#sidekiq_job_class?true, false

Tests that the

Returns:

  • (true)

    if job_class responds to get_sidekiq_options

  • (false)

    if job_class does not respond to get_sidekiq_options



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

def sidekiq_job_class?
  job_method_defined?(:get_sidekiq_options)
end