Class: SidekiqUniqueJobs::Timeout::Calculator

Inherits:
Object
  • Object
show all
Includes:
SidekiqWorkerMethods
Defined in:
lib/sidekiq_unique_jobs/timeout/calculator.rb

Overview

Calculates timeout and expiration

Instance Attribute 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

Constructor Details

#initialize(item) ⇒ Calculator

Returns a new instance of Calculator.

Options Hash (item):

  • :lock_expiration (Integer, nil)

    the configured lock expiration

  • :lock_timeout (Integer, nil)

    the configured lock timeout

  • :class (String)

    the class of the sidekiq worker

  • :at (Float)

    the unix time the job is scheduled at



19
20
21
22
# File 'lib/sidekiq_unique_jobs/timeout/calculator.rb', line 19

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

Instance Attribute Details

#itemObject (readonly)



12
13
14
# File 'lib/sidekiq_unique_jobs/timeout/calculator.rb', line 12

def item
  @item
end

Instance Method Details

#default_lock_timeoutObject

The default lock_timeout of this gem



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

def default_lock_timeout
  SidekiqUniqueJobs.config.default_lock_timeout
end

#lock_expirationObject

The configured lock_expiration



38
39
40
41
42
43
44
# File 'lib/sidekiq_unique_jobs/timeout/calculator.rb', line 38

def lock_expiration
  @lock_expiration ||= begin
    expiration = item[LOCK_EXPIRATION_KEY]
    expiration ||= worker_options[LOCK_EXPIRATION_KEY]
    expiration && expiration + time_until_scheduled
  end
end

#lock_timeoutObject

The configured lock_timeout



47
48
49
50
51
52
53
54
# File 'lib/sidekiq_unique_jobs/timeout/calculator.rb', line 47

def lock_timeout
  @lock_timeout = begin
    timeout = default_worker_options[LOCK_TIMEOUT_KEY]
    timeout = default_lock_timeout if default_lock_timeout
    timeout = worker_options[LOCK_TIMEOUT_KEY] if worker_options.key?(LOCK_TIMEOUT_KEY)
    timeout
  end
end

#scheduled_atFloat

The time a job is scheduled



33
34
35
# File 'lib/sidekiq_unique_jobs/timeout/calculator.rb', line 33

def scheduled_at
  @scheduled_at ||= item[AT_KEY]
end

#time_until_scheduledInteger

The time until a job is scheduled



26
27
28
29
# File 'lib/sidekiq_unique_jobs/timeout/calculator.rb', line 26

def time_until_scheduled
  return 0 unless scheduled_at
  scheduled_at.to_i - Time.now.utc.to_i
end