gpt4 book ai didi

php - 测试 Gearman 守护进程是否正在运行

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:56:51 24 4
gpt4 key购买 nike

我想检查 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 不会抛出异常,它会抛出一个 fatal error :

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)

最佳答案

在最基本的情况下,可以通过命令行使用以下命令检查 Gearman 服务器的状态:

( echo 状态; sleep 0.1)|数控 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 worker。您基本上必须终止运行客户端 worker 的整个进程,然后让主管接管并重新启动另一个工作进程。Gearman 客户端工作人员应该非常短暂。您应该定期监视它们的内存使用情况并自动杀死它们。所以,如果你遇到段错误/核心转储,杀死 worker 是完全正常的。

如前所述,您可以使用 Supervisor 自动启动备份您的工作人员。还要检查一下,它解释了如何获得 Gearman Clients working with Supervisor

关于php - 测试 Gearman 守护进程是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13229566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com