AbstractOperations
is an abstract base class for all Docker cluster operation classes. It provides core functionality for performing operations on multiple Docker nodes simultaneously, with support for sequential and parallel execution, retry on failure, and various levels of error detail.
Sangezar\DockerClient\Cluster\Operations
public const EXECUTION_SEQUENTIAL = 'sequential';
public const EXECUTION_PARALLEL = 'parallel';
public const ERROR_LEVEL_BASIC = 'basic'; // Message only
public const ERROR_LEVEL_STANDARD = 'standard'; // Message + exception type + code
public const ERROR_LEVEL_DETAILED = 'detailed'; // All details, including stack trace
/** @var array<string, DockerClient> */
protected array $nodes;
/** @var string Execution strategy */
protected string $executionStrategy = self::EXECUTION_SEQUENTIAL;
/** @var string Error detail level */
protected string $errorDetailLevel = self::ERROR_LEVEL_STANDARD;
/** @var bool Allow automatic retries on failure */
protected bool $retryOnFailure = false;
/** @var int Maximum number of retries */
protected int $maxRetries = 3;
public function __construct(array $nodes, ?ClusterConfig $config = null)
Constructor.
$nodes
- Array of Docker API clients with node names as keys$config
- Cluster configuration (optional)MissingRequiredParameterException
- if the nodes array is emptyInvalidParameterValueException
- if parameters are invalidpublic function applyConfig(ClusterConfig $config): self
Applies cluster configuration.
$config
- Configuration to applypublic function setExecutionStrategy(string $strategy): self
Sets the execution strategy.
$strategy
- Execution strategy (EXECUTION_SEQUENTIAL or EXECUTION_PARALLEL)InvalidParameterValueException
- if an unknown strategy is specifiedpublic function setErrorDetailLevel(string $level): self
Sets the error detail level.
$level
- Detail level (ERROR_LEVEL_BASIC, ERROR_LEVEL_STANDARD or ERROR_LEVEL_DETAILED)InvalidParameterValueException
- if an unknown level is specifiedpublic function setRetryOnFailure(bool $enable, ?int $maxRetries = null): self
Sets retry on failure settings.
$enable
- Whether retries are allowed$maxRetries
- Maximum number of retries (default 3)InvalidParameterValueException
- if the number of retries is less than 1protected function executeOnAll(callable $operation): array
Executes an operation on all cluster nodes.
$operation
- Function to be executed on each nodeInvalidParameterValueException
- if the operation is not callablepublic function getNodes(): array
Gets all nodes.
DockerClient
instancespublic function isEmpty(): bool
Checks if the node collection is empty.
true
if the collection contains no nodes, false
otherwisepublic function count(): int
Counts the number of nodes.
public function addNode(string $name, DockerClient $client): self
Adds a new node to the collection.
$name
- Node name$client
- Docker API clientInvalidParameterValueException
- if the node name is empty or a node with this name already existspublic function removeNode(string $name): self
Removes a node from the collection.
$name
- Node namepublic function hasNode(string $name): bool
Checks if a node with the specified name exists.
$name
- Node nametrue
if the node exists, false
otherwiseprivate function executeSequential(callable $operation): array
Executes an operation sequentially on all cluster nodes.
$operation
- Function to be executed on each nodeprivate function executeParallel(callable $operation): array
Executes an operation in parallel on all cluster nodes.
$operation
- Function to be executed on each nodeprivate function formatError(\Throwable $e): array
Formats an error according to the set detail level.
$e
- Exception objectclass MyOperations extends AbstractOperations
{
public function perform(): array
{
return $this->executeOnAll(function (DockerClient $client) {
// Perform operation with the client
return $client->container()->list();
});
}
}
// Creating an instance
$nodes = [
'node1' => DockerClient::createTcp('tcp://192.168.1.10:2375'),
'node2' => DockerClient::createTcp('tcp://192.168.1.11:2375'),
];
$operations = new MyOperations($nodes);
// Performing operation on all nodes
$results = $operations->perform();
use Sangezar\DockerClient\Config\ClusterConfig;
use Sangezar\DockerClient\Cluster\Operations\AbstractOperations;
// Creating cluster configuration
$config = ClusterConfig::create()
->setExecutionStrategy(AbstractOperations::EXECUTION_PARALLEL)
->setErrorDetailLevel(AbstractOperations::ERROR_LEVEL_DETAILED)
->setRetryOnFailure(true, 5);
// Applying configuration to operations
$operations->applyConfig($config);
// Or individual configuration
$operations->setExecutionStrategy(AbstractOperations::EXECUTION_PARALLEL)
->setErrorDetailLevel(AbstractOperations::ERROR_LEVEL_DETAILED)
->setRetryOnFailure(true, 5);
// Adding a new node
$operations->addNode('node3', DockerClient::createTcp('tcp://192.168.1.12:2375'));
// Checking if a node exists
if ($operations->hasNode('node1')) {
echo "Node 'node1' exists\n";
}
// Removing a node
$operations->removeNode('node2');
// Getting a list of all nodes
$allNodes = $operations->getNodes();
// Checking the number of nodes
echo "Number of nodes: " . $operations->count() . "\n";