summaryrefslogtreecommitdiff
path: root/packages/backend/src/daemons
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/daemons')
-rw-r--r--packages/backend/src/daemons/ActivityLogCleanupService.ts64
-rw-r--r--packages/backend/src/daemons/DaemonModule.ts3
2 files changed, 67 insertions, 0 deletions
diff --git a/packages/backend/src/daemons/ActivityLogCleanupService.ts b/packages/backend/src/daemons/ActivityLogCleanupService.ts
new file mode 100644
index 0000000000..e2ffef3c5f
--- /dev/null
+++ b/packages/backend/src/daemons/ActivityLogCleanupService.ts
@@ -0,0 +1,64 @@
+/*
+ * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+import { Inject, Injectable, type OnApplicationShutdown } from '@nestjs/common';
+import { LessThan } from 'typeorm';
+import { DI } from '@/di-symbols.js';
+import type { Config } from '@/config.js';
+import { bindThis } from '@/decorators.js';
+import type { ActivityLogsRepository } from '@/models/_.js';
+
+// 10 minutes
+export const scanInterval = 1000 * 60 * 10;
+
+@Injectable()
+export class ActivityLogCleanupService implements OnApplicationShutdown {
+ private scanTimer: NodeJS.Timeout | null = null;
+
+ constructor(
+ @Inject(DI.config)
+ private readonly config: Config,
+
+ @Inject(DI.activityLogsRepository)
+ private readonly activityLogsRepository: ActivityLogsRepository,
+ ) {}
+
+ @bindThis
+ public async start(): Promise<void> {
+ // Just in case start() gets called multiple times.
+ this.dispose();
+
+ // Prune at startup, in case the server was rebooted during the interval.
+ // noinspection ES6MissingAwait
+ this.tick();
+
+ // Prune on a regular interval for the lifetime of the server.
+ this.scanTimer = setInterval(this.tick, scanInterval);
+ }
+
+ @bindThis
+ private async tick(): Promise<void> {
+ // This is the date in UTC of the oldest log to KEEP
+ const oldestAllowed = new Date(Date.now() - this.config.activityLogging.maxAge);
+
+ // Delete all logs older than the threshold.
+ await this.activityLogsRepository.delete({
+ at: LessThan(oldestAllowed),
+ });
+ }
+
+ @bindThis
+ public onApplicationShutdown(): void {
+ this.dispose();
+ }
+
+ @bindThis
+ public dispose(): void {
+ if (this.scanTimer) {
+ clearInterval(this.scanTimer);
+ this.scanTimer = null;
+ }
+ }
+}
diff --git a/packages/backend/src/daemons/DaemonModule.ts b/packages/backend/src/daemons/DaemonModule.ts
index a67907e6dd..12f890b3eb 100644
--- a/packages/backend/src/daemons/DaemonModule.ts
+++ b/packages/backend/src/daemons/DaemonModule.ts
@@ -8,6 +8,7 @@ import { CoreModule } from '@/core/CoreModule.js';
import { GlobalModule } from '@/GlobalModule.js';
import { QueueStatsService } from './QueueStatsService.js';
import { ServerStatsService } from './ServerStatsService.js';
+import { ActivityLogCleanupService } from './ActivityLogCleanupService.js';
@Module({
imports: [
@@ -17,10 +18,12 @@ import { ServerStatsService } from './ServerStatsService.js';
providers: [
QueueStatsService,
ServerStatsService,
+ ActivityLogCleanupService,
],
exports: [
QueueStatsService,
ServerStatsService,
+ ActivityLogCleanupService,
],
})
export class DaemonModule {}