class Mongomatic::TransactionLock

Public Class Methods

create_indexes() click to toggle source
# File lib/mongomatic/transaction_lock.rb, line 4
def self.create_indexes
  collection.create_index("key", :unique => true, :drop_dups => true)
  collection.create_index("expire_at")
end
remove_stale_locks() click to toggle source
# File lib/mongomatic/transaction_lock.rb, line 30
def self.remove_stale_locks
  collection.remove({:expire_at => {"$lte" => Time.now.utc}}, {:safe => true})
end
start(key, duration, &block) click to toggle source
# File lib/mongomatic/transaction_lock.rb, line 9
def self.start(key, duration, &block)
  lock = new(:key => key, :expire_at => Time.now.utc + duration)
  
  # we need to get a lock
  begin
    lock.insert!
  rescue Mongo::OperationFailure => e
    remove_stale_locks
    if find_one(:key => key) == nil
      return start(key, duration, &block)
    end
    raise Mongomatic::Exceptions::CannotGetTransactionLock
  end
  
  begin
    block.call
  ensure
    lock.remove
  end
end