summaryrefslogtreecommitdiff
path: root/packages/backend/src/daemons/ApLogCleanupService.ts
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2025-02-18 19:51:13 +0000
committerHazelnoot <acomputerdog@gmail.com>2025-02-18 19:51:13 +0000
commitc28b27b57f2329ab72a768bb6a1074adeb4dc2e7 (patch)
tree65be25d7ca2da4306033448d628807acf0518e74 /packages/backend/src/daemons/ApLogCleanupService.ts
parentmerge: Fix error message when a peertube object is rejected for bad ID / URL ... (diff)
parentdelete fetch logs when a note or user is deleted (diff)
downloadsharkey-c28b27b57f2329ab72a768bb6a1074adeb4dc2e7.tar.gz
sharkey-c28b27b57f2329ab72a768bb6a1074adeb4dc2e7.tar.bz2
sharkey-c28b27b57f2329ab72a768bb6a1074adeb4dc2e7.zip
merge: Optionally log remote ActivityPub objects to database (!833)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/833 Approved-by: dakkar <dakkar@thenautilus.net> Approved-by: Marie <github@yuugi.dev>
Diffstat (limited to 'packages/backend/src/daemons/ApLogCleanupService.ts')
-rw-r--r--packages/backend/src/daemons/ApLogCleanupService.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/packages/backend/src/daemons/ApLogCleanupService.ts b/packages/backend/src/daemons/ApLogCleanupService.ts
new file mode 100644
index 0000000000..61f76b4e2c
--- /dev/null
+++ b/packages/backend/src/daemons/ApLogCleanupService.ts
@@ -0,0 +1,62 @@
+/*
+ * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+import { Injectable, type OnApplicationShutdown } from '@nestjs/common';
+import { bindThis } from '@/decorators.js';
+import { LoggerService } from '@/core/LoggerService.js';
+import Logger from '@/logger.js';
+import { ApLogService } from '@/core/ApLogService.js';
+
+// 10 minutes
+export const scanInterval = 1000 * 60 * 10;
+
+@Injectable()
+export class ApLogCleanupService implements OnApplicationShutdown {
+ private readonly logger: Logger;
+ private scanTimer: NodeJS.Timeout | null = null;
+
+ constructor(
+ private readonly apLogService: ApLogService,
+ loggerService: LoggerService,
+ ) {
+ this.logger = loggerService.getLogger('activity-log-cleanup');
+ }
+
+ @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> {
+ try {
+ const affected = await this.apLogService.deleteExpiredLogs();
+ this.logger.info(`Activity Log cleanup complete; removed ${affected} expired logs.`);
+ } catch (err) {
+ this.logger.error('Activity Log cleanup failed:', err as Error);
+ }
+ }
+
+ @bindThis
+ public onApplicationShutdown(): void {
+ this.dispose();
+ }
+
+ @bindThis
+ public dispose(): void {
+ if (this.scanTimer) {
+ clearInterval(this.scanTimer);
+ this.scanTimer = null;
+ }
+ }
+}