Class: SidekiqUniqueJobs::LockTTL

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

Overview

Calculates timeout and expiration

Author:

Instance Attribute Summary collapse

Attributes included from SidekiqWorkerMethods

#job_class

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SidekiqWorkerMethods

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

Constructor Details

#initialize(item) ⇒ LockTTL

Returns a new instance of LockTTL.

Parameters:

  • item (Hash)

    the Sidekiq job hash

Options Hash (item):

  • :lock_ttl (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



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

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

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



28
29
30
# File 'lib/sidekiq_unique_jobs/lock_ttl.rb', line 28

def item
  @item
end

Class Method Details

.calculate(item) ⇒ Integer

Note:

this method takes into consideration the time until a job is scheduled

Computes lock ttl from job arguments, sidekiq_options.

Falls back to {SidekiqUniqueJobs::Config#lock_ttl}

Returns:

  • (Integer)

    the number of seconds to live



22
23
24
# File 'lib/sidekiq_unique_jobs/lock_ttl.rb', line 22

def self.calculate(item)
  new(item).calculate
end

Instance Method Details

#calculateInteger

Note:

this method takes into consideration the time until a job is scheduled

Computes lock ttl from job arguments, sidekiq_options.

Falls back to {SidekiqUniqueJobs::Config#lock_ttl}

Returns:

  • (Integer)

    the number of seconds to live



68
69
70
71
72
73
74
75
# File 'lib/sidekiq_unique_jobs/lock_ttl.rb', line 68

def calculate
  ttl = item[LOCK_TTL]
  ttl ||= job_options[LOCK_TTL]
  ttl ||= item[LOCK_EXPIRATION] # TODO: Deprecate at some point
  ttl ||= job_options[LOCK_EXPIRATION] # TODO: Deprecate at some point
  ttl ||= SidekiqUniqueJobs.config.lock_ttl
  ttl && (ttl.to_i + time_until_scheduled)
end

#scheduled_atFloat

The time a job is scheduled

Returns:

  • (Float)

    the exact unix time the job is scheduled at



54
55
56
# File 'lib/sidekiq_unique_jobs/lock_ttl.rb', line 54

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

#time_until_scheduledInteger

Calculates the time until the job is scheduled starting from now

Returns:

  • (Integer)

    the number of seconds until job is scheduled



46
47
48
49
50
# File 'lib/sidekiq_unique_jobs/lock_ttl.rb', line 46

def time_until_scheduled
  return 0 unless scheduled_at

  scheduled_at.to_i - Time.now.utc.to_i
end