Skip to content

Commit

Permalink
增加连接异常捕获和连接错误重试机制
Browse files Browse the repository at this point in the history
  • Loading branch information
kcloze committed Feb 9, 2018
1 parent fe8c2a4 commit 58525dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ public static function getQueue(array $config, Logs $logger)
{
$classQueue=$config['class'] ?? '\Kcloze\Jobs\Queue\RedisTopicQueue';
if (is_callable([$classQueue, 'getConnection'])) {
return $classQueue::getConnection($config, $logger);
//最多尝试连接3次
for ($i=0; $i < 3; $i++) {
$connection=$classQueue::getConnection($config, $logger);
if (is_object($connection)) {
break;
}
}

return $connection;
}
echo 'you must add queue config' . PHP_EOL;
exit;
Expand Down
4 changes: 4 additions & 0 deletions src/Queue/RabbitmqTopicQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public static function getConnection(array $config, Logs $logger)
$factory = new AmqpConnectionFactory($config);
$context = $factory->createContext();
$connection = new self($context, $config['exchange'] ?? null, $logger);
} catch (\AMQPConnectionException $e) {
Utils::catchError($logger, $e);

return false;
} catch (\Throwable $e) {
Utils::catchError($logger, $e);

Expand Down

0 comments on commit 58525dd

Please sign in to comment.