Sha256: a4e7aaec82430eae3b5cba24f2b5bfccbc32e38dcf67ad8534e04f8411e3063b

Contents?: true

Size: 1.7 KB

Versions: 15

Compression:

Stored size: 1.7 KB

Contents

module MotherBrain
  module Mixin
    # A mixin to provide easy access to creating, destroying, and executing within
    # locks in a running motherbrain application.
    module Locks
      extend Forwardable

      def_delegator "MB::LockManager.instance", :locks, :chef_locks
      def_delegator "MB::LockManager.instance", :find, :find_lock

      # 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]
      def chef_lock(options = {})
        find_or_new(options).lock
      end

      # 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]
      def chef_synchronize(options = {}, &block)
        ChefMutex.synchronize(options, &block)
      end

      # 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
      def chef_unlock(options = {})
        find_or_new(options).unlock
      end

      private

        def find_or_new(*args)
          find_lock(*args) || ChefMutex.new(*args)
        end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
motherbrain-1.5.0 lib/mb/mixin/locks.rb
motherbrain-1.4.0 lib/mb/mixin/locks.rb
motherbrain-1.3.0 lib/mb/mixin/locks.rb
motherbrain-1.2.1 lib/mb/mixin/locks.rb
motherbrain-1.2.0 lib/mb/mixin/locks.rb
motherbrain-1.1.3 lib/mb/mixin/locks.rb
motherbrain-1.1.2 lib/mb/mixin/locks.rb
motherbrain-1.1.1 lib/mb/mixin/locks.rb
motherbrain-1.1.0 lib/mb/mixin/locks.rb
motherbrain-1.0.0 lib/mb/mixin/locks.rb
motherbrain-0.14.5 lib/mb/mixin/locks.rb
motherbrain-0.14.4 lib/mb/mixin/locks.rb
motherbrain-0.14.3 lib/mb/mixin/locks.rb
motherbrain-0.14.2 lib/mb/mixin/locks.rb
motherbrain-0.13.1 lib/mb/mixin/locks.rb