class SidekiqUniqueJobs::LockTTL

Calculates timeout and expiration

@author Mikael Henriksson <mikael@mhenrixon.com>

Attributes

item[R]

@!attribute [r] item

@return [Hash] the Sidekiq job hash

Public Class Methods

calculate(item) click to toggle source

Computes lock ttl from job arguments, sidekiq_options.

Falls back to {SidekiqUniqueJobs::Config#lock_ttl}

@note this method takes into consideration the time

until a job is scheduled

@return [Integer] the number of seconds to live

# File lib/sidekiq_unique_jobs/lock_ttl.rb, line 22
def self.calculate(item)
  new(item).calculate
end
new(item) click to toggle source

@param [Hash] item the Sidekiq job hash @option item [Integer, nil] :lock_ttl the configured lock expiration @option item [Integer, nil] :lock_timeout the configured lock timeout @option item [String] :class the class of the sidekiq worker @option item [Float] :at the unix time the job is scheduled at

# File lib/sidekiq_unique_jobs/lock_ttl.rb, line 35
def initialize(item)
  @item         = item
  @worker_class = item[CLASS]
end

Public Instance Methods

calculate() click to toggle source

Computes lock ttl from job arguments, sidekiq_options.

Falls back to {SidekiqUniqueJobs::Config#lock_ttl}

@note this method takes into consideration the time

until a job is scheduled

@return [Integer] the number of seconds to live

# File lib/sidekiq_unique_jobs/lock_ttl.rb, line 68
def calculate
  ttl = item[LOCK_TTL]
  ttl ||= worker_options[LOCK_TTL]
  ttl ||= item[LOCK_EXPIRATION] # TODO: Deprecate at some point
  ttl ||= worker_options[LOCK_EXPIRATION] # TODO: Deprecate at some point
  ttl ||= SidekiqUniqueJobs.config.lock_ttl
  ttl && (ttl.to_i + time_until_scheduled)
end
scheduled_at() click to toggle source

The time a job is scheduled @return [Float] the exact unix time the job is scheduled at

# File lib/sidekiq_unique_jobs/lock_ttl.rb, line 54
def scheduled_at
  @scheduled_at ||= item[AT]
end
time_until_scheduled() click to toggle source

Calculates the time until the job is scheduled starting from now

@return [Integer] the number of seconds until job is scheduled

# 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