Sha256: 8ef873b97ee45869975b340d517e45a9a8962cd05e9758b49705d79eb9301b3f

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

require 'redlock'
module Sufia
  class LockManager
    class UnableToAcquireLockError < StandardError; end

    # TODO: This file is the same as curation_concerns/curation_concerns-models/app/services/curation_concerns/lock_manager.rb.
    #       During the merge of Sufia to use Curation Concerns, this file may be replaced by the Curation Concerns version.

    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)
      client.lock(key, @ttl) do |locked|
        if locked
          yield
        else
          raise UnableToAcquireLockError
        end
      end
    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

2 entries across 2 versions & 1 rubygems

Version Path
sufia-models-6.5.0 app/services/sufia/lock_manager.rb
sufia-models-6.4.0 app/services/sufia/lock_manager.rb