简体   繁体   English

如何获得Gearman中特定类型的排队作业数?

[英]How can I get the number of queued jobs of a particular type in gearman?

I have a number of gearman clients sending a job, say job1. 我有许多齿轮工客户正在发送作业,例如job1。

$client = new GearmanClient();
$client->addServer();
$client->doBackground('job1', 'workload');

It takes, say 10 seconds to process this job. 处理该作业大约需要10秒钟。 I want to track how many 'job1' jobs are waiting for a worker to work on them at any given time. 我想跟踪有多少个“ job1”工作正在等待工人在任何给定时间工作。 How can I do that? 我怎样才能做到这一点?

For quick checking, I use this bash one-liner: 为了快速检查,我使用了这种bash一线式:

(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. 这将打开与在localhost上运行的gearman实例的连接,并发送状态查询。 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. 然后可以使用grep / awk / wc等处理该信息,以进行报告和警报。

I also do the same with the workers query which shows all connected workers. 我也对显示所有已连接工作程序的worker查询进行了同样的操作。

(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/protocol/ . 管理命令的完整列表以及输出的含义位于http://gearman.org/protocol/ Just search for "Administrative Protocol". 只需搜索“管理协议”。

要扩展d5ve的答案,请添加-w参数以“超时”您的netcat连接,否则,您将永远不会回到命令提示符。

$ (echo status ; sleep 0.1) | sudo netcat 127.0.0.1 4730 -w 1
telnet localhost 4730
status

worker_name total_queue currently_running number_of_workers
job1         1          1                 9

I use gearman_top , which is part of mod-gearman . 我使用gearman_top ,它是mod-gearman的一部分。

Example output from the website: 网站的示例输出:

+-----------------------+--------+-------+-------+---------+
| Name                  | Worker | Avail | Queue | Running |
+-----------------------+--------+-------+-------+---------+
| check_results         | 1      | 1     | 0     | 0       |
| host                  | 3      | 3     | 0     | 0       |
| service               | 3      | 3     | 0     | 0       |
| eventhandler          | 3      | 3     | 0     | 0       |
| servicegroup_jmx4perl | 3      | 3     | 0     | 0       |
| hostgroup_japan       | 3      | 3     | 0     | 0       |
+-----------------------+--------+-------+-------+---------+

It doesn't look like there are any immediate ways to get this information. 似乎没有任何直接方法可以获取此信息。

Here are a few options. 这里有一些选择。 First, if you can grab job handles as you create them (search for "Speaking of checking the status"), you can store them away in some central place and query about them from any client. 首先, 如果在创建作业句柄时可以抓住它们 (搜索“说到检查状态”),则可以将它们存储在某个中央位置并从任何客户端查询它们。

Second, you can set your Gearman server to use persistent queues , and then run a query against the queue yourself. 其次,您可以将Gearman服务器设置为使用持久性队列 ,然后自己对队列运行查询。 This might be the easier and cleaner of the two options. 这可能是这两种选择中更简单,更干净的一种。

Gearmand has a telnet interface you can query. Gearmand具有可查询的telnet界面。 (the exact details of the protocol can be found on the gearman website - http://gearman.org/?id=protocol ) (该协议的具体细节可在Gearman的网站上找到- http://gearman.org/?id=protocol

I used this code here as a starting point for rolling my own. 我在这里使用此代码作为滚动自己的代码的起点。 https://github.com/liorbk/php/blob/master/GearmanTelnet.php (this code is perfectly good by itself and you should be able to drop use it out of the box) https://github.com/liorbk/php/blob/master/GearmanTelnet.php (此代码本身非常好,您应该可以立即使用它)

It's a less than pretty solution but until someone improves gearman admin interface so you can talk directly via PHP or writes a plugin for it, you are on your own 这不是一个不错的解决方案,但是直到有人改进了Gearman的管理界面,以便您可以直接通过PHP进行交谈或为其编写插件之前,您还是自己决定

On Ubuntu 18.04 I have the gearadmin binary installed by default with the gearman package. 在Ubuntu 18.04上,默认情况下,我已使用gearman软件包安装了gearadmin二进制文件。

gearadmin --help
gearadmin --status can be used instead of the netcat alternative: gearadmin --status可以代替netcat替代:

gearadmin --status
queue1    334     10      10

And a one liner: 和一个班轮:
while :; do gearadmin --status; sleep 1; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM