inhere/gearman

此包已被废弃,不再维护。未建议替代包。

一个 PHP gearman 工作者管理工具库

v1.0.0 2017-05-25 13:08 UTC

This package is auto-updated.

Last update: 2020-01-19 15:06:18 UTC


README

一个 PHP gearman 工作者管理器。

中文README

可以启动和管理多个 gearman 工作者,您可以设置每个工作者的最大执行时间和最大作业执行次数,达到设置值后。工作者将自动重启进程,防止进程死亡。

参考项目 brianlmoon/GearmanManager,非常感谢此项目。

添加一些特性

  • 代码更易于阅读和理解
  • 支持 start reload restart stop status 命令
  • 更多有用特性

仅支持 Linux 系统,且需要启用 pcntl posix

基本命令

  • start
// start
php bin/manager.php 
// run as daemon
php bin/manager.php --daemon 
  • stop
php bin/manager.php stop
  • restart
php bin/manager.php restart
  • 更多
// see help info
php bin/manager.php --help

// print manager config info
php bin/manager.php -D

// jobs status
php bin/manager.php status
php bin/manager.php status --cmd status

// workers status
php bin/manager.php status --cmd workers

命令和选项

使用 php examples/gwm.php -h,您可以看到所有命令和选项。

root@php5-dev:/var/www/phplang/library/gearman-manager# php examples/gwm.php -h
Gearman worker manager(gwm) script tool. Version 0.1.0

USAGE:
  php examples/gwm.php {COMMAND} -c CONFIG [-v LEVEL] [-l LOG_FILE] [-d] [-w] [-p PID_FILE]
  php examples/gwm.php -h
  php examples/gwm.php -D

COMMANDS:
  start             Start gearman worker manager(default)
  stop              Stop running's gearman worker manager
  restart           Restart running's gearman worker manager
  reload            Reload all running workers of the manager
  status            Get gearman worker manager runtime status

SPECIAL OPTIONS:
  start/restart
    -w,--watch         Automatically watch and reload when 'loader_file' has been modify
    -d,--daemon        Daemon, detach and run in the background
       --jobs          Only register the assigned jobs, multi job name separated by commas(',')
       --no-test       Not add test handler, when job name prefix is 'test'.(eg: test_job)

  status
    --cmd COMMAND      Send command when connect to the job server. allow:status,workers.(default:status)
    --watch-status     Watch status command, will auto refresh status.

PUBLIC OPTIONS:
  -c CONFIG          Load a custom worker manager configuration file
  -s HOST[:PORT]     Connect to server HOST and optional PORT, multi server separated by commas(',')

  -n NUMBER          Start NUMBER workers that do all jobs

  -u USERNAME        Run workers as USERNAME
  -g GROUP_NAME      Run workers as user's GROUP NAME

  -l LOG_FILE        Log output to LOG_FILE or use keyword 'syslog' for syslog support
  -p PID_FILE        File to write master process ID out to

  -r NUMBER          Maximum run job iterations per worker
  -x SECONDS         Maximum seconds for a worker to live
  -t SECONDS         Number of seconds gearmand server should wait for a worker to complete work before timing out

  -v [LEVEL]         Increase verbosity level by one. (eg: -v vv | -v vvv)

  -h,--help          Shows this help information
  -V,--version       Display the version of the manager
  -D,--dump [all]    Parse the command line and config file then dump it to the screen and exit.

添加处理器

您可以使用: function Closure Class/Object 添加作业处理器

类或对象必须是一个实现 __invoke() 方法的类,或者是一个实现 inhere\gearman\jobs\JobInterface 接口的类

  • 文件: job_handlers.php

/**
 * a class implement the '__invoke()'
 */
class TestJob
{
    public function __invoke($workload, \GearmanJob $job)
    {
        echo "from TestJob, call by __invoke";
    }
}

// add job handlers

$mgr->addHandler('reverse_string', function ($string, \GearmanJob $job)
{
    $result = strrev($string);

    echo "Result: $result\n";

    return $result;
});

$mgr->addHandler('test_job', TestJob::class);

// use a class implement the interface `inhere\gearman\jobs\JobInterface`, add some option for the job.
$mgr->addHandler('echo_job', \inhere\gearman\examples\jobs\EchoJob::class, [
    'worker_num' => 2,
    'focus_on' => 1,
]);
  • 继承 inhere\gearman\Job

/**
 * Class EchoJob
 * @package inhere\gearman\jobs
 */
class EchoJob extends Job
{
    /**
     * {@inheritDoc}
     */
    protected function doRun($workload, \GearmanJob $job)
    {
        echo "receive: $workload";
    }
}

启动管理器

使用 php gwm.php -h 查看更多信息

运行: php gwm.php

监控网络面板

您可以通过内置工具查看服务器、作业、工作者信息。

运行:

bash server.sh
// OR
php -S 127.0.0.1:5888 -t web

打开 URL http://127.0.0.1:5888

  • 服务器、作业信息

  • 查看日志

许可证

BSD