import { type Processor, type JobsOptions, Queue as BullQueue, Worker, QueueScheduler } from "bullmq"; import redis from "./redis.server"; import logger from "./logger.server"; type RegisteredQueue = { queue: BullQueue; worker: Worker; scheduler: QueueScheduler; }; declare global { var __registeredQueues: Map | undefined; } const registeredQueues = global.__registeredQueues || (global.__registeredQueues = new Map()); export function Queue( name: string, handler: Processor, defaultJobOptions: JobsOptions = {}, ): BullQueue { if (registeredQueues.has(name)) { return registeredQueues.get(name)!.queue; } const jobOptions: JobsOptions = { attempts: 3, backoff: { type: "exponential", delay: 1000 }, ...defaultJobOptions, }; const queue = new BullQueue(name, { defaultJobOptions: jobOptions, connection: redis, }); const worker = new Worker(name, handler, { connection: redis }); const scheduler = new QueueScheduler(name, { connection: redis }); registeredQueues.set(name, { queue, worker, scheduler }); return queue; } export function CronJob( name: string, handler: Processor, cronSchedule: string, defaultJobOptions: Exclude = {}, ) { const jobOptions: JobsOptions = { ...defaultJobOptions, repeat: { cron: cronSchedule }, }; return function register() { if (registeredQueues.has(name)) { return registeredQueues.get(name)!.queue; } const queue = Queue(name, handler, jobOptions); queue.add(name, undefined, jobOptions); logger.info(`registered cron job "${name}"`); return queue; }; }