bluepsyduck/symfony-process-manager

A process manager for Symfony processes, able to run them in parallel.

1.3.3 2021-12-03 21:30 UTC

This package is auto-updated.

Last update: 2022-01-03 21:41:16 UTC


README

GitHub release (latest SemVer) GitHub build Codecov

This package provides a simple process manager class to be able to execute multiple processes with a specified limit of parallel processes. The class expects the processes to use the Symfony Process component.

Usage

The usage of the process manager is straight forward and best explained with an example.

<?php
use BluePsyduck\SymfonyProcessManager\ProcessManager;
use Symfony\Component\Process\Process;

$numberOfParallelProcesses = 4; // The number of processes to execute in parallel.
$pollInterval = 100; // The interval to use for polling the processes, in milliseconds.
$processStartDelay = 0; // The time to delay the start of processes to space them out, in milliseconds.

$processManager = new ProcessManager($numberOfParallelProcesses, $pollInterval, $processStartDelay);

// Add some processes
// Processes get executed automatically once they are added to the manager. 
// If the limit of parallel processes is reached, they are placed in a queue and wait for a process to finish.
$processManager->addProcess(Process::fromShellCommandline('ls -l'));
$processManager->addProcess(Process::fromShellCommandline('ls -l'));

// Wait for all processes to finish
$processManager->waitForAllProcesses();

Callbacks

The process manager allows for some callbacks to be specified, which get called depending on the state of a process.

  • processStartCallback: Triggered before a process is started.
  • processFinishCallback: Triggered when a process has finished.
  • processTimeoutCallback: Triggered when a process timed out. Note that the processFinishCallback will be triggered afterwards as well.
  • processCheckCallback: Triggered whenever a process is checked for completion. Note that this callback is called periodically, but at least once, between the processStartCallback and the processFinishCallback or processTimeoutCallback respectively.

Each callback gets the process instance which triggered the event passed as only parameter. Here is an example of setting a processStartCallback:

<?php
use BluePsyduck\SymfonyProcessManager\ProcessManager;
use Symfony\Component\Process\Process;

$processManager = new ProcessManager();

$processManager->setProcessStartCallback(function (Process $process): void {
    echo 'Starting process: ' . $process->getCommandLine();
});

$processManager->addProcess(Process::fromShellCommandline('ls -l'));
$processManager->waitForAllProcesses();