Skip to content

feat(cli-repl): do not wait for log clean-up MONGOSH-1990 #2355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
31 changes: 24 additions & 7 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type { DevtoolsConnectOptions } from '@mongosh/service-provider-node-driv
import type { AddressInfo } from 'net';
import sinon from 'sinon';
import type { CliUserConfig } from '@mongosh/types';
import { MongoLogWriter } from 'mongodb-log-writer';
import { MongoLogWriter, MongoLogManager } from 'mongodb-log-writer';
const { EJSON } = bson;

const delay = promisify(setTimeout);
Expand Down Expand Up @@ -478,12 +478,14 @@ describe('CliRepl', function () {
cliRepl = new CliRepl(cliReplOptions);
await cliRepl.start('', {});
await fs.stat(newerlogfile);
try {
await fs.stat(oldlogfile);
expect.fail('missed exception');
} catch (err: any) {
expect(err.code).to.equal('ENOENT');
}
await eventually(async () => {
try {
await fs.stat(oldlogfile);
expect.fail('missed exception');
} catch (err: any) {
expect(err.code).to.equal('ENOENT');
}
});
});

it('verifies the Node.js version', async function () {
Expand Down Expand Up @@ -1382,6 +1384,7 @@ describe('CliRepl', function () {
srv.close();
await once(srv, 'close');
setTelemetryDelay(0);
sinon.restore();
});

context('logging configuration', function () {
Expand Down Expand Up @@ -1409,6 +1412,20 @@ describe('CliRepl', function () {

expect(cliRepl.logWriter).is.undefined;
});

it('logs cleanup errors', async function () {
sinon
.stub(MongoLogManager.prototype, 'cleanupOldLogFiles')
.rejects(new Error('Method not implemented'));
await cliRepl.start(await testServer.connectionString(), {});
expect(
(await log()).filter(
(entry) =>
entry.ctx === 'log' &&
entry.msg === 'Error: Method not implemented'
)
).to.have.lengthOf(1);
});
});

it('times out fast', async function () {
Expand Down
11 changes: 9 additions & 2 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,15 @@ export class CliRepl implements MongoshIOProvider {
this.warnAboutInaccessibleFile(err, path),
});

await this.logManager.cleanupOldLogFiles();
markTime(TimingCategories.Logging, 'cleaned up log files');
// Do not wait for log cleanup and log errors if MongoLogManager throws any.
void this.logManager
.cleanupOldLogFiles()
.catch((err) => {
this.bus.emit('mongosh:error', err, 'log');
})
.finally(() => {
markTime(TimingCategories.Logging, 'cleaned up log files');
});

if (!this.logWriter) {
this.logWriter ??= await this.logManager.createLogWriter();
Expand Down