class Mongo::Lock::Drivers::Moped

Public Class Methods

clear_expired(collection) click to toggle source
# File lib/mongo-lock/drivers/moped.rb, line 16
def self.clear_expired collection
  collection.find(expires_at: { '$lt' => Time.now }).remove_all
end
ensure_indexes(collection) click to toggle source
# File lib/mongo-lock/drivers/moped.rb, line 11
def self.ensure_indexes collection
  collection.indexes.create({ key: 1, owner: 1, expires_at: 1 })
  collection.indexes.create({ ttl: 1 }, { expireAfterSeconds: 0 })
end
release_collection(collection, owner=nil) click to toggle source
# File lib/mongo-lock/drivers/moped.rb, line 6
def self.release_collection collection, owner=nil
  selector = if owner then { owner: owner } else {} end
  collection.find(selector).remove_all
end

Public Instance Methods

find_already_acquired() click to toggle source
# File lib/mongo-lock/drivers/moped.rb, line 47
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/moped.rb, line 20
def find_and_modify options
  operation = options[:insert] ? '$setOnInsert' : '$set'
  existing_lock = lock.configuration.collection.
    find(query).
    modify({
      operation => {
        key: key,
        owner: options[:owner],
        expires_at: options[:expire_at],
        ttl: options[:expire_at]
      }
    }, { upsert: !!options[:insert] })
  existing_lock = nil if existing_lock == {} # Moped returns {} for an empty result

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

  existing_lock
end
find_existing() click to toggle source
# File lib/mongo-lock/drivers/moped.rb, line 55
def find_existing
  lock.configuration.collection.find(query).first
end
remove(options) click to toggle source
# File lib/mongo-lock/drivers/moped.rb, line 43
def remove options
  lock.configuration.collection.find( key: key, owner: options[:owner] ).remove_all
end