我希望能够查询 gearman 服务器以确定我正在运行的工作人员的实例数(基本上我想确保它RunTaskA
可用并且RunTaskB
如果没有工作人员处理这些任务时可用,我希望能够发送发出警报。
有没有办法做到这一点?
另外:如果您知道查询 gearman 服务器的 PHP 方法,那么疯狂的道具。
编辑:我知道本地可用的 PHP gearman 扩展,但我不是在寻找任务提交扩展,我需要一些允许我查询 gearman 服务器并查看有多少工作人员正在为特定任务服务的东西。
class Waps_Gearman_Server {
/**
* @var string
*/
protected $host = "127.0.0.1";
/**
* @var int
*/
protected $port = 4730;
/**
* @param string $host
* @param int $port
*/
public function __construct($host=null,$port=null){
if( !is_null($host) ){
$this->host = $host;
}
if( !is_null($port) ){
$this->port = $port;
}
}
/**
* @return array | null
*/
public function getStatus(){
$status = null;
$handle = fsockopen($this->host,$this->port,$errorNumber,$errorString,30);
if($handle!=null){
fwrite($handle,"status\n");
while (!feof($handle)) {
$line = fgets($handle, 4096);
if( $line==".\n"){
break;
}
if( preg_match("~^(.*)[ \t](\d+)[ \t](\d+)[ \t](\d+)~",$line,$matches) ){
$function = $matches[1];
$status['operations'][$function] = array(
'function' => $function,
'total' => $matches[2],
'running' => $matches[3],
'connectedWorkers' => $matches[4],
);
}
}
fwrite($handle,"workers\n");
while (!feof($handle)) {
$line = fgets($handle, 4096);
if( $line==".\n"){
break;
}
// FD IP-ADDRESS CLIENT-ID : FUNCTION
if( preg_match("~^(\d+)[ \t](.*?)[ \t](.*?) : ?(.*)~",$line,$matches) ){
$fd = $matches[1];
$status['connections'][$fd] = array(
'fd' => $fd,
'ip' => $matches[2],
'id' => $matches[3],
'function' => $matches[4],
);
}
}
fclose($handle);
}
return $status;
}
}
为了快速检查,我使用这个 bash one-liner:
# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730
这将打开到在 localhost 上运行的 gearman 实例的连接,并发送“状态”查询。这包含该实例上作业的名称和数量。然后可以使用 grep/awk/wc 等处理信息以进行报告和警报。
我也对显示所有已连接工作人员的“工作人员”查询执行相同操作。
# (echo workers ; sleep 0.1) | netcat 127.0.0.1 4730
睡眠是为了保持连接打开足够长的时间来回复。
管理命令的完整列表以及输出的含义在http://gearman.org/index.php?id=protocol只需搜索“管理协议”
为了扩展 d5ve 的答案,由于 netcat 将坐在套接字上等待,您可以添加一个 -w 参数,其中包含最大运行秒数。因此,如果您要查询本地主机:
# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730 -w 1
...否则你永远不会回到命令提示符。
据我所知,在 gearman 中没有这样的扩展,管理和监控工作脚本是你的责任,你可以为此尝试其中一个 -
Supervisord 是一个 python 应用程序,用于在后台运行应用程序并监控它们。
或者你可以使用 brian moon 的gearman manager
我认为你需要这个 http://github.com/liorbk/php
Gearman 有一个 php 扩展。你有没有调查过?
在 Python 中,您可以执行以下操作:
import gearman
admin_client = gearman.GearmanAdminClient(['127.0.0.1:4730',])
status = admin_client.get_status()
for w in status:
if w["task"] == "YOUR_TASK_NAME":
print(w)
注意:您必须使用 pip 或 easy_install 安装名为“gearman”的包,以避免运行上述代码的任何异常。
此外,请检查以下管理客户端,它们总体上简化了管理 gearman。
今天偶然发现它,自己没有测试过,但看起来很有希望。
当其他一切都失败时,您可以通过调用在新进程中执行它来使用Ubuntu 包中的gearadmin
工具。这是对其输出格式的参考。gearman-tools
exec()
这假设 PHP 和 Gearman 在同一台服务器上运行。