PHP Process Manager [v0.1.0]

in #utopian-io5 years ago (edited)

php-process-manager

Process manager is lib for flexible and easy control of processes on PHP. It is easy to add one file to cron with will start and stop other processes acording to settings. Flexible and easy tool.

Github or packagist with MIT license. Author @t3ran13

php-process-manager

Process manager on PHP

Install Via Composer

composer require t3ran13/php-process-manager

Basic Usage

It example of usege with saving process state to db

<?php
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');
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();

or without state saving

<?php
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();

Process creation

It is easy creation of own process with ready abstact class or with interface

<?php
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();
        }
    }
    
}

or you can create with interface more flexible

<?php
namespace MyApp;

use ProcessManager\process\ProcessInterface;

class MyProcess implements ProcessInterface
{
    // your methods
}

DB manager

Process manager has ready DB manager realization for working with Redis and has next DB structure:

- DB0
    - {keyPrefix}:{id}:className
    - {keyPrefix}:{id}:processName
    - {keyPrefix}:{id}:priority
    - {keyPrefix}:{id}:pid
    - {keyPrefix}:{id}:executionStep
    - {keyPrefix}:{id}:isRunning
    - {keyPrefix}:{id}:nTriesOfRun
    - {keyPrefix}:{id}:maxNTriesOfRun
    - {keyPrefix}:{id}:secondsBetweenRuns
    - {keyPrefix}:{id}:maxLifetimeWithoutResults
    - {keyPrefix}:{id}:lastUpdateDatetime
    - {keyPrefix}:{id}:data:*
    - {keyPrefix}:{id}:errors:*
    
    - {keyPrefix}:listeners:{id}:last_update_datetime
    - {keyPrefix}:listeners:{id}:status
    - {keyPrefix}:listeners:{id}:mode
    - {keyPrefix}:listeners:{id}:pid
    - {keyPrefix}:listeners:{id}:handler
    - {keyPrefix}:listeners:{id}:data:last_block
    - {keyPrefix}:listeners:{id}:conditions:{n}:key
    - {keyPrefix}:listeners:{id}:conditions:{n}:value
    
    - {keyPrefix}:events:{listener_id}:{block_n}:{trx_n_in_block}
    

Or you can create own DB manager

<?php
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.
    }
   
}

It is better with each commit

Commits were done by me in master branch

  • init (last 14 days)
  • fixes (last 14 days)
  • upd composer.json (last 14 days)
  • fix saving of data and errors in RedisManager (last 14 days)
  • fix method name for errors logging (last 14 days)
  • init README (last 14 days)
  • MIT LICENSE (last 14 days)
  • upd Readme (last 14 days)
Sort:  

Thank you for your contribution. It's good to see a new project from you, but it would have been better if you could write more about the Project like, what does Process manager on PHP does, how it is different than other process managers etc. It would also be better to have more meaningful commit messages than init, fixes.


Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thx for feedback
I will fix it a bit later

Thank you for your review, @codingdefined! Keep up the good work!

hi! i did not got upvote(

Hi @t3ran13!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

As a follower of @followforupvotes this post has been randomly selected and upvoted! Enjoy your upvote and have a great day!

Coin Marketplace

STEEM 0.32
TRX 0.11
JST 0.031
BTC 67344.10
ETH 3749.25
USDT 1.00
SBD 3.69