Module: Mongoid::Locker::Wrapper
- Defined in:
- lib/mongoid/locker/wrapper.rb
Overview
Set of methods to interact with the database.
Class Method Summary collapse
-
.current_mongodb_time(model) ⇒ Time?
Returns the local database server time in UTC.
-
.find_and_lock(doc, opts) ⇒ Hash?
Finds provided document with provided options, locks (sets
locking_name_field
andlocked_at_field
fields), and returns the fields. -
.find_and_unlock(doc, opts) ⇒ Boolean, ...
Finds provided document with provided options, unlocks (sets
locking_name_field
andlocked_at_field
fields tonil
). -
.locked_at(doc) ⇒ Time?
Returns value of
locked_at_field
field for provided document.
Class Method Details
.current_mongodb_time(model) ⇒ Time?
Returns the local database server time in UTC.
115 116 117 118 |
# File 'lib/mongoid/locker/wrapper.rb', line 115 def self.current_mongodb_time(model) info = model.collection.database.command(isMaster: 1) info.ok? ? info.documents.first['localTime'] : nil end |
.find_and_lock(doc, opts) ⇒ Hash?
Finds provided document with provided options, locks (sets locking_name_field
and locked_at_field
fields), and returns the fields.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mongoid/locker/wrapper.rb', line 21 def self.find_and_lock(doc, opts) model = doc.class filter = { _id: doc.id, '$or': [ { '$or': [ { model.locking_name_field => { '$exists': false } }, { model.locked_at_field => { '$exists': false } } ] }, { '$or': [ { model.locking_name_field => { '$eq': nil } }, { model.locked_at_field => { '$eq': nil } } ] }, { '$expr': { '$gte': ['$$NOW', { '$add': ["$#{model.locked_at_field}", model.lock_timeout * 1000] }] } # The expr means "Time.now.utc >= model.locked_at_field + model.lock_timeout * 1000" } ] } update = { '$set': { model.locking_name_field => opts[:locking_name] }, '$currentDate': { model.locked_at_field => true } } = { return_document: :after, projection: { _id: false, model.locking_name_field => true, model.locked_at_field => true }, write_concern: model.locker_write_concern } model.collection.find_one_and_update(filter, update, ) end |
.find_and_unlock(doc, opts) ⇒ Boolean, ...
Finds provided document with provided options, unlocks (sets locking_name_field
and locked_at_field
fields to nil
).
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mongoid/locker/wrapper.rb', line 69 def self.find_and_unlock(doc, opts) model = doc.class filter = { _id: doc.id, model.locking_name_field => opts[:locking_name] } update = { '$set': { model.locking_name_field => nil, model.locked_at_field => nil } } = { write_concern: model.locker_write_concern } result = model.collection.update_one(filter, update, ) result.ok? && result.written_count == 1 end |
.locked_at(doc) ⇒ Time?
Returns value of locked_at_field
field for provided document.
96 97 98 99 100 101 102 103 104 |
# File 'lib/mongoid/locker/wrapper.rb', line 96 def self.locked_at(doc) result = doc.class.collection.find( { _id: doc.id }, projection: { _id: false, doc.locked_at_field => true }, limit: 1 ).first result[doc.locked_at_field.to_s] if result end |