Sha256: a9f80d68b1453487c6470660af904e3aab2313a20fd63bf8b87e2592b865ad73

Contents?: true

Size: 1.95 KB

Versions: 15

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  module Timeout
    # Calculates timeout and expiration
    #
    # @author Mikael Henriksson <mikael@zoolutions.se>
    class Calculator
      include SidekiqUniqueJobs::SidekiqWorkerMethods

      # @attr [Hash] item the Sidekiq job hash
      attr_reader :item

      # @param [Hash] item the Sidekiq job hash
      # @option item [Integer, nil] :lock_expiration 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_KEY]
      end

      # The time until a job is scheduled
      # @return [Integer] the number of seconds until job is scheduled
      def time_until_scheduled
        return 0 unless scheduled_at

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

      # The time a job is scheduled
      # @return [Float] the exact unix time the job is scheduled at
      def scheduled_at
        @scheduled_at ||= item[AT_KEY]
      end

      # The configured lock_expiration
      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

      # The configured lock_timeout
      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

      # The default lock_timeout of this gem
      def default_lock_timeout
        SidekiqUniqueJobs.config.default_lock_timeout
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-6.0.25 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.24 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.23 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.22 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.21 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.20 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.16 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.19 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.13 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.12 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.11 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.10 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.9 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.8 lib/sidekiq_unique_jobs/timeout/calculator.rb
sidekiq-unique-jobs-6.0.7 lib/sidekiq_unique_jobs/timeout/calculator.rb