Silverstripe Version: 3.x
Check, if mysql connection is "alive"
I have a rabbitmq worker cli task that is daemonized by supervisord.
After a while there is an exception, because the database connection is not alive anymore
“Uncaught exception ‘SS_DatabaseException’ with message ‘Couldn’t run query: … MySQL server has gone away’ in …”.
Is there any chance to check, if database is still connected?
Tried to use code below with no luck
public function run($request) {
$max_minutes = 60;
$started_at = time();
if( $channel = T32RabbitMQConnector::getChannel($this->queueName) ) {
$channel->basic_consume($this->queueName, '', false, false, false, false, function($msg){
if( DB::isActive() === false ) {
print "DBConnection is not active - exiting\n";
exit();
}
$data_raw = $msg->body;
/** @var RabbitMQJob $jobObject */
$jobObject = unserialize(base64_decode($data_raw));
$result = $jobObject->execute();
if( $result === true ) {
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
}
});
while(count($channel->callbacks)) {
$channel->wait();
if( (time() - $started_at) / 60 > $max_minutes ) {
print "*************************************\n";
print "Exit after $max_minutes minutes\n";
print "*************************************\n";
break;
}
}
} else {
echo "Could not connect to queue\n";
}
}