lib/em-hiredis/lock.rb
def acquire
      df = EM::DefaultDeferrable.new
      @redis.lock_acquire([@key], [@token, @timeout]).callback { |success|
        if (success)
          EM::Hiredis.logger.debug "#{to_s} acquired"

          EM.cancel_timer(@expire_timer) if @expire_timer
          @expire_timer = EM.add_timer(@timeout - 1) {
            EM::Hiredis.logger.debug "#{to_s} Expires in 1s"
            @onexpire.call if @onexpire
          }

          df.succeed
        else
          EM::Hiredis.logger.debug "#{to_s} failed to acquire"
          df.fail("Lock is not available")
        end
      }.errback { |e|
        EM::Hiredis.logger.error "#{to_s} Error acquiring lock #{e}"
        df.fail(e)
      }
      df
    end
def acquire
      df = EM::DefaultDeferrable.new
      @redis.lock_acquire([@key], [@token, @timeout]).callback { |success|
        if (success)
          EM::Hiredis.logger.debug "#{to_s} acquired"

          EM.cancel_timer(@expire_timer) if @expire_timer
          @expire_timer = EM.add_timer(@timeout - 1) {
            EM::Hiredis.logger.debug "#{to_s} Expires in 1s"
            @onexpire.call if @onexpire
          }

          df.succeed
        else
          EM::Hiredis.logger.debug "#{to_s} failed to acquire"
          df.fail("Lock is not available")
        end
      }.errback { |e|
        EM::Hiredis.logger.error "#{to_s} Error acquiring lock #{e}"
        df.fail(e)
      }
      df
    end
def acquire df = EM :: DefaultDeferrable . new @redis . lock_acquire ( [ @key ] , [ @token , @timeout ] ) . callback { | success | if ( success ) EM :: Hiredis . logger . debug "#{to_s} acquired" EM . cancel_timer ( @expire_timer ) if @expire_timer @expire_timer = EM . add_timer ( @timeout - 1 ) { EM :: Hiredis . logger . debug "#{to_s} Expires in 1s" @onexpire . call if @onexpire } df . succeed else EM :: Hiredis . logger . debug "#{to_s} failed to acquire" df . fail ( "Lock is not available" ) end } . errback { | e | EM :: Hiredis . logger . error "#{to_s} Error acquiring lock #{e}" df . fail ( e ) } df end
Acquire the lock

 This is a re-entrant lock, re-acquiring will succeed and extend the timeout

 Returns a deferrable which either succeeds if the lock can be acquired, or fails if it cannot.
Acquire the lock