Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(database): check MongoDB health with ping

Andras Bacsai658ac6bca57e
diff --git a/app/Actions/Database/StartMongodb.php b/app/Actions/Database/StartMongodb.php
index ed0d0dd7a9..61f252da17 100644
--- a/app/Actions/Database/StartMongodb.php
+++ b/app/Actions/Database/StartMongodb.php
@@ -330,6 +330,7 @@ class StartMongodb
/**
* The shell is chosen when the check runs: images before MongoDB 5 (and some custom images) have only the legacy mongo shell.
+ * The check uses ping, which needs no authentication and passes on members that are not the writable primary (e.g. an uninitiated replica set).
*/
private function generate_health_check_command(): array
{
@@ -354,7 +355,7 @@ class StartMongodb
$command = [
...$command,
'--eval',
- $legacy ? 'quit(db.isMaster().ismaster === true ? 0 : 1)' : 'quit(db.hello().isWritablePrimary === true ? 0 : 1)',
+ 'quit(db.adminCommand({ ping: 1 }).ok === 1 ? 0 : 1)',
];
return implode(' ', array_map('escapeshellarg', $command));
diff --git a/tests/Unit/StartMongodbHealthCheckTest.php b/tests/Unit/StartMongodbHealthCheckTest.php
index adad413e45..e0b57f0ebf 100644
--- a/tests/Unit/StartMongodbHealthCheckTest.php
+++ b/tests/Unit/StartMongodbHealthCheckTest.php
@@ -35,7 +35,7 @@ function runMongodbHealthCheck(array $healthCheck, array $availableShells): stri
return trim((string) $output);
}
-it('uses mongosh when the image has it and the legacy mongo shell otherwise', function (array $availableShells, bool $enableSsl, ?string $sslMode, array $expectedCall) {
+it('checks liveness with ping using mongosh when the image has it and the legacy mongo shell otherwise', function (array $availableShells, bool $enableSsl, ?string $sslMode, array $expectedCall) {
$healthCheck = generatedMongodbHealthCheckCommand([
'uuid' => 'mongodb-test-resource',
'image' => 'registry.example.com/mongo:any',
@@ -54,32 +54,32 @@ it('uses mongosh when the image has it and the legacy mongo shell otherwise', fu
})->with([
'mongosh without TLS' => [['mongosh', 'mongo'], false, null, [
'mongosh', '--quiet', '--host', 'mongodb-test-resource', '--eval',
- 'quit(db.hello().isWritablePrimary === true ? 0 : 1)',
+ 'quit(db.adminCommand({ ping: 1 }).ok === 1 ? 0 : 1)',
]],
'mongosh with TLS require' => [['mongosh'], true, 'require', [
'mongosh', '--quiet', '--host', 'mongodb-test-resource',
'--tls', '--tlsCAFile', '/etc/mongo/certs/ca.pem', '--eval',
- 'quit(db.hello().isWritablePrimary === true ? 0 : 1)',
+ 'quit(db.adminCommand({ ping: 1 }).ok === 1 ? 0 : 1)',
]],
'mongosh with TLS verify-full' => [['mongosh'], true, 'verify-full', [
'mongosh', '--quiet', '--host', 'mongodb-test-resource',
'--tls', '--tlsCAFile', '/etc/mongo/certs/ca.pem',
'--tlsCertificateKeyFile', '/etc/mongo/certs/server.pem', '--eval',
- 'quit(db.hello().isWritablePrimary === true ? 0 : 1)',
+ 'quit(db.adminCommand({ ping: 1 }).ok === 1 ? 0 : 1)',
]],
'legacy mongo without TLS' => [['mongo'], false, null, [
'mongo', '--quiet', '--host', 'mongodb-test-resource', '--eval',
- 'quit(db.isMaster().ismaster === true ? 0 : 1)',
+ 'quit(db.adminCommand({ ping: 1 }).ok === 1 ? 0 : 1)',
]],
'legacy mongo with TLS require' => [['mongo'], true, 'require', [
'mongo', '--quiet', '--host', 'mongodb-test-resource',
'--ssl', '--sslCAFile', '/etc/mongo/certs/ca.pem', '--eval',
- 'quit(db.isMaster().ismaster === true ? 0 : 1)',
+ 'quit(db.adminCommand({ ping: 1 }).ok === 1 ? 0 : 1)',
]],
'legacy mongo with TLS verify-full' => [['mongo'], true, 'verify-full', [
'mongo', '--quiet', '--host', 'mongodb-test-resource',
'--ssl', '--sslCAFile', '/etc/mongo/certs/ca.pem',
'--sslPEMKeyFile', '/etc/mongo/certs/server.pem', '--eval',
- 'quit(db.isMaster().ismaster === true ? 0 : 1)',
+ 'quit(db.adminCommand({ ping: 1 }).ok === 1 ? 0 : 1)',
]],
]);