Class: MongoMutex::Mutex::MongoOperations

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_mutex/mutex.rb

Instance Method Summary collapse

Instance Method Details

#lock_info(collection, lock_id) ⇒ Object



80
81
82
# File 'lib/mongo_mutex/mutex.rb', line 80

def lock_info(collection, lock_id)
  collection.find({_id: lock_id}, limit: 1).first
end

#try_lock(collection, lock_id, locker_id, now, lock_retention_timeout) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mongo_mutex/mutex.rb', line 84

def try_lock(collection, lock_id, locker_id, now, lock_retention_timeout)
  collection.find_one_and_update(
    {:_id => lock_id, :$or => [
      {locked_at: {:$lt => now - lock_retention_timeout}},
      {locked_by: locker_id},
      {locked_by: {:$exists => 0}},
    ]},
    {_id: lock_id, locked_by: locker_id, locked_at: now},
    upsert: true,
  )
end

#unlock(collection, lock_id, locker_id, now, lock_retention_support) ⇒ Object



96
97
98
99
100
101
# File 'lib/mongo_mutex/mutex.rb', line 96

def unlock(collection, lock_id, locker_id, now, lock_retention_support)
  collection.find_one_and_update(
    {_id: lock_id, locked_by: locker_id, locked_at: {:$gte => now - lock_retention_support}},
    {:$unset => {locked_by: 1, locked_at: 1}},
  )
end