bluepsyduck / symfony-process-manager
A process manager for Symfony processes, able to run them in parallel.
Installs: 622 786
Dependents: 5
Suggesters: 0
Security: 0
Stars: 10
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- symfony/process: ^3.0 || ^4.0 || ^5.0 || ^6.0
Requires (Dev)
- bluepsyduck/test-helper: ^1.0
- phpstan/phpstan: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^8.0 || ^9.0
- rregeer/phpunit-coverage-check: ^0.3
- squizlabs/php_codesniffer: ^3.3
README
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 theprocessFinishCallback
orprocessTimeoutCallback
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();