From 8f3e90188f04e5b14777efebaf1f942ecfbcc369 Mon Sep 17 00:00:00 2001 From: Vincent Chalamon Date: Fri, 23 Nov 2018 22:04:20 +0100 Subject: [PATCH 1/2] Fix #198: Support mongodb PHP extension --- Check/DoctrineMongoDb.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Check/DoctrineMongoDb.php b/Check/DoctrineMongoDb.php index 99207b81..ac73a057 100644 --- a/Check/DoctrineMongoDb.php +++ b/Check/DoctrineMongoDb.php @@ -3,6 +3,7 @@ namespace Liip\MonitorBundle\Check; use Doctrine\Bundle\MongoDBBundle\ManagerRegistry; +use MongoDB\Driver\Command; use ZendDiagnostics\Check\AbstractCheck; use ZendDiagnostics\Result\Success; @@ -19,10 +20,19 @@ public function __construct(ManagerRegistry $registry, $connectionName = null) public function check() { - $connection = $this->manager->getConnection(); - $connection->connect(); - - if ($connection->isConnected()) { + $connection = $this->manager->getConnection($this->connectionName); + + if (\method_exists($connection, 'connect')) { + // Using "mongo" PHP extension + $connection->connect(); + + if ($connection->isConnected()) { + return new Success(); + } + } else { + // Using "mongodb" PHP extension + $connection->getManager()->executeCommand('test', new Command(['ping' => 1])); + return new Success(); } From f818541b3f8295013a309c9da2a4f42f40718e03 Mon Sep 17 00:00:00 2001 From: Vincent Chalamon Date: Fri, 23 Nov 2018 23:23:34 +0100 Subject: [PATCH 2/2] Catch connection exception to failure --- Check/DoctrineMongoDb.php | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/Check/DoctrineMongoDb.php b/Check/DoctrineMongoDb.php index ac73a057..363d56d4 100644 --- a/Check/DoctrineMongoDb.php +++ b/Check/DoctrineMongoDb.php @@ -4,6 +4,7 @@ use Doctrine\Bundle\MongoDBBundle\ManagerRegistry; use MongoDB\Driver\Command; +use MongoDB\Driver\Exception\ConnectionException; use ZendDiagnostics\Check\AbstractCheck; use ZendDiagnostics\Result\Success; @@ -22,25 +23,34 @@ public function check() { $connection = $this->manager->getConnection($this->connectionName); + // Using "mongo" PHP extension if (\method_exists($connection, 'connect')) { - // Using "mongo" PHP extension $connection->connect(); if ($connection->isConnected()) { return new Success(); } - } else { - // Using "mongodb" PHP extension - $connection->getManager()->executeCommand('test', new Command(['ping' => 1])); - return new Success(); + return new Failure( + sprintf( + 'Connection "%s" is unavailable.', + $this->connectionName + ) + ); + } + + // Using "mongodb" PHP extension + try { + $connection->getManager()->executeCommand('test', new Command(['ping' => 1])); + } catch (ConnectionException $e) { + return new Failure( + sprintf( + 'Connection "%s" is unavailable.', + $this->connectionName + ) + ); } - return new Failure( - sprintf( - 'Connection "%s" is unavailable.', - $this->connectionName - ) - ); + return new Success(); } }