2017 © Pedro Peláez
 

library supervisor

Supervises forked processes

image

moonspot/supervisor

Supervises forked processes

  • Thursday, February 22, 2018
  • by brianlmoon
  • Repository
  • 2 Watchers
  • 5 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Supervisor - Manage a pool of PHP process

The Supervisor class will manage child process by taking a callback to be called after the child is forked. It will restart new children to replace ones that exit., (*1)

Why?

Supervisor is useful for any application that needs to have long running PHP processes., (*2)

Modern application development sometimes requires running long running PHP processes. Because Supervisor is PHP, it allows you to fork and manage processes that run existing PHP code without any modification., (*3)

One such case is managing Gearman workers. In fact, this project was inspired by how GearmanManager manages workers. The plan is to make it the code that handles the process management in future versions of GearmanManager., (*4)

Example

<?php

// Some Worker Class

class SomeWorker
{
    public $keepWorking = true;

    public function doWork()
    {
        while($this->keepWorking) {
            // do stuff
            usleep(500000);
        }
    }
}

```php <?php, (*5)

require DIR."/../../src/Supervisor.php"; require DIR."/SomeWorker.php";, (*6)

use \Moonspot\Supervisor\Supervisor;, (*7)

class MyApplication { protected $super;, (*8)

protected $worker;

public function __construct()
{
    $this->super = new Supervisor(
        array($this, "monitor"),
        array($this, "log"),
        array($this, "handleSignal")
    );

}

public function startWorkers($count)
{
    for($x=0; $x<$count; $x++){
        $this->super->addChild(
            array($this, "startWorker"),
            array(),
            3600000
        );
    }

    $this->super->wait();
}

public function startWorker()
{
    $this->worker = new SomeWorker();
    $this->worker->doWork();
}

public function handleSignal($signal) {
    $this->worker->keepWorking = false;
}

public function monitor()
{
    // we could call $this->super->stop() or $this->super->restart() here
}

public function log($log)
{
    list($sec, $ms) = explode(".", number_format(microtime(true), 3));
    echo "[".date("Y-m-d H:i:s").".$ms] $log\n";
}

}, (*9)

```php
<?php

require __DIR__."/MyApplication.php";

$app = new MyApplication();
$app->startWorkers(10);

The Versions

22/02 2018

dev-master

9999999-dev https://github.com/brianlmoon/Supervisor

Supervises forked processes

  Sources   Download

BSD BSD-3-Clause

The Requires

  • php >=5.4.0

 

supervisor fork threads pcntl_fork