You are here

protected function ProcessTrait::runCommands in Tome 8

Runs commands with concurrency.

Parameters

string[] $commands: An array of commands to execute.

int $concurrency: The number of concurrent processes to execute.

int $retry_count: The number of times to retry a failed command.

callback|\Closure $callback: (optional) A callback to invoke for each completed callback.

Return value

array An array of errors encountered when running commands.

4 calls to ProcessTrait::runCommands()
ExportCommand::execute in modules/tome_sync/src/Commands/ExportCommand.php
ImportCommand::deleteContent in modules/tome_sync/src/Commands/ImportCommand.php
Deletes content using sub-processes.
ImportCommand::importChunks in modules/tome_sync/src/Commands/ImportCommand.php
Imports chunks of content using sub-processes.
StaticCommand::exportPaths in modules/tome_static/src/Commands/StaticCommand.php
Exports the given paths to the static directory.

File

modules/tome_base/src/ProcessTrait.php, line 29

Class

ProcessTrait
Shared methods for running processes.

Namespace

Drupal\tome_base

Code

protected function runCommands(array $commands, $concurrency, $retry_count, $callback = NULL) {
  $current_processes = [];
  $collected_errors = [];
  $retry_callback = function (&$current_process) use (&$collected_errors, $retry_count) {

    /** @var \Symfony\Component\Process\Process $process */
    $process = $current_process['process'];
    $command = $process
      ->getCommandLine();
    if (!$process
      ->isRunning() && !$process
      ->isSuccessful() && $current_process['retry'] < $retry_count) {
      $collected_errors[] = "Retrying \"{$command}\" after failure...";
      $current_process['process'] = $process
        ->restart();
      ++$current_process['retry'];
    }
  };
  $filter_callback = function ($current_process) use (&$collected_errors, $callback) {

    /** @var \Symfony\Component\Process\Process $process */
    $process = $current_process['process'];
    $is_running = $process
      ->isRunning();
    $command = $process
      ->getCommandLine();
    if (!$is_running) {
      if (!$process
        ->isSuccessful()) {
        $error_output = $process
          ->getErrorOutput();
        $collected_errors[] = "Error when running \"{$command}\":\n  {$error_output}";
      }
      if ($callback) {
        call_user_func($callback, $current_process['process']);
      }
    }
    return $is_running;
  };
  while ($commands || $current_processes) {
    array_walk($current_processes, $retry_callback);
    $current_processes = array_filter($current_processes, $filter_callback);
    if ($commands && count($current_processes) < $concurrency) {
      $command = array_shift($commands);
      $process = new Process($command, isset($_SERVER['PWD']) ? $_SERVER['PWD'] : NULL);
      $process
        ->start();
      $current_processes[] = [
        'process' => $process,
        'retry' => 0,
      ];
    }
    usleep(50000);
  }
  return $collected_errors;
}