Testing to check if Gearman daemon is running

I want to check if the Gearman daemon is working. And only then run the tasks so that my application does not work.

Here is my code:

$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); } 

But the problem is that ping does not throw an exception, it causes a fatal error:

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

Although it is strange if I remove ping and use only runTasks , then an exception is thrown.

Connected:

How do I handle an error when the Gearman daemon drops while processes are running? I get the following error from PHP when I destroy the Gearman daemon:

 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) 
+4
source share
1 answer

At the same time, the simplest check of the Gearman server status can be performed through the command line using:

(echo status ; sleep 0.1) | nc 127.0.0.1 4730 -w 1

However, as noted in this question , you can use fsocketopen to get the status of the relay server.

  // Taken from /questions/205110/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; } } 

As for your second question, I could never restore the gearbox when the connection is lost in the middle. Basically, you have to kill the whole process executed by the work client and let the supervisor restart and restart another workflow. Gearman Clinic staff must be extremely ephemeral. You must regularly monitor them for memory use and automatically kill them. So, if you ever got into segfault / coredump, killing a worker is completely normal.

As already mentioned, you can automatically start your employees using Supervisor. Also check this, it explains how to get Gearman Clients working with Supervisor

+14
source

Source: https://habr.com/ru/post/1444065/


All Articles