定制 moonspot/supervisor 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

moonspot/supervisor

Composer 安装命令:

composer require moonspot/supervisor

包简介

Supervises forked processes

README 文档

README

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

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-03-14