import { Redis } from "ioredis"; import { Queue } from "bullmq"; import { QUEUE_NAME } from "./constants"; import type { VideoJobData } from "./types"; let connection: Redis | null = null; let queue: Queue | null = null; export function getRedisConnection(): Redis { if (!connection) { connection = new Redis(process.env.REDIS_URL || "redis://localhost:6379", { maxRetriesPerRequest: null, }); } return connection; } export function getVideoQueue(): Queue { if (!queue) { queue = new Queue(QUEUE_NAME, { connection: getRedisConnection(), }); } return queue; } export async function enqueueVideoJob(data: VideoJobData) { const q = getVideoQueue(); await q.add("process-video", data, { attempts: 2, backoff: { type: "exponential", delay: 5000 }, removeOnComplete: 100, removeOnFail: 200, }); }