Skip to content

Commit

Permalink
fix: prevent error loop, create crash report file if not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
marknguyen1302 authored Jun 19, 2024
1 parent c55cade commit 00b5fe2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
9 changes: 8 additions & 1 deletion cortex-js/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ async function bootstrap() {
process.exit(1);
},
});

telemetryUseCase = await app.resolve(TelemetryUsecases);
contextService = await app.resolve(ContextService);

telemetryUseCase!.sendCrashReport();
await contextService!.init(async () => CommandFactory.runApplication(app));

await contextService!.init(async () => {
contextService!.set('source', TelemetrySource.CLI);
return CommandFactory.runApplication(app);
});

const notifier = updateNotifier({
pkg: packageJson,
updateCheckInterval: 1000 * 60 * 60, // 1 hour
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TelemetrySource } from '@/domain/telemetry/telemetry.interface';
import { ContextService } from '@/util/context.service';
import { Injectable, NestMiddleware, Logger } from '@nestjs/common';

Expand Down Expand Up @@ -32,6 +33,7 @@ export class AppLoggerMiddleware implements NestMiddleware {
});
this.contextService.init(() => {
this.contextService.set('endpoint', originalUrl ?? url);
this.contextService.set('source', TelemetrySource.CORTEX_SERVER);
next();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class TelemetryRepositoryImpl implements TelemetryRepository {

private async getTelemetryDirectory(): Promise<string> {
const dataFolderPath = await this.fileManagerService.getDataFolderPath();
await this.fileManagerService.createFolderIfNotExistInDataFolder(
'telemetry',
);
return join(dataFolderPath, 'telemetry');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export class FileManagerService {
stats.size === 0 ? data : `\n${data}`,
{
encoding: 'utf8',
flag: 'a+',
},
);
} catch (err) {
Expand Down
39 changes: 28 additions & 11 deletions cortex-js/src/usecases/telemetry/telemetry.usecases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,30 @@ import { HttpException, Inject, Injectable, Scope } from '@nestjs/common';

@Injectable({ scope: Scope.TRANSIENT })
export class TelemetryUsecases {
private readonly crashReports: string[] = [];
private readonly maxSize = 100;

constructor(
@Inject('TELEMETRY_REPOSITORY')
private readonly telemetryRepository: TelemetryRepository,
private readonly contextService: ContextService,
) {
process.on('uncaughtException', async (error: Error) => {
telemetryRepository.createCrashReport(this.buildCrashReport(error));
});

process.on('unhandledRejection', async (error: Error) => {
telemetryRepository.createCrashReport(this.buildCrashReport(error));
});
this.catchException();
}

async createCrashReport(
error: HttpException | Error,
source: TelemetrySource,
): Promise<void> {
try {
if (this.isCrashReportEnabled() === false) {
return;
}
if (!this.isCrashReportEnabled()) return;

const crashReport: CrashReportAttributes = this.buildCrashReport(error);
if (this.crashReports.includes(JSON.stringify(crashReport))) return;
if (this.crashReports.length >= this.maxSize) {
this.crashReports.shift();
}
this.crashReports.push(JSON.stringify(crashReport));

await this.telemetryRepository.createCrashReport(crashReport, source);
} catch (e) {}
Expand Down Expand Up @@ -83,6 +84,22 @@ export class TelemetryUsecases {
}

private isCrashReportEnabled(): boolean {
return process.env.CORTEX_CRASH_REPORT === '1';
return process.env.CORTEX_CRASH_REPORT !== '0';
}

private async catchException(): Promise<void> {
process.on('uncaughtException', async (error: Error) => {
await this.createCrashReport(
error,
this.contextService.get('source') as TelemetrySource,
);
});

process.on('unhandledRejection', async (error: Error) => {
await this.createCrashReport(
error,
this.contextService.get('source') as TelemetrySource,
);
});
}
}

0 comments on commit 00b5fe2

Please sign in to comment.