PHP code example of zjwshisb / process-manager

1. Go to this page and download the library: Download zjwshisb/process-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zjwshisb / process-manager example snippets


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


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

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

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

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

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

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

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

$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();
shell
/path/to/php scipt.php