用于PHP的Gearman Worker管理工具GearmanManager

项目地址:https://github.com/brianlmoon/GearmanManager

PHP环境要求

  • PHP 5.5.9
  • POSIX extension
  • Process Control extension
  • pecl/gearman or Net_Gearman

使用GearmanManager的理由

运行Gearman的Worker是项比较让人讨厌的任务。千篇一律的代码...GearmanManager的目标是让运行worker成为一项运维性任务而不是开发任务。

文件名直接对应于Gearmand服务器中的function,这种方式极大简化了function在worker中的注册。

工作原理

我们创建一个比如叫“worker_dir”的目录来存放所有的worker代码。以下写法基于安装了pecl/gearman扩展的情形下的写法:

过程式的代码:

# cat worker_dir/example_function.php

function example_function($job, &$log) {

    $workload = $job->workload();

    // do work on $job here as documented in pecl/gearman docs

    // Log is an array that is passed in by reference that can be
    // added to for logging data that is not part of the return data
    $log[] = "Success";

    // return your result for the client
    return $result;

}

该文件的代码会在gearmand中注册一个名叫“example_function”的function。

面向对象式的代码:

# cat worker_dir/ExampleFunction.php

class ExampleFunction {

    public function run($job, &$log) {

        $workload = $job->workload();

        // do work on $job here as documented in pecl/gearman docs

        // Log is an array that is passed in by reference that can be
        // added to for logging data that is not part of the return data
        $log[] = "Success";

        // return your result for the client
        return $result;

    }

}

该文件的代码会在gearmand中注册一个名叫“ExampleFunction”的function。

更多特性

GearmanManager不止是让创建worker更简单,它还提供进程管理。如果进程挂掉,会重新启动。也可以设置让worker运行一段时间后销毁以免内存溢出。

GearmanManager有个选项可以设置监控worker目录,当新代码被部署时重启相应的worker。

在关闭GearmanManager时,它会允许worker进程完成任务的执行后退出。

高级内容

配置Worker

默认情况下,GearmanManager会确保至少有一个Worker并且会执行所有的任务。也就是默认只创建一个进程。在生产环境这很明显不够理想。

GearmanManager的ini配置文件中由几段内容组成。先有全局部分:[GearmanManager] ,剩下的针对每个function可以定义一段内容。

worker_dir - 定义worker function文件存放目录,可以通过逗号分隔的形式指定多个目录

include - 要注册到该服务器的function清单,表示包含worker中所有function,默认为

count - 该设置定义了用于执行所有funciton所需运行的最低worker数。例如如果设为10,将会启动10个进程,并注册所有的function。默认值为0。

dedicated_count - 该设置定义了专用于某一个function的进程数。比如你有5个function,设置dedicated_count = 2,即为每个function启动了两个进程,总共10个进程。默认值为1。

max_worker_lifetime - 设置每个worker进程的最大生命周期,单位为秒。当worker完成任务后,worker会检测它是否运行到了最大生命周期,如果到了就会退出。manager进程就好启动一个新进程并注册同一个任务来替代退出的进程。默认值为1个小时。

auto_update - 如果设置为1,manager进程会启动一个助手进程来监控worker存放目录的变更。如果发现新代码文件,会发送信号给父进程以便杀掉worker进程来加载新的代码。

对于每个注册function的worker,也有单独的针对设置项。

count - 设置一个整数值来确保至少有多少个worker注册了该function。因为每个进程可以注册多个function。

dedicated_count - 设置一个整数值来确保至少有多少个进程专用于该function。该进程不会做其它事情。

日志记录

命令行有很多选择,通过-h可以查看。

-v :

-v Logs only information about the start up and shutdown
-vv Logs information about process creation and exiting
-vvv Logs information about workers and the work they are doing
-vvvv Logs debug information
-vvvvv Logs crazy amounts of data about all manner of things

-l 指定日志文件存放目录。如果未指定,日志数据将被发送到stdout。也可以设置为syslog以便让日志数据发送到syslog中。

指定Gearmand服务器

两种方式可以指定服务器:在命令行或配置文件中。

命令行: -h [HOST[:PORT][,[HOST[:PORT]]]]。 例如: -h 10.1.1.1:4730,10.1.1.2:4730

配置文件:在 [GearmanManager] 全局段:

host - 10.1.1.1:4730,10.1.1.2:4730

; exclude - A list of workers in worker_dir to exclude ; host - The gearmand host ; log_file - Path to log file or syslog ; max_runs_per_worker - Maximum number of jobs a worker will process before restarting ; pid_file - Path to the file where the pid will be stored for the main process ; prefix - Prefix for class names used by workers ; user - System user to run as ; worker_restart_splay - Number of seconds to stagger restarting and launching of workers to prevent all workers dying a t once

运行守护进程

最简命令:

./pecl-manager.php -c /path/to/config.ini

还有其它一些参数:

-P - 管理进程的pid文件所在目录,也可以在配置文件中通过pid_file指定

-d - 如果在命令行指定该参数,管理器会以守护进程形式运行

-u - 指定守护进程的用户,也可在配置文件中通过user指定

调试

GearmanManager中用了(@),造成错误消息不提示,相应error_log是不会记录的,这样调试比较困难。

解决方案就是安装Xdebug:

  • Install Xdebug
  • Configure it
  • Profit!

安装调试工具

pecl install xdebug

配置调试工具

加上xdebug.scream参数到 xdebug.ini中:

zend_extension="/path/to/where/your/xdebug.so"
xdebug.scream = 1
xdebug.show_exception_trace = 1
原文地址:https://www.cnblogs.com/x3d/p/gearman-worker-manager.html