Sha256: a6aac762478b7baa1a61d7fca8d75d5e6e042667b86dcec9e23a0702cfe6fa8e

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  module Lock
    class WhileExecuting
      def self.synchronize(item, redis_pool = nil)
        new(item, redis_pool).synchronize { yield }
      end

      def initialize(item, redis_pool = nil)
        @item = item
        @redis_pool = redis_pool
        @unique_digest = "#{create_digest}:run"
        @mutex = Mutex.new
      end

      def synchronize
        @mutex.synchronize do
          sleep 0.1 until locked?
          yield
        end
      rescue Sidekiq::Shutdown
        logger.fatal { "the unique_key: #{@unique_digest} needs to be unlocked manually" }
        raise
      ensure
        SidekiqUniqueJobs.connection(@redis_pool) { |conn| conn.del @unique_digest }
      end

      def locked?
        Scripts.call(:synchronize, @redis_pool,
                     keys: [@unique_digest],
                     argv: [Time.now.to_i, max_lock_time]) == 1
      end

      def max_lock_time
        @max_lock_time ||= RunLockTimeoutCalculator.for_item(@item).seconds
      end

      def execute(_callback)
        synchronize do
          yield
        end
      end

      def create_digest
        @create_digest ||= @item[UNIQUE_DIGEST_KEY]
        @create_digest ||= SidekiqUniqueJobs::UniqueArgs.digest(@item)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-5.0.11 lib/sidekiq_unique_jobs/lock/while_executing.rb