-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
apps/backend/src/modules/logs-cleanup/logs-cleanup.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Controller, OnModuleInit } from "@nestjs/common"; | ||
import { Cron, CronExpression } from "@nestjs/schedule"; | ||
|
||
import { LogsCleanupService } from "src/modules/logs-cleanup/logs-cleanup.service"; | ||
|
||
@Controller("logs-cleanup") | ||
export class LogsCleanupController implements OnModuleInit { | ||
constructor(private readonly logsCleanupService: LogsCleanupService) {} | ||
|
||
async onModuleInit(): Promise<void> { | ||
return this.logsCleanupService.cleanupLogs(); | ||
} | ||
|
||
@Cron(CronExpression.EVERY_10_MINUTES) | ||
async cronLogsCleanup(): Promise<void> { | ||
return this.logsCleanupService.cleanupLogs(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
apps/backend/src/modules/logs-cleanup/logs-cleanup.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { LogsCleanupController } from "src/modules/logs-cleanup/logs-cleanup.controller"; | ||
import { LogsCleanupService } from "src/modules/logs-cleanup/logs-cleanup.service"; | ||
|
||
@Module({ | ||
controllers: [LogsCleanupController], | ||
providers: [LogsCleanupService], | ||
imports: [], | ||
}) | ||
export class LogsCleanupModule {} |
60 changes: 60 additions & 0 deletions
60
apps/backend/src/modules/logs-cleanup/logs-cleanup.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
|
||
import { LogLevel, LogMessage } from "src/enums/log.enum"; | ||
import { LoggerService } from "src/modules/logger/logger.service"; | ||
import { PrismaService } from "src/modules/prisma/prisma.service"; | ||
|
||
const MAX_COUNT = 500_000; | ||
|
||
@Injectable() | ||
export class LogsCleanupService { | ||
constructor( | ||
private readonly prisma: PrismaService, | ||
private readonly logger: LoggerService, | ||
) {} | ||
|
||
async cleanupLogs(): Promise<void> { | ||
try { | ||
const recordCount = await this.prisma.requestLog.count(); | ||
|
||
if (recordCount < MAX_COUNT) { | ||
return; | ||
} | ||
|
||
const last = await this.prisma.requestLog.findFirst({ | ||
orderBy: { createdAt: "desc" }, | ||
skip: MAX_COUNT, | ||
}); | ||
|
||
if (!last) { | ||
return; | ||
} | ||
|
||
const { count } = await this.prisma.requestLog.deleteMany({ | ||
where: { | ||
createdAt: { | ||
lte: last.createdAt, | ||
}, | ||
}, | ||
}); | ||
|
||
await this.logger.createLog( | ||
LogLevel.log, | ||
LogMessage.REQUEST_LOGS_CLEANUP, | ||
{ | ||
message: "Successfully removed old logs", | ||
count, | ||
}, | ||
); | ||
} catch (error) { | ||
await this.logger.createLog( | ||
LogLevel.error, | ||
LogMessage.REQUEST_LOGS_CLEANUP, | ||
{ | ||
message: "Failed to cleanup logs", | ||
error: JSON.stringify(error), | ||
}, | ||
); | ||
} | ||
} | ||
} |