|
| 1 | +import * as cron from 'node-cron'; |
| 2 | +import type { FastifyBaseLogger } from 'fastify'; |
| 3 | + |
| 4 | +/** |
| 5 | + * CronJob interface - each cron job file must export this structure |
| 6 | + */ |
| 7 | +export interface CronJob { |
| 8 | + /** Cron schedule expression (e.g., every 2 minutes, hourly, daily) */ |
| 9 | + schedule: string; |
| 10 | + |
| 11 | + /** Optional job name for logging */ |
| 12 | + name?: string; |
| 13 | + |
| 14 | + /** The task function to execute */ |
| 15 | + task: () => void | Promise<void>; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * CronManager - Manages all cron jobs in the application |
| 20 | + * |
| 21 | + * This class discovers, registers, and manages all cron jobs. |
| 22 | + * It provides lifecycle management including graceful shutdown. |
| 23 | + */ |
| 24 | +export class CronManager { |
| 25 | + private jobs: Map<string, cron.ScheduledTask> = new Map(); |
| 26 | + private logger: FastifyBaseLogger; |
| 27 | + |
| 28 | + constructor(logger: FastifyBaseLogger) { |
| 29 | + this.logger = logger; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Register a new cron job |
| 34 | + * |
| 35 | + * @param jobDefinition - The cron job definition |
| 36 | + */ |
| 37 | + register(jobDefinition: CronJob): void { |
| 38 | + const jobName = jobDefinition.name || 'unnamed-job'; |
| 39 | + |
| 40 | + try { |
| 41 | + const task = cron.schedule(jobDefinition.schedule, async () => { |
| 42 | + this.logger.info({ |
| 43 | + job: jobName, |
| 44 | + schedule: jobDefinition.schedule |
| 45 | + }, 'Executing cron job'); |
| 46 | + |
| 47 | + try { |
| 48 | + await jobDefinition.task(); |
| 49 | + this.logger.debug({ job: jobName }, 'Cron job completed successfully'); |
| 50 | + } catch (error) { |
| 51 | + this.logger.error({ |
| 52 | + job: jobName, |
| 53 | + error |
| 54 | + }, 'Cron job execution failed'); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + this.jobs.set(jobName, task); |
| 59 | + this.logger.info({ |
| 60 | + job: jobName, |
| 61 | + schedule: jobDefinition.schedule |
| 62 | + }, 'Cron job registered'); |
| 63 | + |
| 64 | + } catch (error) { |
| 65 | + this.logger.error({ |
| 66 | + job: jobName, |
| 67 | + schedule: jobDefinition.schedule, |
| 68 | + error |
| 69 | + }, 'Failed to register cron job'); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Start all registered cron jobs |
| 75 | + */ |
| 76 | + start(): void { |
| 77 | + this.logger.info({ count: this.jobs.size }, 'Cron jobs are running (auto-started on schedule)'); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Stop all cron jobs gracefully |
| 82 | + */ |
| 83 | + stop(): void { |
| 84 | + this.logger.info({ count: this.jobs.size }, 'Stopping cron jobs'); |
| 85 | + |
| 86 | + for (const [name, task] of this.jobs.entries()) { |
| 87 | + task.stop(); |
| 88 | + this.logger.debug({ job: name }, 'Cron job stopped'); |
| 89 | + } |
| 90 | + |
| 91 | + this.jobs.clear(); |
| 92 | + this.logger.info('All cron jobs stopped successfully'); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get list of registered job names |
| 97 | + */ |
| 98 | + getJobNames(): string[] { |
| 99 | + return Array.from(this.jobs.keys()); |
| 100 | + } |
| 101 | +} |
0 commit comments