Sha256: c01c0029d3eeaccb04b081910487dcb28727e0df711de53658fc805f04c5c4f9

Contents?: true

Size: 808 Bytes

Versions: 2

Compression:

Stored size: 808 Bytes

Contents

require 'morlock/gem_client'

class Morlock
  DEFAULT_EXPIRATION = 60

  attr_accessor :client

  def initialize(client)
    @client = Morlock::GemClient.wrap(client)
  end

  def lock(key, options = {})
    lock_obtained = @client.add(key, options[:expiration] || DEFAULT_EXPIRATION)
    puts "Lock for #{key} #{lock_obtained ? "obtained" : "not obtained"}." if options[:verbose]
    yield if lock_obtained && block_given?
    options[:success].call if lock_obtained && options[:success]
    options[:failure].call if !lock_obtained && options[:failure]
    lock_obtained
  ensure
    if lock_obtained
      if @client.delete(key)
        puts "Lock removed for #{key}" if options[:verbose]
      else
        puts "Someone else removed the lock for #{key}!" if options[:verbose]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
morlock-0.0.7 lib/morlock/base.rb
morlock-0.0.6 lib/morlock/base.rb