-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.service.ts
More file actions
304 lines (274 loc) · 7.53 KB
/
queue.service.ts
File metadata and controls
304 lines (274 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Queue Service Implementation using BullMQ and Redis
*/
import { Queue, Worker, Job } from 'bullmq';
import { IQueueService } from '../interfaces/services';
import { logger } from '../utils/logger';
import { config } from '../config';
import IORedis from 'ioredis';
/**
* Queue Service using BullMQ
*/
export class QueueService implements IQueueService {
private queues: Map<string, Queue>;
private workers: Map<string, Worker>;
private connection: IORedis;
constructor() {
this.queues = new Map();
this.workers = new Map();
// Create Redis connection
this.connection = new IORedis({
host: config.redis.host,
port: config.redis.port,
password: config.redis.password,
maxRetriesPerRequest: null,
});
logger.info('Queue service initialized', {
host: config.redis.host,
port: config.redis.port,
});
}
/**
* Add a job to the queue
*/
async addJob(
jobName: string,
data: Record<string, unknown>,
options?: Record<string, unknown>
): Promise<string> {
try {
const queue = this.getOrCreateQueue(jobName);
const job = await queue.add(jobName, data, {
attempts: config.processing.maxRetries,
backoff: {
type: 'exponential',
delay: config.processing.retryDelayBase,
},
removeOnComplete: {
count: 100, // Keep last 100 completed jobs
},
removeOnFail: {
count: 500, // Keep last 500 failed jobs
},
...options,
});
logger.debug('Job added to queue', {
jobId: job.id,
jobName,
data,
});
return job.id as string;
} catch (error) {
logger.error('Failed to add job to queue', { error, jobName, data });
throw new Error(`Failed to add job to queue: ${jobName}`);
}
}
/**
* Process jobs from the queue
*/
async processJobs(
jobName: string,
processor: (job: Job) => Promise<void>
): Promise<void> {
try {
if (this.workers.has(jobName)) {
logger.warn('Worker already exists for job', { jobName });
return;
}
const worker = new Worker(
jobName,
async (job: Job) => {
logger.info('Processing job', {
jobId: job.id,
jobName: job.name,
data: job.data,
});
try {
await processor(job);
logger.info('Job completed successfully', {
jobId: job.id,
jobName: job.name,
});
} catch (error) {
logger.error('Job processing failed', {
jobId: job.id,
jobName: job.name,
error,
});
throw error;
}
},
{
connection: this.connection,
concurrency: config.processing.workerConcurrency,
settings: {
backoffStrategy: (attemptsMade: number) => {
// Exponential backoff with max delay cap
const delay = Math.min(
config.processing.retryDelayBase * Math.pow(2, attemptsMade - 1),
config.processing.retryDelayMax
);
return delay;
},
},
}
);
// Handle worker events
worker.on('completed', (job) => {
logger.debug('Worker completed job', {
jobId: job.id,
jobName: job.name,
});
});
worker.on('failed', (job, err) => {
logger.error('Worker failed job', {
jobId: job?.id,
jobName: job?.name,
error: err,
});
});
this.workers.set(jobName, worker);
logger.info('Worker created and started', { jobName });
} catch (error) {
logger.error('Failed to create worker', { error, jobName });
throw new Error(`Failed to create worker for: ${jobName}`);
}
}
/**
* Get job status
*/
async getJobStatus(jobId: string): Promise<string> {
try {
// Try to find the job in all queues
for (const [queueName, queue] of this.queues) {
try {
const job = await queue.getJob(jobId);
if (job) {
const state = await job.getState();
logger.debug('Job status retrieved', {
jobId,
queueName,
state,
});
return state;
}
} catch (error) {
// Continue searching in other queues
continue;
}
}
logger.warn('Job not found in any queue', { jobId });
return 'not_found';
} catch (error) {
logger.error('Failed to get job status', { error, jobId });
throw new Error('Failed to get job status');
}
}
/**
* Remove a job from the queue
*/
async removeJob(jobId: string): Promise<void> {
try {
// Try to remove from all queues
for (const [queueName, queue] of this.queues) {
try {
const job = await queue.getJob(jobId);
if (job) {
await job.remove();
logger.info('Job removed from queue', {
jobId,
queueName,
});
return;
}
} catch (error) {
// Continue searching in other queues
continue;
}
}
logger.warn('Job not found for removal', { jobId });
} catch (error) {
logger.error('Failed to remove job', { error, jobId });
throw new Error('Failed to remove job');
}
}
/**
* Get or create a queue for a job type
*/
private getOrCreateQueue(jobName: string): Queue {
if (!this.queues.has(jobName)) {
const queue = new Queue(jobName, {
connection: this.connection,
defaultJobOptions: {
removeOnComplete: {
count: 100,
},
removeOnFail: {
count: 500,
},
},
});
this.queues.set(jobName, queue);
logger.debug('Created new queue', { jobName });
}
return this.queues.get(jobName)!;
}
/**
* Close all queues and workers
*/
async close(): Promise<void> {
try {
// Close all workers
for (const [name, worker] of this.workers) {
await worker.close();
logger.debug('Worker closed', { name });
}
// Close all queues
for (const [name, queue] of this.queues) {
await queue.close();
logger.debug('Queue closed', { name });
}
// Close Redis connection
await this.connection.quit();
logger.info('Queue service closed');
} catch (error) {
logger.error('Error closing queue service', { error });
throw error;
}
}
/**
* Get queue metrics
*/
async getQueueMetrics(jobName: string): Promise<{
waiting: number;
active: number;
completed: number;
failed: number;
}> {
try {
const queue = this.getOrCreateQueue(jobName);
const [waiting, active, completed, failed] = await Promise.all([
queue.getWaitingCount(),
queue.getActiveCount(),
queue.getCompletedCount(),
queue.getFailedCount(),
]);
return { waiting, active, completed, failed };
} catch (error) {
logger.error('Failed to get queue metrics', { error, jobName });
throw new Error('Failed to get queue metrics');
}
}
/**
* Ping Redis to check connectivity
*/
async ping(): Promise<boolean> {
try {
const result = await this.connection.ping();
return result === 'PONG';
} catch (error) {
logger.error('Redis ping failed', { error });
return false;
}
}
}