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)


104
105
106
# File 'lib/osctld/manipulable.rb', line 104

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

#init_manipulableObject



83
84
85
# File 'lib/osctld/manipulable.rb', line 83

def init_manipulable
  @manipulation_lock = ManipulationLock.new
end

#is_being_manipulated?Boolean

Check if any thread holds the lock

Returns:

  • (Boolean)


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

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)


96
97
98
# File 'lib/osctld/manipulable.rb', line 96

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

#manipulated_byObject

Get lock holder



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

def manipulated_by
  @manipulation_lock.holder
end

#release_manipulation_lockObject

Release the lock



109
110
111
# File 'lib/osctld/manipulable.rb', line 109

def release_manipulation_lock
  @manipulation_lock.release
end