|
| 1 | +import type { AnyDatabase } from '../db'; |
| 2 | +import type { FastifyBaseLogger } from 'fastify'; |
| 3 | +import type { Worker, WorkerResult } from './types'; |
| 4 | +import { queueJobs, queueJobBatches } from '../db/schema.sqlite'; |
| 5 | +import { lt, sql } from 'drizzle-orm'; |
| 6 | + |
| 7 | +interface CleanupPayload { |
| 8 | + olderThanDays: number; |
| 9 | +} |
| 10 | + |
| 11 | +export class CleanupOldJobsWorker implements Worker { |
| 12 | + constructor( |
| 13 | + private readonly db: AnyDatabase, |
| 14 | + private readonly logger: FastifyBaseLogger |
| 15 | + ) {} |
| 16 | + |
| 17 | + async execute(payload: unknown, jobId: string): Promise<WorkerResult> { |
| 18 | + if (!this.isValidPayload(payload)) { |
| 19 | + return { |
| 20 | + success: false, |
| 21 | + message: 'Invalid payload format' |
| 22 | + }; |
| 23 | + } |
| 24 | + |
| 25 | + const { olderThanDays } = payload as CleanupPayload; |
| 26 | + |
| 27 | + this.logger.info({ |
| 28 | + jobId, |
| 29 | + olderThanDays, |
| 30 | + operation: 'cleanup_old_jobs' |
| 31 | + }, 'Starting cleanup of old queue jobs'); |
| 32 | + |
| 33 | + try { |
| 34 | + const cutoffDate = new Date(); |
| 35 | + cutoffDate.setDate(cutoffDate.getDate() - olderThanDays); |
| 36 | + |
| 37 | + // Delete old jobs |
| 38 | + const jobsResult = await this.db |
| 39 | + .delete(queueJobs) |
| 40 | + .where(lt(queueJobs.created_at, cutoffDate)); |
| 41 | + |
| 42 | + const jobsDeleted = (jobsResult.changes || jobsResult.rowsAffected || 0); |
| 43 | + |
| 44 | + // Delete orphaned batches (batches with no jobs) |
| 45 | + const batchesResult = await this.db |
| 46 | + .delete(queueJobBatches) |
| 47 | + .where( |
| 48 | + sql`${queueJobBatches.id} NOT IN (SELECT DISTINCT ${queueJobs.batch_id} FROM ${queueJobs} WHERE ${queueJobs.batch_id} IS NOT NULL)` |
| 49 | + ); |
| 50 | + |
| 51 | + const batchesDeleted = (batchesResult.changes || batchesResult.rowsAffected || 0); |
| 52 | + |
| 53 | + this.logger.info({ |
| 54 | + jobId, |
| 55 | + jobsDeleted, |
| 56 | + batchesDeleted, |
| 57 | + cutoffDate: cutoffDate.toISOString(), |
| 58 | + operation: 'cleanup_old_jobs' |
| 59 | + }, 'Cleanup completed successfully'); |
| 60 | + |
| 61 | + return { |
| 62 | + success: true, |
| 63 | + message: `Deleted ${jobsDeleted} jobs and ${batchesDeleted} orphaned batches older than ${olderThanDays} days`, |
| 64 | + data: { |
| 65 | + jobsDeleted, |
| 66 | + batchesDeleted, |
| 67 | + cutoffDate: cutoffDate.toISOString() |
| 68 | + } |
| 69 | + }; |
| 70 | + } catch (error) { |
| 71 | + this.logger.error({ |
| 72 | + jobId, |
| 73 | + error, |
| 74 | + operation: 'cleanup_old_jobs' |
| 75 | + }, 'Cleanup job failed'); |
| 76 | + |
| 77 | + throw error; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private isValidPayload(payload: unknown): payload is CleanupPayload { |
| 82 | + if (typeof payload !== 'object' || payload === null) return false; |
| 83 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 84 | + const p = payload as any; |
| 85 | + return typeof p.olderThanDays === 'number' && p.olderThanDays > 0; |
| 86 | + } |
| 87 | +} |
0 commit comments