Skip to content

Commit

Permalink
[fix] catch connect exception
Browse files Browse the repository at this point in the history
  • Loading branch information
telanflow committed Dec 10, 2019
1 parent 7675f5b commit 1d17f37
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/Server/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -56,6 +57,7 @@ public function write($data, $flag = null)
* @param bool $checkPacket
* @return string
* @throws PacketCheckException
* @throws Exception
*/
public function read($checkPacket = true)
{
Expand Down
43 changes: 29 additions & 14 deletions src/Server/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -107,6 +116,9 @@ public function signalHandler($signal)
}
}

/**
* @throws PacketCheckException
*/
protected function consume(): void
{
$recvData = $this->client->read();
Expand Down Expand Up @@ -217,7 +229,10 @@ protected function savePidFile()
/**
* Set process name.
*
* @param string $subProcessName
*
* @codeCoverageIgnore
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
protected function setProcessName($subProcessName)
{
Expand Down

0 comments on commit 1d17f37

Please sign in to comment.