Sha256: 0ba161c17b4e7fd825a094a9bcb74cea977ce15b9800b5737f8417e89d7d8bde

Contents?: true

Size: 757 Bytes

Versions: 1

Compression:

Stored size: 757 Bytes

Contents

module SidekiqUniqueJobs
  module Lock
    class WhileExecuting
      def self.synchronize(item, redis_pool = nil, &block)
        new(item, redis_pool).synchronize(&block)
      end

      def initialize(item, redis_pool = nil)
        @unique_digest = item['unique_digest'.freeze]
        @run_key = "#{@unique_digest}:run"
        @mutex = Mutex.new
        @redis_pool = redis_pool
      end

      def synchronize(&_block)
        @mutex.lock
        sleep 0.001 until locked?

        yield

      ensure
        SidekiqUniqueJobs.connection(@redis_pool) { |c| c.del @run_key }
        @mutex.unlock
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

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