zjwshisb / process-manager
A simple php process manager
1.1.1
2024-10-25 06:02 UTC
Requires
- php: ^8.1
- ext-pcntl: *
- ext-posix: *
- monolog/monolog: ^3.7
- symfony/process: ^6.4
Requires (Dev)
- laravel/pint: ^1.18
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^10
README
This package provider an easy way to executes php callable in sub-processes with pcntl extension and executes commands in sub-processes with symfony/process package.
Example
Setting multiple process to consume job or run shell commands.
First, white a script like below.
// script.php $manager = new \Zjwshisb\ProcessManager\Manager(); $manager->setLogger(); // setting php callback $manager->spawnPhp(function () { $count = 1; // pseudocode // get job to do // for Prevent memory leakage, after reach 100 times, exit while ($count <= 100) { if ($job = Queue::getJob()) { $job->handle(); $count++; } else { sleep(1); } } }) // 10 processes to run ->setProcessCount(10); // processes will always restart after exit. ->setRuntime(0); // or execute shell commands, $manager->spawnCmd(["shell command"]) ->setProcessCount(10); ->setRuntime(0); // can call spawnPhp/spawnCmd multiple times to add different jobs. $manager->start();
Then, Run the script.php.
/path/to/php scipt.php
It is better to use supervisor to keep the script alive.
Supervisor config file like below.
[program:process-manager]
command=/path/to/php /path/to/script.php
autostart=true
autorestart=true
stderr_logfile=/path/to/stderr.log
stdout_logfile=/path/to/stdout.log
numprocs=1
Installation
Requirements
- php >= 8.1
- ext-pcntl
- ext-posix
- Linux/Macos
Composer
composer require zjwshisb/process-manager:1.0
Usage
Basic usage:
$manager = new \Zjwshisb\ProcessManager\Manager(); // or $manager = new \Zjwshisb\ProcessManager\Manager("PHP Process Manager", "/var/runtime/", 100 * 1000); // custom logger setting, any instance implement Psr\Log\LoggerInterface support // if null mean set to Monolog\Logger $manager->setLogger() // executes the php callback. $manager->spawnPhp(function () {return "hello world"}) // executes commands. $manager->spawnCmd(["echo", "hello world"]) // below methods only support in spawnCmd // ->setEnv() // ->setInput() // ->setWorkingDirectory() // start all process $manager->start();
Get success output:
// spawnCmd is the same usage. $manager = new \Zjwshisb\ProcessManager\Manager(); $manager->spawnPhp(function () {return "hello world"}) ->onSuccess(function (\Zjwshisb\ProcessManager\Process\PcntlProcess $process) { // this will be "hello world" $output = $process->getOutput(); }) $manager->start();
Get error output:
// spawnCmd is the same usage. $manager = new \Zjwshisb\ProcessManager\Manager(); $manager->spawnPhp(function () { throw new RuntimeException("hello world") }) ->onError(function (\Zjwshisb\ProcessManager\Process\PcntlProcess $process) { // this will be "hello world" $output = $process->getErrorOutput(); }) $manager->start();
Set timeout:
$manager = new \Zjwshisb\ProcessManager\Manager(); // set timeout, default 60 seconds. // set to 0 mean no timeout. // spawnCmd is the same usage. $manager->spawnPhp(function () {sleep(10);}) ->setTimeout(5) ->onTimeout(function () { //any things to do after timeout }) $manager->start();
Run multiple times:
$manager = new \Zjwshisb\ProcessManager\Manager(); // this will echo 1 10 times totally. // spawnCmd is the same usage. $manager->spawnPhp(function () {echo 1 . PHP_EOL;}) // set run times, default to 1 ->setRunTimes(10) $manager->start();
Run Always:
$manager = new \Zjwshisb\ProcessManager\Manager(); // this will echo 1 always util being stop by other signal. // spawnCmd is the same usage. $manager->spawnPhp(function () {echo 1 . PHP_EOL;}) // if value set to zero or negative, the callback will run infinitely. ->setRunTimes(0) $manager->start();
Set Multiple process:
$manager = new \Zjwshisb\ProcessManager\Manager(); // this will "echo 1" 10 times totally. // spawnCmd is the same usage. $manager->spawnPhp(function () {echo 1 . PHP_EOL;}) // set process count, default to 1. ->setProcessCount(10) $manager->start();
Multiple process run multiple times:
$manager = new \Zjwshisb\ProcessManager\Manager(); // this will "echo 1" 10*10 times totally. // spawnCmd is the same usage. $manager->spawnPhp(function () {echo 1 . PHP_EOL;}) // set process count, default to 1. ->setProcessCount(10) ->setRunTimes(10) $manager->start();
License
MIT