mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 13:02:07 +02:00
MDL-54592 cachestore_mongodb: check MongoDB server connection
The connection to the MongoDB server is checked on the creation and deletion of a cache store instance. If the connection fails during creation, the instance is set as not ready. Before deleting a instance, a new cachestore_mongodb object is created, and connection checked. There is no reason to check again the connection in the instance_deleted() method.
This commit is contained in:
parent
2a944b5b41
commit
02a5d26bd5
17
cache/stores/mongodb/lib.php
vendored
17
cache/stores/mongodb/lib.php
vendored
@ -144,6 +144,9 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable {
|
||||
|
||||
try {
|
||||
$this->connection = new MongoDB\Client($this->server, $this->options);
|
||||
// Required because MongoDB\Client does not try to connect to the server
|
||||
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
|
||||
$this->connection->getManager()->selectServer($rp);
|
||||
$this->isready = true;
|
||||
} catch (MongoDB\Driver\Exception\RuntimeException $e) {
|
||||
// We only want to catch RuntimeException here.
|
||||
@ -480,18 +483,10 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable {
|
||||
public function instance_deleted() {
|
||||
// We can't use purge here that acts upon a collection.
|
||||
// Instead we must drop the named database.
|
||||
if ($this->connection) {
|
||||
$connection = $this->connection;
|
||||
} else {
|
||||
try {
|
||||
$connection = new MongoDB\Client($this->server, $this->options);
|
||||
} catch (MongoDB\Driver\Exception\RuntimeException $e) {
|
||||
// We only want to catch RuntimeException here.
|
||||
// If the server cannot be connected to we cannot clean it.
|
||||
return;
|
||||
}
|
||||
if (!$this->is_ready()) {
|
||||
return;
|
||||
}
|
||||
$database = $connection->selectDatabase($this->databasename);
|
||||
$database = $this->connection->selectDatabase($this->databasename);
|
||||
$database->drop();
|
||||
$connection = null;
|
||||
$database = null;
|
||||
|
Loading…
x
Reference in New Issue
Block a user