Module: Mongoid::Locker::Wrapper

Defined in:
lib/mongoid/locker/wrapper.rb

Overview

Set of methods to interact with the database.

Class Method Summary collapse

Class Method Details

.current_mongodb_time(model) ⇒ Time?

Returns the local database server time in UTC.

Examples:

Mongoid::Locker::Wrapper.current_mongodb_time(User)
#=> 2019-03-19 07:24:36 UTC

Parameters:

  • model (Class)

    the model class

Returns:

  • (Time)

    current time

  • (nil)

    if response was failed



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.

Examples:

Mongoid::Locker::Wrapper.find_and_lock(document, opts)
#=> {"locked_at"=>2019-03-19 07:51:24 UTC, "locking_name"=>nil}
Mongoid::Locker::Wrapper.find_and_lock(document, opts)
# => nil

Parameters:

  • doc (Mongoid::Document)
  • opts (Hash)

    (see #with_lock)

Returns:

  • (Hash)

    with locking_name_field and locked_at_field fields

  • (nil)

    if the document was not found, was already locked



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 } }
        ]
      },
      {
        '$where': "new Date() - this.#{model.locked_at_field} >= #{model.lock_timeout * 1000}"
      }
    ]
  }
  update = {
    '$set': { model.locking_name_field => opts[:locking_name] },
    '$currentDate': { model.locked_at_field => true }
  }
  options = {
    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, options)
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).

Examples:

Mongoid::Locker::Wrapper.find_and_unlock(doc, opts)
#=> true
Mongoid::Locker::Wrapper.find_and_unlock(doc, opts)
#=> false

Parameters:

  • doc (Mongoid::Document)
  • opts (Hash)

    (see #with_lock)

Returns:

  • (Boolean)
  • (true)

    if the document was unlocked

  • (false)

    if the document was not found, was not unlocked



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
    }
  }
  options = { write_concern: model.locker_write_concern }

  result = model.collection.update_one(filter, update, options)
  result.ok? && result.written_count == 1
end

.locked_at(doc) ⇒ Time?

Returns value of locked_at_field field for provided document.

Examples:

Mongoid::Locker::Wrapper.locked_at(document)
#=> 2019-06-03 13:50:46 UTC

Parameters:

  • doc (Mongoid::Document)

Returns:

  • (Time)

    locked_at_field field time

  • (nil)

    if response was failed



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