import { Queue, Worker, type Job } from "bullmq"; const connection = { host: Bun.env.REDIS_HOST ?? "localhost", port: Number(Bun.env.REDIS_PORT ?? 6379), }; export const notificationQueue = new Queue("notifications", { connection, defaultJobOptions: { removeOnComplete: true, removeOnFail: 100, }, }); export function createNotificationWorker( processor: (job: Job) => Promise, ): Worker { return new Worker("notifications", processor, { connection, concurrency: 1, limiter: { max: 1, duration: 2000, // 1 job per 2 seconds }, }); } export async function enqueueNotification( id: string, data: Record, ): Promise { await notificationQueue.add(id, { notificationId: id, ...data }); }