Sha256: a1a7b9b7bd95b536d7406620227cf79b7d612c731b22495f5727ee4e13ece10b

Contents?: true

Size: 729 Bytes

Versions: 1

Compression:

Stored size: 729 Bytes

Contents

module WithAdvisoryLock
  class Base
    attr_reader :connection, :lock_name, :timeout_seconds

    def initialize(connection, lock_name, timeout_seconds)
      @connection = connection
      @lock_name = lock_name
      @timeout_seconds = timeout_seconds
    end

    def quoted_lock_name
      connection.quote(lock_name)
    end

    def with_advisory_lock(&block)
      give_up_at = Time.now + @timeout_seconds if @timeout_seconds
      while @timeout_seconds.nil? || Time.now < give_up_at do
        if try_lock
          begin
            return yield
          ensure
            release_lock
          end
        else
          sleep(0.1)
        end
      end
      false # failed to get lock in time.
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
with_advisory_lock-0.0.1 lib/with_advisory_lock/base.rb