Sha256: 127d7026bfc54a5f4c786c9e1f02821cc316ff7d212be843ea5b965d000e92c3

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

class WithLock::Client
  extend WithLock::Common
  @@locker = nil
  
  def self.scoped_name(name)
    "#{scope}-#{name}"
  end
  
  def self.identity
    "#{`hostname`.strip}|#{$$}"
  end
  
  def self.get(name,timeout=5)
    locker.get(identity,scoped_name(name),timeout)
  end
  
  def self.release(name)
    locker.release(identity,scoped_name(name))
  end
  
  def self.mine?(name)
    locker.mine?(identity,scoped_name(name))
  end
  
  def self.with_lock(name, timeout=5, &block)
    begin
      locked = mine?(name) && increment(name)
      locked ||= get(name,timeout) || raise(WithLock::LockException.new("Failed to obtain lock #{name} in #{timeout} seconds."))
      yield
    ensure
      if locker_available?
        decrement(name).to_i > 0 || release(name) || logger.debug("Warning: lock #{name} not released!")
      end
    end
  end
  
  def self.locker_available?
    locker.url
    true
  rescue DRb::DRbConnError => e
    false
  end
  
  def self.increment(name)
    locker.increment(identity,scoped_name(name))
  end

  def self.decrement(name)
    locker.decrement(identity,scoped_name(name))
  end
  
  def self.scope
    @@scope ||= Rails.env if defined? Rails
    @@scope ||= File.expand_path('.').split(File::SEPARATOR).last
  end
  
  def self.reconnect!
    DRb.stop_service
    @@uri = DRb.start_service
    @@locker = DRbObject.new_with_uri(url)
  end
  
  def self.locker
    return @@locker if (@@locker.url rescue false)
    @@locker = nil
    tries = 3
    while @@locker.nil? && tries > 0 do
      sleep 0.2 if tries < 3
      tries -= 1
      reconnect!
      @@locker = nil unless (@@locker.url rescue false)
    end
    raise WithLock::LockException.new("Couldn't connect to locker.") if @@locker.nil?
    @@locker
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
with_lock-0.1.1 lib/with_lock/client.rb
with_lock-0.1 lib/with_lock/client.rb
with_lock-0.0.4.alpha lib/with_lock/client.rb