class Mutex::Mutex

@api private

Public Class Methods

add_static_bson_objectid(name, objectid) click to toggle source
# File lib/tengine/core/mutex.rb, line 66
def add_static_bson_objectid(name, objectid)
  static_bson_objectids[name] = objectid.is_a?(Moped::BSON::ObjectId) ? objectid :
    Moped::BSON::ObjectId.from_string(objectid.to_s)
end
clear_static_bson_objectids() click to toggle source
# File lib/tengine/core/mutex.rb, line 62
def clear_static_bson_objectids
  @static_bson_objectids = nil
end
find_or_create(name, ttl) click to toggle source
# File lib/tengine/core/mutex.rb, line 71
def find_or_create name, ttl
  id = Moped::BSON::ObjectId.legal?(name) ? name : static_bson_objectids[name] or
    raise ArgumentError, "Unknown name for BSON::ObjectId: #{name.inspect}"
  collection.find(_id: id).upsert(_id: id, ttl: ttl)
  return find(id)
end
static_bson_objectids() click to toggle source
# File lib/tengine/core/mutex.rb, line 58
def static_bson_objectids
  @static_bson_objectids ||= {}
end

Public Instance Methods

heartbeat(id) click to toggle source

attempt to refresh lock

# File lib/tengine/core/mutex.rb, line 110
def heartbeat id
  _update(
    { :_id => _id, "waiters._id" => id._id, },
    { "$set" => { "waiters.$.timeout" => Time.now + ttl, }, }
  )
end
invalidate() click to toggle source

delete stale locks

# File lib/tengine/core/mutex.rb, line 94
def invalidate
  # can this be done via standard mongoid queries?
  _update("$pull" => { :waiters => { :timeout => { "$lt" => Time.now, }, }, } )
end
lock(id) click to toggle source

attempt to gain lock

# File lib/tengine/core/mutex.rb, line 100
def lock id
  _update("$push" => { :waiters => { :_id => id._id, :timeout => Time.now + ttl, }, })
end
unlock(id) click to toggle source

attempt to release lock

# File lib/tengine/core/mutex.rb, line 105
def unlock id
  _update("$pull" => { :waiters => { :_id => id._id, }, })
end

Private Instance Methods

_update(q = {}) click to toggle source

暫定対応mongodbフェールオーバ中にtengine_resource_watchdが落ちてしまう

# File lib/tengine/core/mutex.rb, line 82
def _update q = {}, r
  update_in_safe_mode(
    self.class.collection,
    { :_id => _id, }.update(q),
    r
  )
  reload
end