GithubHelp home page GithubHelp logo

supervisor's Introduction

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.

Why?

Supervisor is useful for any application that needs to have long running PHP processes.

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.

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.

Example

<?php

// Some Worker Class

class SomeWorker
{
    public $keepWorking = true;

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

require __DIR__."/../../src/Supervisor.php";
require __DIR__."/SomeWorker.php";

use \Moonspot\Supervisor\Supervisor;

class MyApplication
{
    protected $super;

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

require __DIR__."/MyApplication.php";

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

supervisor's People

Contributors

brianlmoon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

renowncoder

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.