From 1d17f374b975b3fe1dc650a4b8f997cb1d80fee3 Mon Sep 17 00:00:00 2001 From: telan Date: Tue, 10 Dec 2019 11:45:22 +0800 Subject: [PATCH] [fix] catch connect exception --- src/Server/Client.php | 2 ++ src/Server/Manager.php | 43 ++++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Server/Client.php b/src/Server/Client.php index 19e03e9..6dbb565 100644 --- a/src/Server/Client.php +++ b/src/Server/Client.php @@ -9,6 +9,7 @@ use Telanflow\Binlog\Constants\CommandTypeConst; use Telanflow\Binlog\Exceptions\PacketCheckException; use Telanflow\Binlog\Event\BinlogCurrent; +use Exception; class Client extends BaseClient { @@ -56,6 +57,7 @@ public function write($data, $flag = null) * @param bool $checkPacket * @return string * @throws PacketCheckException + * @throws Exception */ public function read($checkPacket = true) { diff --git a/src/Server/Manager.php b/src/Server/Manager.php index 67a63c2..75a77d7 100644 --- a/src/Server/Manager.php +++ b/src/Server/Manager.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Log; use Telanflow\Binlog\Configure\Configure; use Telanflow\Binlog\Constants\EventTypeConst; use Telanflow\Binlog\Event\EventBinaryData; @@ -15,6 +16,7 @@ use Telanflow\Binlog\Exceptions\ConnectionException; use Telanflow\Binlog\Exceptions\EventBinaryDataException; use Telanflow\Binlog\Helpers\OS; +use Telanflow\Binlog\Exceptions\PacketCheckException; class Manager { @@ -61,22 +63,29 @@ public function run() pcntl_signal(SIGTERM, [$this, 'signalHandler'], false); pcntl_signal(SIGPIPE, SIG_IGN, false); - // conn - if(!$this->client->connect(Configure::getHost(), Configure::getPort(), 10)) { - throw new ConnectionException($this->client->reuse, $this->client->errCode); - } - // auth - $this->client->authenticate(); - // registerSlave - $this->client->getBinlogStream(); + try { - while (!$this->exit) - { - pcntl_signal_dispatch(); + // conn + if(!$this->client->connect(Configure::getHost(), Configure::getPort(), 10)) { + throw new ConnectionException($this->client->reuse, $this->client->errCode); + } + // auth + $this->client->authenticate(); + // registerSlave + $this->client->getBinlogStream(); - try { - $this->consume(); - } catch (EventBinaryDataException $e) {} + while (!$this->exit) + { + pcntl_signal_dispatch(); + + try { + $this->consume(); + } catch (EventBinaryDataException $e) {} + } + + } catch (\Exception $e) { + print_r("Connect error: " . $e->getMessage() . ' : ' . $e->getCode()); + Log::error($e->getMessage()); } } @@ -107,6 +116,9 @@ public function signalHandler($signal) } } + /** + * @throws PacketCheckException + */ protected function consume(): void { $recvData = $this->client->read(); @@ -217,7 +229,10 @@ protected function savePidFile() /** * Set process name. * + * @param string $subProcessName + * * @codeCoverageIgnore + * @throws \Illuminate\Contracts\Container\BindingResolutionException */ protected function setProcessName($subProcessName) {