From ab6b6cd9c905471c230a7603ca8d0fc47aca77d0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 13 Dec 2021 15:58:14 +0100 Subject: [PATCH 1/2] chore(cli-repl): warn about missing connectivity MONGOSH-1067 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Warn the user when the driver appears to connect successfully but a “ping” command still fails to run. --- packages/cli-repl/src/mongosh-repl.ts | 22 ++++++++++++++++++++++ packages/cli-repl/test/e2e.spec.ts | 9 +++++++++ 2 files changed, 31 insertions(+) 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..0c829a5550 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://nonexistent/?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'); From bd86341bc6a130b7f31fbc4c2b9ca7a525ee5b57 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 13 Dec 2021 17:20:57 +0100 Subject: [PATCH 2/2] fixup: try ECONNREFUSED instead --- packages/cli-repl/test/e2e.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli-repl/test/e2e.spec.ts b/packages/cli-repl/test/e2e.spec.ts index 0c829a5550..dc99a87447 100644 --- a/packages/cli-repl/test/e2e.spec.ts +++ b/packages/cli-repl/test/e2e.spec.ts @@ -1143,7 +1143,7 @@ describe('e2e', function() { describe('with incomplete loadBalanced connectivity', () => { it('prints a warning at startup', async() => { - const shell = TestShell.start({ args: [ 'mongodb://nonexistent/?loadBalanced=true' ] }); + 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');