Skip to content

Commit 82cb3d1

Browse files
alenakhineikagagik
authored andcommitted
feat(cli-repl): do not wait for log clean-up MONGOSH-1990 (#2355)
* feat(cli-repl): do not wait for log clean-up MONGOSH-1990 * test: add test for log manager throwing error * test: remove only * fix: mark time after log cleanup
1 parent 7b8432d commit 82cb3d1

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import type { DevtoolsConnectOptions } from '@mongosh/service-provider-node-driv
3333
import type { AddressInfo } from 'net';
3434
import sinon from 'sinon';
3535
import type { CliUserConfig } from '@mongosh/types';
36-
import { MongoLogWriter } from 'mongodb-log-writer';
36+
import { MongoLogWriter, MongoLogManager } from 'mongodb-log-writer';
3737
const { EJSON } = bson;
3838

3939
const delay = promisify(setTimeout);
@@ -478,12 +478,14 @@ describe('CliRepl', function () {
478478
cliRepl = new CliRepl(cliReplOptions);
479479
await cliRepl.start('', {});
480480
await fs.stat(newerlogfile);
481-
try {
482-
await fs.stat(oldlogfile);
483-
expect.fail('missed exception');
484-
} catch (err: any) {
485-
expect(err.code).to.equal('ENOENT');
486-
}
481+
await eventually(async () => {
482+
try {
483+
await fs.stat(oldlogfile);
484+
expect.fail('missed exception');
485+
} catch (err: any) {
486+
expect(err.code).to.equal('ENOENT');
487+
}
488+
});
487489
});
488490

489491
it('verifies the Node.js version', async function () {
@@ -1382,6 +1384,7 @@ describe('CliRepl', function () {
13821384
srv.close();
13831385
await once(srv, 'close');
13841386
setTelemetryDelay(0);
1387+
sinon.restore();
13851388
});
13861389

13871390
context('logging configuration', function () {
@@ -1409,6 +1412,20 @@ describe('CliRepl', function () {
14091412

14101413
expect(cliRepl.logWriter).is.undefined;
14111414
});
1415+
1416+
it('logs cleanup errors', async function () {
1417+
sinon
1418+
.stub(MongoLogManager.prototype, 'cleanupOldLogFiles')
1419+
.rejects(new Error('Method not implemented'));
1420+
await cliRepl.start(await testServer.connectionString(), {});
1421+
expect(
1422+
(await log()).filter(
1423+
(entry) =>
1424+
entry.ctx === 'log' &&
1425+
entry.msg === 'Error: Method not implemented'
1426+
)
1427+
).to.have.lengthOf(1);
1428+
});
14121429
});
14131430

14141431
it('times out fast', async function () {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,15 @@ export class CliRepl implements MongoshIOProvider {
266266
this.warnAboutInaccessibleFile(err, path),
267267
});
268268

269-
await this.logManager.cleanupOldLogFiles();
270-
markTime(TimingCategories.Logging, 'cleaned up log files');
269+
// Do not wait for log cleanup and log errors if MongoLogManager throws any.
270+
void this.logManager
271+
.cleanupOldLogFiles()
272+
.catch((err) => {
273+
this.bus.emit('mongosh:error', err, 'log');
274+
})
275+
.finally(() => {
276+
markTime(TimingCategories.Logging, 'cleaned up log files');
277+
});
271278

272279
if (!this.logWriter) {
273280
this.logWriter ??= await this.logManager.createLogWriter();

0 commit comments

Comments
 (0)