用于PHP的Gearman Worker管理工具GearmanManager

简介: 项目地址:https://github.com/brianlmoon/GearmanManagerPHP环境要求PHP 5.5.9POSIX extensionProcess Control extensionpecl/gearman or Net_Gearman使用GearmanManager的理由运行Gearman的Worker是项比较让人讨厌的任务。

项目地址: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 at 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
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
7月前
|
算法 PHP 数据安全/隐私保护
【实战】php goto解密工具,无需安装php环境,直接解密
php goto解密方法,php解密工具,goto解密,php微擎解密,代码可读性,代码可维护性
385 1
phpenv:PHP多版本安装和管理工具
phpenv:PHP多版本安装和管理工具
684 0
|
8月前
|
IDE NoSQL 关系型数据库
开发php,需要安装哪些工具
开发php,需要安装哪些工具
|
8月前
|
SQL 缓存 Linux
XHProf - PHP性能分析工具
XHProf - PHP性能分析工具的安装使用
114 1
|
4月前
|
数据采集 监控 JavaScript
巧用简单工具:PHP使用simple_html_dom库助你轻松爬取JD.com
本文将介绍如何使用PHP语言和一个简单的第三方库simple_html_dom来爬取JD.com的商品信息。simple_html_dom是一个轻量级的HTML解析器,它可以方便地从HTML文档中提取元素和属性,而无需使用正则表达式或DOM操作。本文将通过一个实例来展示如何使用simple_html_dom库来爬取JD.com的商品名称、价格、评分和评论数,并将结果保存到CSV文件中。本文还将介绍如何使用代理IP技术来避免被目标网站封禁或限制。
巧用简单工具:PHP使用simple_html_dom库助你轻松爬取JD.com
|
7月前
|
算法 PHP 数据安全/隐私保护
【实战】解决php中的goto加密解密,php在线解密工具
php goto解密方法,php goto在线解密工具
|
9月前
|
机器学习/深度学习 Linux 测试技术
Linux环境下面安装PHP性能分析工具XHProf(PHP5.6)
Linux环境下面安装PHP性能分析工具XHProf(PHP5.6)
164 0
|
9月前
|
Linux PHP
Linux环境下安装pecl工具,PHP扩展便捷安装
Linux环境下安装pecl工具,PHP扩展便捷安装
377 0
|
9月前
|
数据采集 前端开发 数据可视化
漏刻有时数据可视化大屏数据采集工具(1):调用微信jsAPI扫描条形码或二维码ajax返回传参PHP源代码
漏刻有时数据可视化大屏数据采集工具(1):调用微信jsAPI扫描条形码或二维码ajax返回传参PHP源代码
72 0
|
9月前
|
算法 PHP 数据安全/隐私保护