Hyd*_*erA 4 php daemon worker gearman
我想查看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);
}
Run Code Online (Sandbox Code Playgroud)
但问题是ping不会抛出异常,它会抛出一个致命的错误:
PHP Error[2]: GearmanClient::ping(): flush(GEARMAN_COULD_NOT_CONNECT) 127.0.0.1:4730 -> libgearman/connection.cc:673
Run Code Online (Sandbox Code Playgroud)
虽然奇怪的是,如果我删除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)
Run Code Online (Sandbox Code Playgroud)
Lay*_*yke 14
在最基本的情况下,可以通过命令行使用以下命令检查Gearman服务器的状态:
(echo status ; sleep 0.1) | nc 127.0.0.1 4730 -w 1
但是,如此问题中所述,您可以使用它fsocketopen来获取齿轮箱服务器的状态.
// 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;
}
}
Run Code Online (Sandbox Code Playgroud)
关于你的第二个问题,当连接丢失时,我从来没有能够找到一个齿轮工人.您基本上必须终止运行客户端工作程序的整个进程,并让主管收回并重新启动另一个工作进程.Gearman客户工作者应该是极端短暂的.您应该定期监视它们的内存使用情况并自动查杀它们.所以,如果你曾经遇到过segfault/coredump,那么杀死这名工人是完全正常的.
如上所述,您可以使用Supervisor自动启动工作人员.同时检查一下,它解释了如何让Gearman客户端与Supervisor一起工作