module MotherBrain::Mixin::Locks

A mixin to provide easy access to creating, destroying, and executing within locks in a running motherbrain application.

Public Instance Methods

chef_lock(options = {}) click to toggle source

Attempts to create a lock. Fails if the lock already exists.

@see ChefMutex#initialize

@raise [MotherBrain::InvalidLockType] if the lock type is invalid @raise [MotherBrain::ResourceLocked] if the lock is unobtainable

@return [Boolean]

# File lib/mb/mixin/locks.rb, line 19
def chef_lock(options = {})
  find_or_new(options).lock
end
chef_synchronize(options = {}, &block) click to toggle source

Creates a new ChefMutex on the given resource and runs the given block inside of it. The lock is released when the block completes.

@see ChefMutex#initialize

@raise [MotherBrain::InvalidLockType] if the lock type is invalid @raise [MotherBrain::ResourceLocked] if the lock is unobtainable

@return [Boolean]

# File lib/mb/mixin/locks.rb, line 32
def chef_synchronize(options = {}, &block)
  ChefMutex.synchronize(options, &block)
end
chef_unlock(options = {}) click to toggle source

Attempts to unlock the lock. Fails if the lock doesn’t exist, or if it is held by someone else

@see ChefMutex#initialize

@raise [MotherBrain::InvalidLockType] if the lock type is invalid @raise [MotherBrain::ResourceLocked] if the lock is owned by someone else

# File lib/mb/mixin/locks.rb, line 43
def chef_unlock(options = {})
  find_or_new(options).unlock
end

Private Instance Methods

find_or_new(*args) click to toggle source
# File lib/mb/mixin/locks.rb, line 49
def find_or_new(*args)
  find_lock(*args) || ChefMutex.new(*args)
end