Sha256: 10debcdb8e3fabb4ee6a45476ebd7ab438bdd0db27ef611da52bd5ae37149952

Contents?: true

Size: 979 Bytes

Versions: 18

Compression:

Stored size: 979 Bytes

Contents

# frozen_string_literal: true
require 'redlock'
module Hyrax
  class LockManager
    class UnableToAcquireLockError < StandardError; end

    attr_reader :client

    # @param [Fixnum] time_to_live How long to hold the lock in milliseconds
    # @param [Fixnum] retry_count How many times to retry to acquire the lock before raising UnableToAcquireLockError
    # @param [Fixnum] retry_delay Maximum wait time in milliseconds before retrying. Wait time is a random value between 0 and retry_delay.
    def initialize(time_to_live, retry_count, retry_delay)
      @ttl = time_to_live
      @client = Redlock::Client.new([Redis.current], retry_count: retry_count, retry_delay: retry_delay)
    end

    # Blocks until lock is acquired or timeout.
    def lock(key)
      returned_from_block = nil
      client.lock(key, @ttl) do |locked|
        raise UnableToAcquireLockError unless locked
        returned_from_block = yield
      end
      returned_from_block
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
hyrax-3.6.0 app/services/hyrax/lock_manager.rb
hyrax-4.0.0.rc2 app/services/hyrax/lock_manager.rb
hyrax-4.0.0.rc1 app/services/hyrax/lock_manager.rb
hyrax-3.5.0 app/services/hyrax/lock_manager.rb
hyrax-4.0.0.beta2 app/services/hyrax/lock_manager.rb
hyrax-3.4.2 app/services/hyrax/lock_manager.rb
hyrax-4.0.0.beta1 app/services/hyrax/lock_manager.rb
hyrax-3.4.1 app/services/hyrax/lock_manager.rb
hyrax-3.4.0 app/services/hyrax/lock_manager.rb
hyrax-3.3.0 app/services/hyrax/lock_manager.rb
hyrax-3.2.0 app/services/hyrax/lock_manager.rb
hyrax-3.1.0 app/services/hyrax/lock_manager.rb
hyrax-3.0.2 app/services/hyrax/lock_manager.rb
hyrax-3.0.1 app/services/hyrax/lock_manager.rb
hyrax-3.0.0 app/services/hyrax/lock_manager.rb
hyrax-3.0.0.pre.rc4 app/services/hyrax/lock_manager.rb
hyrax-3.0.0.pre.rc3 app/services/hyrax/lock_manager.rb
hyrax-3.0.0.pre.rc2 app/services/hyrax/lock_manager.rb