mirror of
https://github.com/elyby/accounts.git
synced 2024-12-25 22:59:53 +05:30
Добавлено восстановление соединения для AMQP воркера
This commit is contained in:
parent
4bebf6c581
commit
e6fe2f3755
@ -2,19 +2,21 @@
|
|||||||
namespace console\controllers;
|
namespace console\controllers;
|
||||||
|
|
||||||
use Ely\Amqp\ControllerTrait;
|
use Ely\Amqp\ControllerTrait;
|
||||||
|
use Exception;
|
||||||
use PhpAmqpLib\Message\AMQPMessage;
|
use PhpAmqpLib\Message\AMQPMessage;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\console\Controller;
|
use yii\console\Controller;
|
||||||
use yii\db\Exception as YiiDbException;
|
use yii\db\Exception as YiiDbException;
|
||||||
use yii\helpers\ArrayHelper;
|
use yii\helpers\ArrayHelper;
|
||||||
use yii\helpers\Inflector;
|
use yii\helpers\Inflector;
|
||||||
use yii\helpers\StringHelper;
|
|
||||||
|
|
||||||
abstract class AmqpController extends Controller {
|
abstract class AmqpController extends Controller {
|
||||||
use ControllerTrait {
|
use ControllerTrait {
|
||||||
callback as _callback;
|
callback as _callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private $reconnected = false;
|
||||||
|
|
||||||
public final function actionIndex() {
|
public final function actionIndex() {
|
||||||
$this->start();
|
$this->start();
|
||||||
}
|
}
|
||||||
@ -35,12 +37,17 @@ abstract class AmqpController extends Controller {
|
|||||||
try {
|
try {
|
||||||
$this->_callback($msg);
|
$this->_callback($msg);
|
||||||
} catch (YiiDbException $e) {
|
} catch (YiiDbException $e) {
|
||||||
if (StringHelper::startsWith($e->getMessage(), 'Error while sending QUERY packet')) {
|
if ($this->reconnected || !$this->isRestorableException($e)) {
|
||||||
exit(self::EXIT_CODE_ERROR);
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw $e;
|
$this->reconnected = true;
|
||||||
|
Yii::$app->db->close();
|
||||||
|
Yii::$app->db->open();
|
||||||
|
$this->callback($msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->reconnected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,4 +64,9 @@ abstract class AmqpController extends Controller {
|
|||||||
return ArrayHelper::getValue($this->getRoutesMap(), $route, 'route' . Inflector::camelize($route));
|
return ArrayHelper::getValue($this->getRoutesMap(), $route, 'route' . Inflector::camelize($route));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function isRestorableException(Exception $e): bool {
|
||||||
|
return strpos($e->getMessage(), 'MySQL server has gone away') !== false
|
||||||
|
|| strcmp($e->getMessage(), 'Error while sending QUERY packet') !== false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user