Sha256: 1c1aa42678342d573a37f63934021d7046c7b6d5e22067f65c7a642799a230aa

Contents?: true

Size: 1.69 KB

Versions: 10

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  # Calculates timeout and expiration
  #
  # @author Mikael Henriksson <mikael@zoolutions.se>
  class LockTimeout
    # includes "SidekiqUniqueJobs::SidekiqWorkerMethods"
    # @!parse include SidekiqUniqueJobs::SidekiqWorkerMethods
    include SidekiqUniqueJobs::SidekiqWorkerMethods

    #
    # Calculates the timeout for a Sidekiq job
    #
    # @param [Hash] item sidekiq job hash
    #
    # @return [Integer] timeout in seconds
    #
    def self.calculate(item)
      new(item).calculate
    end

    # @!attribute [r] item
    #   @return [Hash] the Sidekiq job hash
    attr_reader :item

    # @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
    def initialize(item)
      @item         = item
      @worker_class = item[CLASS]
    end

    #
    # Finds a lock timeout in either of
    #  default worker options, {default_lock_timeout} or provided worker_options
    #
    #
    # @return [Integer, nil]
    #
    def calculate
      timeout = default_worker_options[LOCK_TIMEOUT]
      timeout = default_lock_timeout if default_lock_timeout
      timeout = worker_options[LOCK_TIMEOUT] if worker_options.key?(LOCK_TIMEOUT)
      timeout
    end

    #
    # The configured default_lock_timeout
    # @see SidekiqUniqueJobs::Config#lock_timeout
    #
    #
    # @return [Integer, nil]
    #
    def default_lock_timeout
      SidekiqUniqueJobs.config.lock_timeout
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-7.0.0.beta25 lib/sidekiq_unique_jobs/lock_timeout.rb
sidekiq-unique-jobs-7.0.0.beta24 lib/sidekiq_unique_jobs/lock_timeout.rb
sidekiq-unique-jobs-7.0.0.beta23 lib/sidekiq_unique_jobs/lock_timeout.rb
sidekiq-unique-jobs-7.0.0.beta22 lib/sidekiq_unique_jobs/lock_timeout.rb
sidekiq-unique-jobs-7.0.0.beta21 lib/sidekiq_unique_jobs/lock_timeout.rb
sidekiq-unique-jobs-7.0.0.beta20 lib/sidekiq_unique_jobs/lock_timeout.rb
sidekiq-unique-jobs-7.0.0.beta19 lib/sidekiq_unique_jobs/lock_timeout.rb
sidekiq-unique-jobs-7.0.0.beta18 lib/sidekiq_unique_jobs/lock_timeout.rb
sidekiq-unique-jobs-7.0.0.beta17 lib/sidekiq_unique_jobs/lock_timeout.rb
sidekiq-unique-jobs-7.0.0.beta16 lib/sidekiq_unique_jobs/lock_timeout.rb