Sha256: ec98d77b3548979d44f95bacb806208b5a6d14cd3907436da80c6569b405135e

Contents?: true

Size: 1.16 KB

Versions: 22

Compression:

Stored size: 1.16 KB

Contents

require 'redlock'
module CurationConcerns
  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([uri], 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

    private

      def uri
        @uri ||= begin
          opts = options
          URI("#{opts[:scheme]}://#{opts[:host]}:#{opts[:port]}").to_s
        end
      end

      def options
        ::Resque.redis.redis.client.options
      end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
curation_concerns-0.14.0.pre3 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.14.0.pre2 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.14.0.pre1 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.13.1 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.13.0 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.12.0 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.12.0.pre9 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.12.0.pre8 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.12.0.pre7 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.12.0.pre6 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.12.0.pre5 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.12.0.pre4 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.12.0.pre3 app/services/curation_concerns/lock_manager.rb
curation_concerns-0.12.0.pre2 app/services/curation_concerns/lock_manager.rb
curation_concerns-models-0.12.0.pre1 app/services/curation_concerns/lock_manager.rb
curation_concerns-models-0.11.0 app/services/curation_concerns/lock_manager.rb
curation_concerns-models-0.11.0.rc1 app/services/curation_concerns/lock_manager.rb
curation_concerns-models-0.10.0 app/services/curation_concerns/lock_manager.rb
curation_concerns-models-0.9.0 app/services/curation_concerns/lock_manager.rb
curation_concerns-models-0.8.0 app/services/curation_concerns/lock_manager.rb