Module: OsCtld::Manipulable

Included in:
Container, Group, IdRange, Pool, Repository, User
Defined in:
lib/osctld/manipulable.rb

Overview

Adds support for manipulation locks.

Only one thread at a time can hold the lock. This locking mechanism should be used for longer running operations on osctld resources, such as containers or pools. Internal mechanisms should not depend on this lock.

#init_manipulable has to be called before the lock can be used.

Classes that include Manipulable can also define method `manipulable_resource` which is called to give users meaningful error messages. It returns an array with two elements: `[resource name, resource identification]`, e.g. for containers it is `[“container”, “<pool>:<ctid>”]`.

Classes that are manipulating other resources (are acquiring the locks) can define method `manipulation_holder` that returns a string identifying the operation that is holding the lock.

Defined Under Namespace

Classes: ManipulationLock

Instance Method Summary collapse

Instance Method Details

#acquire_manipulation_lock(by, block: false) ⇒ Boolean

Acquire the lock

Parameters:

  • by (Object)

    lock holder

  • block (Boolean) (defaults to: false)

    wait for the lock or fail if it is taken

Returns:

  • (Boolean)


115
116
117
# File 'lib/osctld/manipulable.rb', line 115

def acquire_manipulation_lock(by, block: false)
  @manipulation_lock.acquire(self, by, block: block)
end

#init_manipulableObject



94
95
96
# File 'lib/osctld/manipulable.rb', line 94

def init_manipulable
  @manipulation_lock = ManipulationLock.new
end

#is_being_manipulated?Boolean

Check if any thread holds the lock

Returns:

  • (Boolean)


130
131
132
# File 'lib/osctld/manipulable.rb', line 130

def is_being_manipulated?
  @manipulation_lock.locked?
end

#manipulate(by, block: false) { ... } ⇒ Object

Acquire a lock

Given block is executed with the lock held, the lock is then released and the block's return value is returned.

Parameters:

  • by (Object)

    lock holder

  • block (Boolean) (defaults to: false)

    wait for the lock or fail if it is taken

Yields:

  • block called with the lock held

Returns:

  • (Object)


107
108
109
# File 'lib/osctld/manipulable.rb', line 107

def manipulate(by, block: false, &codeblock)
  @manipulation_lock.acquire(self, by, block: block, &codeblock)
end

#manipulated_byObject

Get lock holder



125
126
127
# File 'lib/osctld/manipulable.rb', line 125

def manipulated_by
  @manipulation_lock.holder
end

#release_manipulation_lockObject

Release the lock



120
121
122
# File 'lib/osctld/manipulable.rb', line 120

def release_manipulation_lock
  @manipulation_lock.release
end