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

Author:

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.

Parameters:

  • item (Hash)

    the Sidekiq job hash

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



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

def default_lock_timeout
  SidekiqUniqueJobs.config.default_lock_timeout
end

#lock_expirationObject

The configured lock_expiration



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

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

#lock_timeoutObject

The configured lock_timeout



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

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

Returns:

  • (Float)

    the exact unix time the job is scheduled at



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

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

#time_until_scheduledInteger

The time until a job is scheduled

Returns:

  • (Integer)

    the number of seconds until job is scheduled



26
27
28
29
30
# 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