From e6fe2f375570deb5b1ab3a78d5d8769afddac289 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sat, 4 Nov 2017 15:52:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=B2=D0=BE=D1=81=D1=81=D1=82=D0=B0=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BE=D0=B5=D0=B4?= =?UTF-8?q?=D0=B8=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F=20AMQ?= =?UTF-8?q?P=20=D0=B2=D0=BE=D1=80=D0=BA=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console/controllers/AmqpController.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/console/controllers/AmqpController.php b/console/controllers/AmqpController.php index e7c3bc0..e06c765 100644 --- a/console/controllers/AmqpController.php +++ b/console/controllers/AmqpController.php @@ -2,19 +2,21 @@ namespace console\controllers; use Ely\Amqp\ControllerTrait; +use Exception; use PhpAmqpLib\Message\AMQPMessage; use Yii; use yii\console\Controller; use yii\db\Exception as YiiDbException; use yii\helpers\ArrayHelper; use yii\helpers\Inflector; -use yii\helpers\StringHelper; abstract class AmqpController extends Controller { use ControllerTrait { callback as _callback; } + private $reconnected = false; + public final function actionIndex() { $this->start(); } @@ -35,12 +37,17 @@ abstract class AmqpController extends Controller { try { $this->_callback($msg); } catch (YiiDbException $e) { - if (StringHelper::startsWith($e->getMessage(), 'Error while sending QUERY packet')) { - exit(self::EXIT_CODE_ERROR); + if ($this->reconnected || !$this->isRestorableException($e)) { + 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)); } + 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; + } + }