Skip to content

Commit 97acb9d

Browse files
authored
chore(cli-repl): warn about missing connectivity MONGOSH-1067 (#1171)
Warn the user when the driver appears to connect successfully but a “ping” command still fails to run.
1 parent 47f8777 commit 97acb9d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ class MongoshNodeRepl implements EvaluationListener {
187187
}
188188
await this.greet(mongodVersion);
189189
await this.printStartupLog(instanceState);
190+
await this.printBasicConnectivityWarning(instanceState);
190191

191192
this.inspectCompact = await this.getConfig('inspectCompact');
192193
this.inspectDepth = await this.getConfig('inspectDepth');
@@ -459,6 +460,27 @@ class MongoshNodeRepl implements EvaluationListener {
459460
this.output.write(text);
460461
}
461462

463+
/**
464+
* Print a warning if the server is not able to respond to commands.
465+
* This can happen in load balanced mode, for example.
466+
*/
467+
async printBasicConnectivityWarning(instanceState: ShellInstanceState): Promise<void> {
468+
if (this.shellCliOptions.nodb || this.shellCliOptions.quiet) {
469+
return;
470+
}
471+
472+
let err: Error;
473+
try {
474+
await instanceState.currentDb.adminCommand({ ping: 1 });
475+
return;
476+
} catch (error: any) {
477+
err = error;
478+
}
479+
480+
const text = this.clr('The server failed to respond to a ping and may be unavailable:', ['bold', 'yellow']);
481+
this.output.write(text + '\n' + this.formatError(err) + '\n');
482+
}
483+
462484
/**
463485
* Evaluate a piece of input code. This is called by the AsyncRepl eval function
464486
* and calls the {@link ShellEvaluator} eval function, passing along all of its

packages/cli-repl/test/e2e.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,15 @@ describe('e2e', function() {
11411141
});
11421142
});
11431143

1144+
describe('with incomplete loadBalanced connectivity', () => {
1145+
it('prints a warning at startup', async() => {
1146+
const shell = TestShell.start({ args: [ 'mongodb://localhost:1/?loadBalanced=true' ] });
1147+
await shell.waitForPrompt();
1148+
shell.assertContainsOutput('The server failed to respond to a ping and may be unavailable');
1149+
shell.assertContainsOutput('MongoNetworkError');
1150+
});
1151+
});
1152+
11441153
describe('run Node.js scripts as-is', () => {
11451154
it('runs Node.js scripts as they are when using MONGOSH_RUN_NODE_SCRIPT', async() => {
11461155
const filename = path.resolve(__dirname, 'fixtures', 'simple-console-log.js');

0 commit comments

Comments
 (0)