有什么方法可以访问 Gearman 管理吗?

发布于 2024-08-30 16:05:05 字数 302 浏览 3 评论 0原文

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

有什么方法可以做到这一点吗?

另外:如果你知道查询 gearman 服务器的 PHP 方法,那就太疯狂了

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

I want to be able to query a gearman server to determine how many instances of a worker I have running (basically I want to make sure that RunTaskA is available and RunTaskB is available if there are no workers handling those tasks, I want to be able to send an alert out.

Is there any way to do this?

Also: Mad props if you know of a PHP way to query the gearman server.

Edit: I know about the PHP gearman extension that is available natively, but I am not looking for a task submission extension, I need something that allows me to query the gearman server and see how many workers are serving a specific task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

删除会话 2024-09-06 16:05:05
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;
    }

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

}
皇甫轩 2024-09-06 16:05:05

为了快速检查,我使用了这个 bash 单行代码:

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

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

我也对“workers”查询执行相同的操作,该查询显示所有连接的工作人员。

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

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

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

For quick checking, I use this bash one-liner:

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

This opens a connection to a gearman instance running on localhost, and sends the "status" query. This contains the name and number of jobs on that instance. The information can then be processed with grep/awk/wc etc. for reporting and alerting.

I also do the same with the "workers" query which shows all connected workers.

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

The sleep is to keep the connection open long enough for the reply.

The full list of administrative commands, and what the output means is at http://gearman.org/index.php?id=protocol Just search for "Administrative Protocol"

暗地喜欢 2024-09-06 16:05:05

为了扩展 d5ve 的答案,由于 netcat 将坐在套接字上等待,因此您可以添加一个 -w 参数以及运行的最大秒数。因此,如果您正在查询 localhost:

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

...,否则您将永远不会回到命令提示符。

To expand on d5ve's answer, since netcat will sit and wait on the socket, you can add a -w parameter with a maximum number of seconds to run. So if you're querying localhost:

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

... otherwise you never get back to a command prompt.

汹涌人海 2024-09-06 16:05:05

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

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

或者您可以使用 brian Moon 的 gearman manager

As far as i know there are no such extension in gearman, Managing and monitoring worker script is your responsibility you can try one of these for this purpose -

Supervisord is a python appliation for running application in background and monitoring them.

OR you can use brian moon's gearman manager

护你周全 2024-09-06 16:05:05

Gearman has a php extension. Have you looked into that yet?

PHP Link

复古式 2024-09-06 16:05:05

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

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

Stumbled on it today, haven't tested it myself but it looks promising.

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

玩心态 2024-09-06 16:05:05

在 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 的管理。

In Python you could do the following:

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)

Note: you have to install package named "gearman" using pip or easy_install to avoid any exceptions running the above code.

Also, Check the following admin clients that is simplifying administering gearman in general.

魂归处 2024-09-06 16:05:05

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

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

When everything else fails, you can use the gearadmin tool found in the gearman-tools package in Ubuntu by calling exec() to execute it in the new process. Here is a reference to its output format.

This assumes PHP and Gearman are running on the same server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文