有没有办法访问Gearman管理?

时间:2010-05-02 04:18:08

标签: php gearman

我希望能够查询一个gearman服务器,以确定我运行的工人的实例数(基本上我想确保RunTaskA可用,RunTaskB可用没有工人处理这些任务,我希望能够发出警报。

有没有办法做到这一点?

另外:如果你知道用PHP查询gearman服务器的方式,那就是Mad props。

编辑:我知道本机可用的PHP gearman扩展,但我不是在寻找任务提交扩展,我需要一些东西可以让我查询gearman服务器并查看有多少工人正在完成一项特定任务。

9 个答案:

答案 0 :(得分: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;
    }

}

答案 1 :(得分:25)

为了快速检查,我使用这个bash one-liner:

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

这将打开与localhost上运行的gearman实例的连接,并发送“status”查询。它包含该实例上的作业的名称和数量。然后可以使用grep / awk / wc等处理信息以进行报告和警报。

我也对显示所有连接工作人员的“工人”查询也这样做。

# (echo workers ; sleep 0.1) | netcat 127.0.0.1 4730

睡眠是为了保持连接打开足够长的时间以便回复。

管理命令的完整列表,以及输出在http://gearman.org/index.php?id=protocol的含义仅搜索“管理协议”

答案 2 :(得分:4)

要扩展d5ve的答案,因为netcat将在套接字上等待,你可以添加一个带有最大秒数的-w参数来运行。所以如果你在查询localhost:

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

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

答案 3 :(得分:1)

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

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

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

答案 4 :(得分:1)

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

答案 5 :(得分:0)

Gearman has a php extension。你看过那个吗?

PHP Link

答案 6 :(得分:0)

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

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

答案 7 :(得分: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)

注意:您必须安装名为" gearman"使用pip或easy_install来避免运行上述代码的任何异常。

另外,请检查以下管理客户端,这样可以简化管理装备。

答案 8 :(得分:0)

当其他所有方法都失败时,您可以使用Ubuntu中gearadmin包中的gearman-tools工具,通过调用exec()在新进程中执行它。这是a reference to its output format

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