我希望能够查询Gearman服务器,以确定我正在运行多少个worker示例(基本上,我想确保如果没有处理这些任务的worker,则RunTaskA
和RunTaskB
可用,我希望能够发送警报)。
有没有办法做到这一点?
此外:如果您知道通过PHP查询Gearman服务器的方法,那就更好了。
编辑:我知道可以使用PHP Gearman扩展来提交任务,但我不需要这种扩展,我需要一种允许我查询Gearman服务器并查看多少个worker正在服务于特定任务的方法。
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;
}
}
为了快速检查,我使用这个 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”即可。
补充d5ve的回答,由于netcat将在套接字上等待,因此您可以添加一个-w参数并设置最大运行秒数。所以如果你正在查询localhost:
# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730 -w 1
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。
当一切都失败了,您可以使用Ubuntu中gearman-tools
软件包中找到的gearadmin
工具,并通过调用exec()
在新进程中执行它。这里是输出格式的参考。
这假设PHP和Gearman在同一台服务器上运行。