Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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<void> {
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
Expand Down
9 changes: 9 additions & 0 deletions packages/cli-repl/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down