Class: Mongomatic::TransactionLock
- Defined in:
- lib/mongomatic/transaction_lock.rb
Constant Summary
Constants included from TypedFields
Mongomatic::TypedFields::KNOWN_TYPES
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Methods inherited from Base
#==, #[], #[]=, all, collection, collection_name, count, db, db=, #delete, #do_callback, do_callback, #doc, #doc=, drop, each, empty?, find, find_one, first, #has_key?, #hash_for_field, #initialize, #insert, insert, #insert!, insert!, #is_new?, #merge, #new?, #reload, #remove, #remove!, #removed?, #set_value_for_key, #to_hash, #transaction, #update, #update!, #valid?, #validate, #value_for_key
Methods included from TypedFields
Methods included from ActiveModelCompliancy
#destroyed?, #new_record?, #persisted?, #to_key, #to_model, #to_param
Methods included from Util
Methods included from Modifiers
#add_to_set, #add_to_set!, #inc, #inc!, #pop_first, #pop_first!, #pop_last, #pop_last!, #pull, #pull!, #pull_all, #pull_all!, #push, #push!, #push_all, #push_all!, #set, #set!, #unset, #unset!
Constructor Details
This class inherits a constructor from Mongomatic::Base
Class Method Details
.create_indexes ⇒ Object
4 5 6 7 |
# 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 ⇒ Object
30 31 32 |
# 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) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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 |