class Mongo::Lock::Drivers::Mongo

Attributes

lock[RW]

Public Class Methods

clear_expired(collection) click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 22
def self.clear_expired collection
  collection.remove expires_at: { '$lt' => Time.now }
end
ensure_indexes(collection) click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 13
def self.ensure_indexes collection
  collection.create_index([
    ['key', ::Mongo::ASCENDING],
    ['owner', ::Mongo::ASCENDING],
    ['expires_at', ::Mongo::ASCENDING]
  ])
  collection.create_index([['ttl', ::Mongo::ASCENDING]],{ expireAfterSeconds: 0 })
end
new(lock) click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 26
def initialize lock
  self.lock = lock
end
release_collection(collection, owner=nil) click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 8
def self.release_collection collection, owner=nil
  selector = if owner then { owner: owner } else {} end
  collection.remove(selector)
end

Public Instance Methods

find_already_acquired() click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 84
def find_already_acquired
  lock.configuration.collection.find({
    key: key,
    owner: lock.configuration.owner,
    expires_at: { '$gt' => Time.now }
  })
end
find_and_modify(options) click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 52
def find_and_modify options
  operation = options[:insert] ? '$setOnInsert' : '$set'
  existing_lock = lock.configuration.collection.find_and_modify({
    query: query,
    update: {
      operation => {
        key: key,
        owner: options[:owner],
        expires_at: options[:expire_at],
        ttl: options[:expire_at]
      }
    },
    upsert: !!options[:insert]
  })

  if existing_lock
    lock.expires_at = existing_lock['expires_at']
  else
    lock.expires_at = options[:expire_at]
  end

  existing_lock
end
find_and_update(time, options) click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 47
def find_and_update time, options
  options[:expire_at] = lock.expires_at + time
  find_and_modify options
end
find_existing() click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 92
def find_existing
  lock.configuration.collection.find(query).first
end
find_or_insert(options) click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 41
def find_or_insert options
  options[:expire_at] = Time.now + options[:expire_in]
  options[:insert] = true
  find_and_modify options
end
is_acquired?() click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 80
def is_acquired?
  find_already_acquired.count > 0
end
key() click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 30
def key
  lock.key
end
query() click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 34
def query
  {
    key: key,
    expires_at: { '$gt' => Time.now }
  }
end
remove(options) click to toggle source
# File lib/mongo-lock/drivers/mongo.rb, line 76
def remove options
  lock.configuration.collection.remove key: key, owner: options[:owner]
end