Sha256: dc5d5afbb3006f78302cd13ab58b9c038c334aa66bc66857bae2a242fb840fb8

Contents?: true

Size: 1.3 KB

Versions: 8

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

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

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

      attr_reader :redis_pool, :unique_key, :jid

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

        @redis_pool    = redis_pool
        @unique_key    = unique_key
        @jid           = jid
      end

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

        handle_result(result)
      end

      def handle_result(result)
        case result
        when 1
          logger.debug { "successfully unlocked #{unique_key}" }
          true
        when 0
          logger.debug { "expiring lock #{unique_key} is not owned by #{jid}" }
          false
        when -1
          logger.debug { "#{unique_key} is not a known key" }
          false
        else
          raise UnexpectedValue, "failed to release lock : unexpected return value (#{result})"
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-5.0.11 lib/sidekiq_unique_jobs/scripts/release_lock.rb
sidekiq-unique-jobs-5.0.10 lib/sidekiq_unique_jobs/scripts/release_lock.rb
sidekiq-unique-jobs-5.0.9 lib/sidekiq_unique_jobs/scripts/release_lock.rb
sidekiq-unique-jobs-5.0.8 lib/sidekiq_unique_jobs/scripts/release_lock.rb
sidekiq-unique-jobs-5.0.7 lib/sidekiq_unique_jobs/scripts/release_lock.rb
sidekiq-unique-jobs-5.0.6 lib/sidekiq_unique_jobs/scripts/release_lock.rb
sidekiq-unique-jobs-5.0.5 lib/sidekiq_unique_jobs/scripts/release_lock.rb
sidekiq-unique-jobs-5.0.4 lib/sidekiq_unique_jobs/scripts/release_lock.rb