有没有一种方法可以访问Gearman管理界面?

26

我希望能够查询Gearman服务器,以确定我正在运行多少个worker示例(基本上,我想确保如果没有处理这些任务的worker,则RunTaskARunTaskB可用,我希望能够发送警报)。

有没有办法做到这一点?

此外:如果您知道通过PHP查询Gearman服务器的方法,那就更好了。

编辑:我知道可以使用PHP Gearman扩展来提交任务,但我不需要这种扩展,我需要一种允许我查询Gearman服务器并查看多少个worker正在服务于特定任务的方法。

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

}

25

为了快速检查,我使用这个 Bash 单行命令:

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

这将打开到在本地主机上运行的Gearman实例的连接,并发送“status”查询。其中包含该实例上作业的名称和数量。可以使用grep/awk/wc等工具处理信息以用于报告和警报。

我还使用“workers”查询执行相同操作,该查询显示所有已连接的工作者。

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

这个睡眠操作是为了保持连接足够长的时间以等待回复。

完整的管理员命令列表以及输出含义,请访问http://gearman.org/index.php?id=protocol,搜索“Administrative Protocol”即可。


非常好,我喜欢这个解决方案的简单性。 - Mitch Dempsey

4

补充d5ve的回答,由于netcat将在套接字上等待,因此您可以添加一个-w参数并设置最大运行秒数。所以如果你正在查询localhost:

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

否则你永远无法返回命令提示符。

1
据我所知,Gearman 中没有这样的扩展,管理和监视工作脚本是您的责任,您可以尝试其中一个来实现此目的 -
Supervisord 是一个 Python 应用程序,用于在后台运行应用程序并监视它们。
或者,您可以使用 Brian Moon 的 Gearman Manager

我想我必须设计一种心跳系统。 - Mitch Dempsey

1

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。


0

1
那是用于提交任务的。我已经在使用它了,但该扩展程序并没有提供任何访问Gearman管理端的权限。 - Mitch Dempsey

0

当一切都失败了,您可以使用Ubuntu中gearman-tools软件包中找到的gearadmin工具,并通过调用exec()在新进程中执行它。这里是输出格式的参考

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


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接