我想查看Gearman守护程序是否正在运行。然后才运行任务,这样我的应用程序就不会崩溃。
这是我的代码:
$daemonRunning = true;
while( true )
{
try
{
Yii::app()->gearman->client->ping( true );
if ( $daemonRunning === false )
{
echo "Daemon back online. Starting signature process...\n";
}
Yii::app()->gearman->client->runTasks();
}
catch( GearmanException $e )
{
echo "Daemon appears to be down. Waiting for it to come back up...\n";
$daemonRunning = false;
}
sleep(1);
}
但问题是ping
没有抛出异常,它会引发致命错误:
PHP Error[2]: GearmanClient::ping(): flush(GEARMAN_COULD_NOT_CONNECT) 127.0.0.1:4730 -> libgearman/connection.cc:673
虽然很奇怪,如果我删除ping
并仅使用runTasks
,则会抛出异常。
相关:
当进程运行时Gearman守护程序出现故障时,如何处理错误?当我关闭Gearman守护进程时,我从PHP得到以下错误:
php: libgearman/universal.cc:481: gearman_return_t connection_loop(gearman_universal_st&, const gearman_packet_st&, Check&): Assertion `&con->_packet == universal.packet_list' failed.
Aborted (core dumped)
答案 0 :(得分:14)
最基本的是,检查Gearman服务器的状态可以通过命令行使用:
(echo status ; sleep 0.1) | nc 127.0.0.1 4730 -w 1
然而,作为noted in this question,您可以使用fsocketopen
来获取gearman服务器的状态。
// Taken from https://stackoverflow.com/questions/2752431/any-way-to-access-gearman-administration
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;
}
}
关于你的第二个问题,当连接丢失时,我从未能够找到一名齿轮工人。您基本上必须终止运行客户端工作程序的整个进程,并让主管收回并重新启动另一个工作进程。 Gearman客户工作者应该是极端短暂的。您应该定期监视它们的内存使用情况并自动查杀它们。所以,如果你曾经遇到过segfault / coredump,那么杀死这名工人是完全正常的。
如上所述,您可以使用Supervisor自动启动工作人员备份。另外,请查看此问题,它解释了如何获取Gearman Clients working with Supervisor