25

我希望能够查询 gearman 服务器以确定我正在运行的工作人员的实例数(基本上我想确保它RunTaskA可用并且RunTaskB如果没有工作人员处理这些任务时可用,我希望能够发送发出警报。

有没有办法做到这一点?

另外:如果您知道查询 gearman 服务器的 PHP 方法,那么疯狂的道具。

编辑:我知道本地可用的 PHP gearman 扩展,但我不是在寻找任务提交扩展,我需要一些允许我查询 gearman 服务器并查看有多少工作人员正在为特定任务服务的东西。

4

9 回答 9

36
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;
    }

}
于 2010-05-27T18:30:21.790 回答
25

为了快速检查,我使用这个 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只需搜索“管理协议”

于 2010-06-03T10:59:47.907 回答
4

为了扩展 d5ve 的答案,由于 netcat 将坐在套接字上等待,您可以添加一个 -w 参数,其中包含最大运行秒数。因此,如果您要查询本地主机:

# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730 -w 1

...否则你永远不会回到命令提示符。

于 2011-04-29T21:40:23.750 回答
1

据我所知,在 gearman 中没有这样的扩展,管理和监控工作脚本是你的责任,你可以为此尝试其中一个 -

Supervisord 是一个 python 应用程序,用于在后台运行应用程序并监控它们。

或者你可以使用 brian moon 的gearman manager

于 2010-05-04T11:11:53.247 回答
1

我认为你需要这个 http://github.com/liorbk/php

于 2010-07-21T09:05:31.153 回答
0

Gearman 有一个 php 扩展。你有没有调查过?

PHP 链接

于 2010-05-02T05:59:47.890 回答
0

在 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。

于 2014-06-13T20:26:16.247 回答
0

今天偶然发现它,自己没有测试过,但看起来很有希望。

https://github.com/yugene/Gearman-Monitor

于 2013-07-25T21:48:49.783 回答
0

当其他一切都失败时,您可以通过调用在新进程中执行它来使用Ubuntu 包中的gearadmin工具。这是对其输出格式的参考gearman-toolsexec()

这假设 PHP 和 Gearman 在同一台服务器上运行。

于 2017-01-09T09:02:20.397 回答