2012-11-05 81 views
4

我想檢查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); 
} 

但問題是,ping不拋出異常,它拋出一個致命的錯誤:

PHP Error[2]: GearmanClient::ping(): flush(GEARMAN_COULD_NOT_CONNECT) 127.0.0.1:4730 -> libgearman/connection.cc:673 

雖然奇怪的是,如果我刪除ping,並只使用runTasks,然後拋出異常。

相關:

如何處理錯誤時的Gearman守護程序出現故障,而進程正在運行?我從PHP以下錯誤,當我打倒的Gearman守護程序:

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) 

回答

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客戶工人應該是極度短暫的。您應該定期監視它們的內存使用情況並自動殺死它們。所以,如果你遇到過段錯誤/ coredump,殺死工作者是完全正常的。

如前所述,您可以使用Supervisor自動啓動備份您的員工。另外檢查一下,它解釋瞭如何獲得Gearman Clients working with Supervisor

相關問題