【问题标题】:Testing to see if Gearman daemon is running测试 Gearman 守护进程是否正在运行
【发布时间】:2012-11-05 09:51:20
【问题描述】:

我想检查一下 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)

【问题讨论】:

    标签: php daemon gearman worker


    【解决方案1】:

    在最基本的情况下,检查 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 客户端工作人员应该是非常短暂的。您应该定期监视它们的内存使用情况并自动杀死它们。所以,如果你遇到了段错误/核心转储,杀死工人是完全正常的。

    如前所述,您可以使用 Supervisor 自动启动备份您的工作人员。也看看这个,它解释了如何获得Gearman Clients working with Supervisor

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 2023-03-02
      • 1970-01-01
      • 2017-11-25
      • 2013-04-16
      相关资源
      最近更新 更多