diff --git a/packages/cli-repl/src/mongosh-repl.ts b/packages/cli-repl/src/mongosh-repl.ts index 7333651087..2c5b1e8f3e 100644 --- a/packages/cli-repl/src/mongosh-repl.ts +++ b/packages/cli-repl/src/mongosh-repl.ts @@ -187,6 +187,7 @@ class MongoshNodeRepl implements EvaluationListener { } await this.greet(mongodVersion); await this.printStartupLog(instanceState); + await this.printBasicConnectivityWarning(instanceState); this.inspectCompact = await this.getConfig('inspectCompact'); this.inspectDepth = await this.getConfig('inspectDepth'); @@ -459,6 +460,27 @@ class MongoshNodeRepl implements EvaluationListener { this.output.write(text); } + /** + * Print a warning if the server is not able to respond to commands. + * This can happen in load balanced mode, for example. + */ + async printBasicConnectivityWarning(instanceState: ShellInstanceState): Promise { + if (this.shellCliOptions.nodb || this.shellCliOptions.quiet) { + return; + } + + let err: Error; + try { + await instanceState.currentDb.adminCommand({ ping: 1 }); + return; + } catch (error: any) { + err = error; + } + + const text = this.clr('The server failed to respond to a ping and may be unavailable:', ['bold', 'yellow']); + this.output.write(text + '\n' + this.formatError(err) + '\n'); + } + /** * Evaluate a piece of input code. This is called by the AsyncRepl eval function * and calls the {@link ShellEvaluator} eval function, passing along all of its diff --git a/packages/cli-repl/test/e2e.spec.ts b/packages/cli-repl/test/e2e.spec.ts index df77121867..dc99a87447 100644 --- a/packages/cli-repl/test/e2e.spec.ts +++ b/packages/cli-repl/test/e2e.spec.ts @@ -1141,6 +1141,15 @@ describe('e2e', function() { }); }); + describe('with incomplete loadBalanced connectivity', () => { + it('prints a warning at startup', async() => { + const shell = TestShell.start({ args: [ 'mongodb://localhost:1/?loadBalanced=true' ] }); + await shell.waitForPrompt(); + shell.assertContainsOutput('The server failed to respond to a ping and may be unavailable'); + shell.assertContainsOutput('MongoNetworkError'); + }); + }); + describe('run Node.js scripts as-is', () => { it('runs Node.js scripts as they are when using MONGOSH_RUN_NODE_SCRIPT', async() => { const filename = path.resolve(__dirname, 'fixtures', 'simple-console-log.js');