|
| 1 | +import { Queue, QueueOptions } from "bullmq"; |
| 2 | +import Redis from "ioredis"; |
| 3 | + |
| 4 | +export interface PollReputationJobData { |
| 5 | + pollId: string; |
| 6 | + eventId: string; // For idempotency |
| 7 | + groupId: string; |
| 8 | +} |
| 9 | + |
| 10 | +export class JobQueueService { |
| 11 | + private queue: Queue<PollReputationJobData>; |
| 12 | + private redis: Redis; |
| 13 | + |
| 14 | + constructor() { |
| 15 | + // Create Redis connection |
| 16 | + this.redis = new Redis({ |
| 17 | + host: process.env.REDIS_HOST || "localhost", |
| 18 | + port: parseInt(process.env.REDIS_PORT || "6379", 10), |
| 19 | + maxRetriesPerRequest: null, |
| 20 | + }); |
| 21 | + |
| 22 | + // Create BullMQ queue |
| 23 | + const queueOptions: QueueOptions = { |
| 24 | + connection: { |
| 25 | + host: process.env.REDIS_HOST || "localhost", |
| 26 | + port: parseInt(process.env.REDIS_PORT || "6379", 10), |
| 27 | + }, |
| 28 | + defaultJobOptions: { |
| 29 | + attempts: 3, |
| 30 | + backoff: { |
| 31 | + type: "exponential", |
| 32 | + delay: 2000, |
| 33 | + }, |
| 34 | + removeOnComplete: { |
| 35 | + age: 3600, // Keep completed jobs for 1 hour |
| 36 | + count: 1000, |
| 37 | + }, |
| 38 | + removeOnFail: { |
| 39 | + age: 86400, // Keep failed jobs for 24 hours |
| 40 | + }, |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + this.queue = new Queue<PollReputationJobData>("poll-reputation-calculation", queueOptions); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Enqueue a poll reputation calculation job with deduplication |
| 49 | + */ |
| 50 | + async enqueuePollReputationJob( |
| 51 | + pollId: string, |
| 52 | + groupId: string, |
| 53 | + eventId: string |
| 54 | + ): Promise<void> { |
| 55 | + try { |
| 56 | + // Use pollId as the job ID for deduplication (same poll = same job) |
| 57 | + const jobId = `poll-reputation:${pollId}`; |
| 58 | + |
| 59 | + // Check if job already exists or was recently processed |
| 60 | + const existingJob = await this.queue.getJob(jobId); |
| 61 | + if (existingJob) { |
| 62 | + const state = await existingJob.getState(); |
| 63 | + if (state === "active" || state === "waiting" || state === "delayed") { |
| 64 | + // Job already queued, skip |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Add job with deduplication |
| 70 | + await this.queue.add( |
| 71 | + "calculate-poll-reputation", |
| 72 | + { |
| 73 | + pollId, |
| 74 | + groupId, |
| 75 | + eventId, |
| 76 | + }, |
| 77 | + { |
| 78 | + jobId, // Use pollId as job ID for deduplication |
| 79 | + removeOnComplete: true, |
| 80 | + removeOnFail: false, |
| 81 | + } |
| 82 | + ); |
| 83 | + } catch (error) { |
| 84 | + throw error; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Close the queue connection |
| 90 | + */ |
| 91 | + async close(): Promise<void> { |
| 92 | + await this.queue.close(); |
| 93 | + await this.redis.quit(); |
| 94 | + } |
| 95 | + |
| 96 | + getQueue(): Queue<PollReputationJobData> { |
| 97 | + return this.queue; |
| 98 | + } |
| 99 | +} |
| 100 | + |
0 commit comments