PHP code example of t3ran13 / php-process-manager

1. Go to this page and download the library: Download t3ran13/php-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/ */

    

t3ran13 / php-process-manager example snippets



namespace MyApp;

use ProcessManager\db\RedisManager;
use ProcessManager\ProcessManager;

$db = new RedisManager();

$pm = (new ProcessManager($db))
    ->setProcessName('MainProcess')
    ->setMaxRunningProcesses(2);
if ($pm->hasState()) {
    $pm->loadState();
} else {
    $pm->setPriority(25)
        ->setExecutionStep(1)
        ->setMaxNTriesOfRun(0)
        ->setSecondsBetweenRuns(60)
        ->setMaxLifetimeWithoutResults(30)
        ->saveState();
}

$BEP = (new BlockchainExplorerProcess($db))
    ->setProcessName('BlockchainExplorerProcess')
    ->generateIdFromProcessName();//have to be dane before interaction with state
if ($BEP->hasState()) {
    $BEP->loadState();
} else {
    $BEP->setPriority(30)
        ->setExecutionStep(1)
        ->setMaxNTriesOfRun(7)
        ->setSecondsBetweenRuns(60)
        ->setMaxLifetimeWithoutResults(30)
        ->saveState();
}

$test = new PostIsCreatedHandler($db);
if ($test->hasState()) {
    $test->loadState();
} else {
    $test->setProcessName('PostIsCreatedHandler')
        ->setPriority(35)
        ->setExecutionStep(1)
        ->setMaxNTriesOfRun(0)
        ->setSecondsBetweenRuns(3)
        ->setMaxLifetimeWithoutResults(6)
        ->saveState();
}

$pm->addProcess($BEP)
    ->addProcess($test);
$pm->start();



namespace MyApp;

use ProcessManager\db\RedisManager;
use ProcessManager\ProcessManager;

$db = new RedisManager();

$pm = (new ProcessManager($db))
    ->setProcessName('MainProcess')
    ->setMaxRunningProcesses(2)
    ->setPriority(25)
    ->setExecutionStep(1)
    ->setMaxNTriesOfRun(0)
    ->setSecondsBetweenRuns(60)
    ->setMaxLifetimeWithoutResults(30);

$BEP = (new BlockchainExplorerProcess($db))
    ->setProcessName('BlockchainExplorerProcess')
    ->setPriority(30)
    ->setExecutionStep(1)
    ->setMaxNTriesOfRun(7)
    ->setSecondsBetweenRuns(60)
    ->setMaxLifetimeWithoutResults(30);

$test = new PostIsCreatedHandler($db);
$test->setProcessName('PostIsCreatedHandler')
    ->setPriority(35)
    ->setExecutionStep(1)
    ->setMaxNTriesOfRun(0)
    ->setSecondsBetweenRuns(3)
    ->setMaxLifetimeWithoutResults(6)
    ->saveState();

$pm->addProcess($BEP)
    ->addProcess($test);
$pm->start();



namespace MyApp;

use ProcessManager\process\ProcessAbstract;

class MyProcess extends ProcessAbstract
{
    private   $isStopSignal = false;

    public function initSignalsHandlers()
    {
        pcntl_signal(SIGTERM, [$this, 'signalsHandlers']); //kill
        pcntl_signal(SIGINT, [$this, 'signalsHandlers']); //ctrl+c
        pcntl_signal(SIGHUP, [$this, 'signalsHandlers']); //restart process
    }

    public function signalsHandlers($signo, $signinfo)
    {
        switch ($signo) {
            case SIGINT:
            case SIGTERM:
            case SIGHUP:
                $this->isStopSignal = true;
                break;
            default:
        }
    }

    public function start()
    {
        echo PHP_EOL . date('Y-m-d H:i:s') . " {$this->getProcessName()} is started";

        while (!$this->isStopNeeded() && !$this->isStopSignal) {
            //some code
            pcntl_signal_dispatch();
        }
    }
    
}


namespace MyApp;

use ProcessManager\process\ProcessInterface;

class MyProcess implements ProcessInterface
{
    // your methods
}


namespace MyApp;

use ProcessManager\db\DBManagerInterface;

class MyDBManager implements DBManagerInterface
{
    public function newConnect(){
        // TODO: Implement newConnect() method.
    }
    public function updProcessStateById($id,$fields){
        // TODO: Implement updProcessStateById() method.
    }
    public function getProcessStateById($id,$field = null){
        // TODO: Implement getProcessStateById() method.
    }
    public function addErrorToList($id,string $error){
        // TODO: Implement addErrorToList() method.
    }
   
}