Class: Pvectl::Services::ContainerLifecycle
- Inherits:
-
Object
- Object
- Pvectl::Services::ContainerLifecycle
- Defined in:
- lib/pvectl/services/container_lifecycle.rb,
sig/pvectl/services/container_lifecycle.rbs
Overview
Orchestrates container lifecycle operations.
Handles execution of start/stop/shutdown/restart operations with sync/async modes, error handling, and result collection.
Constant Summary collapse
- SYNC_OPERATIONS =
i[start stop].freeze
- ASYNC_OPERATIONS =
i[shutdown restart].freeze
- ALL_OPERATIONS =
(SYNC_OPERATIONS + ASYNC_OPERATIONS).freeze
- DEFAULT_TIMEOUT =
60
Instance Method Summary collapse
-
#call_api(operation, container) ⇒ String
Calls the appropriate API method.
-
#execute(operation, containers) ⇒ Array<Models::ContainerOperationResult>
Executes a lifecycle operation on a list of containers.
-
#execute_single(operation, container) ⇒ Models::ContainerOperationResult
Executes operation on a single container.
-
#initialize(container_repository, task_repository, options = {}) ⇒ ContainerLifecycle
constructor
Creates a new ContainerLifecycle service.
-
#sync_mode?(operation) ⇒ Boolean
Determines if operation should run in sync mode.
-
#timeout ⇒ Integer
Returns configured timeout.
-
#validate_operation!(operation) ⇒ void
Validates operation is supported.
Constructor Details
#initialize(container_repository, task_repository, options = {}) ⇒ ContainerLifecycle
Creates a new ContainerLifecycle service.
27 28 29 30 31 |
# File 'lib/pvectl/services/container_lifecycle.rb', line 27 def initialize(container_repository, task_repository, = {}) @container_repository = container_repository @task_repository = task_repository = end |
Instance Method Details
#call_api(operation, container) ⇒ String
Calls the appropriate API method.
91 92 93 |
# File 'lib/pvectl/services/container_lifecycle.rb', line 91 def call_api(operation, container) @container_repository.send(operation, container.vmid, container.node) end |
#execute(operation, containers) ⇒ Array<Models::ContainerOperationResult>
Executes a lifecycle operation on a list of containers.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pvectl/services/container_lifecycle.rb', line 38 def execute(operation, containers) validate_operation!(operation) results = [] containers.each do |container| result = execute_single(operation, container) results << result break if [:fail_fast] && result.failed? end results end |
#execute_single(operation, container) ⇒ Models::ContainerOperationResult
Executes operation on a single container.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pvectl/services/container_lifecycle.rb', line 58 def execute_single(operation, container) task_upid = call_api(operation, container) if sync_mode?(operation) task = @task_repository.wait(task_upid, timeout: timeout) Models::ContainerOperationResult.new( container: container, operation: operation, task: task, success: task.successful? ) else Models::ContainerOperationResult.new( container: container, operation: operation, task_upid: task_upid, success: :pending ) end rescue StandardError => e Models::ContainerOperationResult.new( container: container, operation: operation, success: false, error: e. ) end |
#sync_mode?(operation) ⇒ Boolean
Determines if operation should run in sync mode.
99 100 101 102 103 104 |
# File 'lib/pvectl/services/container_lifecycle.rb', line 99 def sync_mode?(operation) return false if [:async] return true if [:wait] SYNC_OPERATIONS.include?(operation) end |
#timeout ⇒ Integer
Returns configured timeout.
109 110 111 |
# File 'lib/pvectl/services/container_lifecycle.rb', line 109 def timeout [:timeout] || DEFAULT_TIMEOUT end |
#validate_operation!(operation) ⇒ void
This method returns an undefined value.
Validates operation is supported.
117 118 119 120 121 |
# File 'lib/pvectl/services/container_lifecycle.rb', line 117 def validate_operation!(operation) return if ALL_OPERATIONS.include?(operation) raise ArgumentError, "Unknown operation: #{operation}. Valid: #{ALL_OPERATIONS.join(', ')}" end |