docker-php-client

AbstractOperations Class Documentation

Description

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.

Namespace

Sangezar\DockerClient\Cluster\Operations

Constants

Execution Strategy Types

public const EXECUTION_SEQUENTIAL = 'sequential';
public const EXECUTION_PARALLEL = 'parallel';

Error Detail Levels

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

Properties

/** @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;

Methods

__construct

public function __construct(array $nodes, ?ClusterConfig $config = null)

Constructor.

Parameters:

Exceptions:

applyConfig

public function applyConfig(ClusterConfig $config): self

Applies cluster configuration.

Parameters:

Returns:

setExecutionStrategy

public function setExecutionStrategy(string $strategy): self

Sets the execution strategy.

Parameters:

Returns:

Exceptions:

setErrorDetailLevel

public function setErrorDetailLevel(string $level): self

Sets the error detail level.

Parameters:

Returns:

Exceptions:

setRetryOnFailure

public function setRetryOnFailure(bool $enable, ?int $maxRetries = null): self

Sets retry on failure settings.

Parameters:

Returns:

Exceptions:

executeOnAll

protected function executeOnAll(callable $operation): array

Executes an operation on all cluster nodes.

Parameters:

Returns:

Exceptions:

getNodes

public function getNodes(): array

Gets all nodes.

Returns:

isEmpty

public function isEmpty(): bool

Checks if the node collection is empty.

Returns:

count

public function count(): int

Counts the number of nodes.

Returns:

addNode

public function addNode(string $name, DockerClient $client): self

Adds a new node to the collection.

Parameters:

Returns:

Exceptions:

removeNode

public function removeNode(string $name): self

Removes a node from the collection.

Parameters:

Returns:

hasNode

public function hasNode(string $name): bool

Checks if a node with the specified name exists.

Parameters:

Returns:

Protected and Private Methods

executeSequential

private function executeSequential(callable $operation): array

Executes an operation sequentially on all cluster nodes.

Parameters:

Returns:

executeParallel

private function executeParallel(callable $operation): array

Executes an operation in parallel on all cluster nodes.

Parameters:

Returns:

formatError

private function formatError(\Throwable $e): array

Formats an error according to the set detail level.

Parameters:

Returns:

Usage Examples

Basic Usage in Inherited Classes

class 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();

Configuring Execution Strategy and Error Handling

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);

Managing Nodes During Execution

// 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";