Module: OsCtld::Manipulable
- 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
-
#acquire_manipulation_lock(by, block: false) ⇒ Boolean
Acquire the lock.
- #init_manipulable ⇒ Object
-
#is_being_manipulated? ⇒ Boolean
Check if any thread holds the lock.
-
#manipulate(by, block: false) { ... } ⇒ Object
Acquire a lock.
-
#manipulated_by ⇒ Object
Get lock holder.
-
#release_manipulation_lock ⇒ Object
Release the lock.
Instance Method Details
#acquire_manipulation_lock(by, block: false) ⇒ Boolean
Acquire the lock
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_manipulable ⇒ Object
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
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.
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_by ⇒ Object
Get lock holder
114 115 116 |
# File 'lib/osctld/manipulable.rb', line 114 def manipulated_by @manipulation_lock.holder end |
#release_manipulation_lock ⇒ Object
Release the lock
109 110 111 |
# File 'lib/osctld/manipulable.rb', line 109 def release_manipulation_lock @manipulation_lock.release end |