Sha256: 7dbe6b5968a40eeccbcb8f6cc32e40c0021e11bd8131ca231ef101243c828b60

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module SidekiqUniqueJobs
  module Scripts
    class AcquireLock
      extend Forwardable
      def_delegator SidekiqUniqueJobs, :logger

      def self.execute(redis_pool, unique_key, jid, max_lock_time)
        new(redis_pool, unique_key, jid, max_lock_time).execute
      end

      attr_reader :redis_pool, :unique_key, :jid, :max_lock_time

      def initialize(_redis_pool, unique_key, jid, max_lock_time)
        raise UniqueKeyMissing, 'unique_key is required' if unique_key.nil?
        raise JidMissing, 'jid is required' if jid.nil?
        raise MaxLockTimeMissing, 'max_lock_time is required' if max_lock_time.nil?

        @unique_key    = unique_key
        @jid           = jid
        @max_lock_time = max_lock_time
      end

      def execute
        result = Scripts.call(:acquire_lock, redis_pool,
                              keys: [unique_key],
                              argv: [jid, max_lock_time])

        handle_result(result)
      end

      def handle_result(result)
        case result
        when 1
          logger.debug { "successfully acquired lock #{unique_key} for #{max_lock_time} seconds" }
          true
        when 0
          logger.debug { "failed to acquire lock for #{unique_key}" }
          false
        else
          raise UnexpectedValue, "failed to acquire lock : unexpected return value (#{result})"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-5.0.2 lib/sidekiq_unique_jobs/scripts/acquire_lock.rb
sidekiq-unique-jobs-5.0.1 lib/sidekiq_unique_jobs/scripts/acquire_lock.rb