我希望能够查询一个gearman服务器来确定我运行的一个工人的实例数(基本上我想确保它RunTaskA
可用并且RunTaskB
如果没有工人处理这些任务就可用,我希望能够发送警惕.
有没有办法做到这一点?
另外:疯狂的道具,如果你知道一种PHP方式来查询gearman服务器.
编辑:我知道本地可用的PHP gearman扩展,但我不是在寻找任务提交扩展,我需要一些东西,允许我查询gearman服务器,看看有多少工人正在为特定任务服务.
小智 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;
}
}
Run Code Online (Sandbox Code Playgroud)
d5v*_*5ve 25
为了快速检查,我使用这个bash one-liner:
# (echo status ; sleep 0.1) | netcat 127.0.0.1 4730
Run Code Online (Sandbox Code Playgroud)
这将打开与localhost上运行的gearman实例的连接,并发送"status"查询.它包含该实例上的作业的名称和数量.然后可以使用grep/awk/wc等处理该信息以进行报告和警报.
我也对"工人"查询做了同样的事情,该查询显示了所有连接的工作人员.
# (echo workers ; sleep 0.1) | netcat 127.0.0.1 4730
Run Code Online (Sandbox Code Playgroud)
睡眠是为了保持连接打开足够长的时间以便回复.
管理命令的完整列表以及输出的含义http://gearman.org/index.php?id=protocol只需搜索"管理协议"