@nathapp/nestjs-queue 2.1.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +106 -0
- package/TODO.md +44 -0
- package/dist/constant/index.d.ts +1 -0
- package/dist/constant/index.js +18 -0
- package/dist/constant/index.js.map +1 -0
- package/dist/constant/queue.constant.d.ts +6 -0
- package/dist/constant/queue.constant.js +10 -0
- package/dist/constant/queue.constant.js.map +1 -0
- package/dist/decorator/index.d.ts +3 -0
- package/dist/decorator/index.js +20 -0
- package/dist/decorator/index.js.map +1 -0
- package/dist/decorator/on-queue-event.decorator.d.ts +6 -0
- package/dist/decorator/on-queue-event.decorator.js +18 -0
- package/dist/decorator/on-queue-event.decorator.js.map +1 -0
- package/dist/decorator/process.decorator.d.ts +5 -0
- package/dist/decorator/process.decorator.js +18 -0
- package/dist/decorator/process.decorator.js.map +1 -0
- package/dist/decorator/processor.decorator.d.ts +5 -0
- package/dist/decorator/processor.decorator.js +12 -0
- package/dist/decorator/processor.decorator.js.map +1 -0
- package/dist/enum/index.d.ts +3 -0
- package/dist/enum/index.js +20 -0
- package/dist/enum/index.js.map +1 -0
- package/dist/enum/job-status.enum.d.ts +8 -0
- package/dist/enum/job-status.enum.js +13 -0
- package/dist/enum/job-status.enum.js.map +1 -0
- package/dist/enum/provider.enum.d.ts +7 -0
- package/dist/enum/provider.enum.js +12 -0
- package/dist/enum/provider.enum.js.map +1 -0
- package/dist/enum/queue-event.enum.d.ts +11 -0
- package/dist/enum/queue-event.enum.js +16 -0
- package/dist/enum/queue-event.enum.js.map +1 -0
- package/dist/explorer/index.d.ts +1 -0
- package/dist/explorer/index.js +18 -0
- package/dist/explorer/index.js.map +1 -0
- package/dist/explorer/processor.explorer.d.ts +25 -0
- package/dist/explorer/processor.explorer.js +241 -0
- package/dist/explorer/processor.explorer.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/interface/circuit-breaker.interface.d.ts +21 -0
- package/dist/interface/circuit-breaker.interface.js +10 -0
- package/dist/interface/circuit-breaker.interface.js.map +1 -0
- package/dist/interface/health.interface.d.ts +12 -0
- package/dist/interface/health.interface.js +3 -0
- package/dist/interface/health.interface.js.map +1 -0
- package/dist/interface/idempotency-store.interface.d.ts +14 -0
- package/dist/interface/idempotency-store.interface.js +3 -0
- package/dist/interface/idempotency-store.interface.js.map +1 -0
- package/dist/interface/index.d.ts +5 -0
- package/dist/interface/index.js +22 -0
- package/dist/interface/index.js.map +1 -0
- package/dist/interface/job.interface.d.ts +56 -0
- package/dist/interface/job.interface.js +3 -0
- package/dist/interface/job.interface.js.map +1 -0
- package/dist/interface/metrics.interface.d.ts +12 -0
- package/dist/interface/metrics.interface.js +3 -0
- package/dist/interface/metrics.interface.js.map +1 -0
- package/dist/interface/queue-options.interface.d.ts +84 -0
- package/dist/interface/queue-options.interface.js +3 -0
- package/dist/interface/queue-options.interface.js.map +1 -0
- package/dist/interface/queue-provider.interface.d.ts +55 -0
- package/dist/interface/queue-provider.interface.js +7 -0
- package/dist/interface/queue-provider.interface.js.map +1 -0
- package/dist/module/index.d.ts +3 -0
- package/dist/module/index.js +20 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/queue.module-definition.d.ts +2 -0
- package/dist/module/queue.module-definition.js +11 -0
- package/dist/module/queue.module-definition.js.map +1 -0
- package/dist/module/queue.module.d.ts +3 -0
- package/dist/module/queue.module.js +26 -0
- package/dist/module/queue.module.js.map +1 -0
- package/dist/module/queue.provider.d.ts +2 -0
- package/dist/module/queue.provider.js +57 -0
- package/dist/module/queue.provider.js.map +1 -0
- package/dist/provider/bullmq/bullmq.options.d.ts +12 -0
- package/dist/provider/bullmq/bullmq.options.js +3 -0
- package/dist/provider/bullmq/bullmq.options.js.map +1 -0
- package/dist/provider/bullmq/bullmq.provider.d.ts +52 -0
- package/dist/provider/bullmq/bullmq.provider.js +476 -0
- package/dist/provider/bullmq/bullmq.provider.js.map +1 -0
- package/dist/provider/bullmq/index.d.ts +1 -0
- package/dist/provider/bullmq/index.js +18 -0
- package/dist/provider/bullmq/index.js.map +1 -0
- package/dist/provider/index.d.ts +4 -0
- package/dist/provider/index.js +21 -0
- package/dist/provider/index.js.map +1 -0
- package/dist/provider/kafka/index.d.ts +1 -0
- package/dist/provider/kafka/index.js +18 -0
- package/dist/provider/kafka/index.js.map +1 -0
- package/dist/provider/kafka/kafka.provider.d.ts +59 -0
- package/dist/provider/kafka/kafka.provider.js +598 -0
- package/dist/provider/kafka/kafka.provider.js.map +1 -0
- package/dist/provider/rabbitmq/index.d.ts +1 -0
- package/dist/provider/rabbitmq/index.js +18 -0
- package/dist/provider/rabbitmq/index.js.map +1 -0
- package/dist/provider/rabbitmq/rabbitmq.provider.d.ts +48 -0
- package/dist/provider/rabbitmq/rabbitmq.provider.js +498 -0
- package/dist/provider/rabbitmq/rabbitmq.provider.js.map +1 -0
- package/dist/provider/redis-pubsub/index.d.ts +1 -0
- package/dist/provider/redis-pubsub/index.js +18 -0
- package/dist/provider/redis-pubsub/index.js.map +1 -0
- package/dist/provider/redis-pubsub/redis-pubsub.provider.d.ts +40 -0
- package/dist/provider/redis-pubsub/redis-pubsub.provider.js +226 -0
- package/dist/provider/redis-pubsub/redis-pubsub.provider.js.map +1 -0
- package/dist/service/index.d.ts +1 -0
- package/dist/service/index.js +18 -0
- package/dist/service/index.js.map +1 -0
- package/dist/service/queue.service.d.ts +36 -0
- package/dist/service/queue.service.js +89 -0
- package/dist/service/queue.service.js.map +1 -0
- package/dist/store/in-memory-idempotency.store.d.ts +15 -0
- package/dist/store/in-memory-idempotency.store.js +84 -0
- package/dist/store/in-memory-idempotency.store.js.map +1 -0
- package/dist/store/index.d.ts +1 -0
- package/dist/store/index.js +6 -0
- package/dist/store/index.js.map +1 -0
- package/dist/utils/circuit-breaker.d.ts +30 -0
- package/dist/utils/circuit-breaker.js +133 -0
- package/dist/utils/circuit-breaker.js.map +1 -0
- package/dist/utils/delayed-scheduler.d.ts +16 -0
- package/dist/utils/delayed-scheduler.js +100 -0
- package/dist/utils/delayed-scheduler.js.map +1 -0
- package/dist/utils/errors.d.ts +23 -0
- package/dist/utils/errors.js +85 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +22 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/lock-renewal.d.ts +14 -0
- package/dist/utils/lock-renewal.js +61 -0
- package/dist/utils/lock-renewal.js.map +1 -0
- package/dist/utils/redis-idempotency-store.d.ts +21 -0
- package/dist/utils/redis-idempotency-store.js +88 -0
- package/dist/utils/redis-idempotency-store.js.map +1 -0
- package/dist/utils/sanitize.d.ts +3 -0
- package/dist/utils/sanitize.js +55 -0
- package/dist/utils/sanitize.js.map +1 -0
- package/dist/utils/validation.d.ts +8 -0
- package/dist/utils/validation.js +60 -0
- package/dist/utils/validation.js.map +1 -0
- package/docker-compose.e2e.yml +69 -0
- package/docs/FINAL_CODE_REVIEW.md +328 -0
- package/docs/IMPLEMENTATION_PLAN.md +748 -0
- package/docs/archive/CIRCUIT_BREAKER_PLAN.md +35 -0
- package/docs/archive/CODE_REVIEW.md +596 -0
- package/docs/archive/CODE_REVIEW_V2.md +1081 -0
- package/docs/archive/CODE_REVIEW_V3.md +402 -0
- package/docs/archive/IMPLEMENTATION_PLAN.md +505 -0
- package/docs/archive/IMPLEMENTATION_PLAN_BUGFIX.md +428 -0
- package/package.json +70 -0
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
# Implementation Plan: nestjs-queue Enhancements
|
|
2
|
+
|
|
3
|
+
**Date**: 2026-02-08
|
|
4
|
+
**Based on**: CODE_REVIEW_V3.md
|
|
5
|
+
**Estimated Total Effort**: 4-6 hours
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Phase 1: Quick Wins (1-2 hours)
|
|
10
|
+
|
|
11
|
+
### Task 1.1: Add `off()` Method for Event Listeners ✅ COMPLETE
|
|
12
|
+
|
|
13
|
+
**Files to Modify**:
|
|
14
|
+
- `lib/interface/queue-provider.interface.ts`
|
|
15
|
+
- `lib/provider/bullmq/bullmq.provider.ts`
|
|
16
|
+
- `lib/provider/kafka/kafka.provider.ts`
|
|
17
|
+
- `lib/provider/rabbitmq/rabbitmq.provider.ts`
|
|
18
|
+
- `lib/service/queue.service.ts`
|
|
19
|
+
|
|
20
|
+
**Implementation Steps**:
|
|
21
|
+
|
|
22
|
+
1. **Update Interface** (`queue-provider.interface.ts`):
|
|
23
|
+
```typescript
|
|
24
|
+
/**
|
|
25
|
+
* Unsubscribe from queue events
|
|
26
|
+
*/
|
|
27
|
+
off(
|
|
28
|
+
queueName: string,
|
|
29
|
+
event: QueueEvent,
|
|
30
|
+
callback: (job: IJob, result?: any) => void,
|
|
31
|
+
): void;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Remove all event listeners for a queue
|
|
35
|
+
*/
|
|
36
|
+
removeAllListeners(queueName: string): void;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
2. **BullMQ Implementation**:
|
|
40
|
+
```typescript
|
|
41
|
+
// Add listener registry to track original -> wrapped mappings
|
|
42
|
+
private readonly listenerRegistry: Map<Function, { queueName: string; event: QueueEvent; wrapped: Function }> = new Map();
|
|
43
|
+
|
|
44
|
+
on(queueName, event, callback, filter?) {
|
|
45
|
+
const wrappedCallback = async ({ jobId, returnvalue, failedReason }) => {
|
|
46
|
+
// ... existing logic
|
|
47
|
+
};
|
|
48
|
+
this.listenerRegistry.set(callback, { queueName, event, wrapped: wrappedCallback });
|
|
49
|
+
queueEvents.on(bullEvent, wrappedCallback);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
off(queueName, event, callback) {
|
|
53
|
+
const entry = this.listenerRegistry.get(callback);
|
|
54
|
+
if (entry && entry.queueName === queueName && entry.event === event) {
|
|
55
|
+
const queueEvents = this.queueEvents.get(queueName);
|
|
56
|
+
if (queueEvents) {
|
|
57
|
+
queueEvents.off(this.mapEventToBullMQ(event), entry.wrapped as any);
|
|
58
|
+
}
|
|
59
|
+
this.listenerRegistry.delete(callback);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
removeAllListeners(queueName) {
|
|
64
|
+
for (const [callback, entry] of this.listenerRegistry) {
|
|
65
|
+
if (entry.queueName === queueName) {
|
|
66
|
+
this.off(queueName, entry.event, callback);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
3. **Kafka/RabbitMQ Implementation** (simpler - internal emitter):
|
|
73
|
+
```typescript
|
|
74
|
+
off(queueName, event, callback) {
|
|
75
|
+
const queueListeners = this.eventListeners.get(queueName);
|
|
76
|
+
if (!queueListeners) return;
|
|
77
|
+
|
|
78
|
+
const listeners = queueListeners.get(event);
|
|
79
|
+
if (!listeners) return;
|
|
80
|
+
|
|
81
|
+
const index = listeners.indexOf(callback);
|
|
82
|
+
if (index > -1) {
|
|
83
|
+
listeners.splice(index, 1);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
removeAllListeners(queueName) {
|
|
88
|
+
this.eventListeners.delete(queueName);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
4. **Update QueueService**:
|
|
93
|
+
```typescript
|
|
94
|
+
off(queueName: string, event: QueueEvent, callback: (job: IJob, result?: any) => void): void {
|
|
95
|
+
return this.provider.off(queueName, event, callback);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
removeAllListeners(queueName: string): void {
|
|
99
|
+
return this.provider.removeAllListeners(queueName);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**Tests to Add**: `test/e2e/event-listener.e2e-spec.ts`
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
### Task 1.2: Kafka CorrelationId Propagation
|
|
108
|
+
|
|
109
|
+
**Files to Modify**:
|
|
110
|
+
- `lib/provider/kafka/kafka.provider.ts`
|
|
111
|
+
|
|
112
|
+
**Implementation Steps**:
|
|
113
|
+
|
|
114
|
+
1. **In `add()` method** (around line 280):
|
|
115
|
+
```typescript
|
|
116
|
+
const headers: Record<string, string> = {
|
|
117
|
+
'x-job-name': jobName,
|
|
118
|
+
'x-job-id': job.id,
|
|
119
|
+
'x-attempts': '0',
|
|
120
|
+
'x-max-attempts': String(opts?.attempts || 1),
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// Add correlationId
|
|
124
|
+
if (opts?.correlationId) {
|
|
125
|
+
headers['x-correlation-id'] = opts.correlationId;
|
|
126
|
+
job.correlationId = opts.correlationId;
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
2. **In `process()` eachMessage** (around line 350):
|
|
131
|
+
```typescript
|
|
132
|
+
const job: IJob<T> = JSON.parse(message.value.toString());
|
|
133
|
+
|
|
134
|
+
// Extract correlationId from headers
|
|
135
|
+
job.correlationId = message.headers?.['x-correlation-id']?.toString();
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
3. **In retry logic** (around line 380):
|
|
139
|
+
```typescript
|
|
140
|
+
await this.add(queueName, job.name, job.data, {
|
|
141
|
+
...job.opts,
|
|
142
|
+
delay,
|
|
143
|
+
jobId: job.id,
|
|
144
|
+
correlationId: job.correlationId, // Preserve on retry
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
### Task 1.3: Improve Duplicate Worker Handling
|
|
151
|
+
|
|
152
|
+
**Files to Modify**:
|
|
153
|
+
- `lib/provider/bullmq/bullmq.provider.ts`
|
|
154
|
+
- `lib/provider/kafka/kafka.provider.ts`
|
|
155
|
+
|
|
156
|
+
**Implementation**:
|
|
157
|
+
```typescript
|
|
158
|
+
// Change return type to boolean
|
|
159
|
+
async process<T>(...): Promise<boolean> {
|
|
160
|
+
if (this.workers.has(workerKey)) {
|
|
161
|
+
this.logger.warn(`Worker already exists for ${workerKey}`);
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
// ... existing logic
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
**Note**: Update interface signature to `Promise<void | boolean>` for backward compatibility.
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Phase 2: Health Checks (1 hour)
|
|
174
|
+
|
|
175
|
+
### Task 2.1: Add Health Check Interface
|
|
176
|
+
|
|
177
|
+
**Files to Create/Modify**:
|
|
178
|
+
- `lib/interface/health.interface.ts` (new)
|
|
179
|
+
- `lib/interface/queue-provider.interface.ts`
|
|
180
|
+
- All provider files
|
|
181
|
+
- `lib/service/queue.service.ts`
|
|
182
|
+
|
|
183
|
+
**New Interface**:
|
|
184
|
+
```typescript
|
|
185
|
+
// lib/interface/health.interface.ts
|
|
186
|
+
export interface ConnectionHealth {
|
|
187
|
+
name: string;
|
|
188
|
+
status: 'connected' | 'disconnected' | 'error';
|
|
189
|
+
error?: string;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export interface HealthCheckResult {
|
|
193
|
+
healthy: boolean;
|
|
194
|
+
provider: string;
|
|
195
|
+
connections: ConnectionHealth[];
|
|
196
|
+
latencyMs?: number;
|
|
197
|
+
timestamp: number;
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**BullMQ Implementation**:
|
|
202
|
+
```typescript
|
|
203
|
+
async isHealthy(): Promise<HealthCheckResult> {
|
|
204
|
+
const timestamp = Date.now();
|
|
205
|
+
try {
|
|
206
|
+
// Use any existing queue or create a temp one
|
|
207
|
+
const queueName = this.queues.keys().next().value || '__health__';
|
|
208
|
+
const queue = this.getQueue(queueName);
|
|
209
|
+
|
|
210
|
+
const start = Date.now();
|
|
211
|
+
await queue.client.ping();
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
healthy: true,
|
|
215
|
+
provider: 'bullmq',
|
|
216
|
+
connections: [{ name: 'redis', status: 'connected' }],
|
|
217
|
+
latencyMs: Date.now() - start,
|
|
218
|
+
timestamp,
|
|
219
|
+
};
|
|
220
|
+
} catch (error: any) {
|
|
221
|
+
return {
|
|
222
|
+
healthy: false,
|
|
223
|
+
provider: 'bullmq',
|
|
224
|
+
connections: [{ name: 'redis', status: 'error', error: error.message }],
|
|
225
|
+
timestamp,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
**Kafka Implementation**:
|
|
232
|
+
```typescript
|
|
233
|
+
async isHealthy(): Promise<HealthCheckResult> {
|
|
234
|
+
const timestamp = Date.now();
|
|
235
|
+
const connections: ConnectionHealth[] = [];
|
|
236
|
+
|
|
237
|
+
try {
|
|
238
|
+
const start = Date.now();
|
|
239
|
+
const admin = await this.ensureAdmin();
|
|
240
|
+
await admin.listTopics(); // Simple connectivity check
|
|
241
|
+
|
|
242
|
+
connections.push({ name: 'broker', status: 'connected' });
|
|
243
|
+
|
|
244
|
+
if (this.isProducerConnected) {
|
|
245
|
+
connections.push({ name: 'producer', status: 'connected' });
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
for (const [name] of this.consumers) {
|
|
249
|
+
connections.push({ name: `consumer:${name}`, status: 'connected' });
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
healthy: true,
|
|
254
|
+
provider: 'kafka',
|
|
255
|
+
connections,
|
|
256
|
+
latencyMs: Date.now() - start,
|
|
257
|
+
timestamp,
|
|
258
|
+
};
|
|
259
|
+
} catch (error: any) {
|
|
260
|
+
return {
|
|
261
|
+
healthy: false,
|
|
262
|
+
provider: 'kafka',
|
|
263
|
+
connections: [{ name: 'broker', status: 'error', error: error.message }],
|
|
264
|
+
timestamp,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
**RabbitMQ Implementation**:
|
|
271
|
+
```typescript
|
|
272
|
+
async isHealthy(): Promise<HealthCheckResult> {
|
|
273
|
+
const timestamp = Date.now();
|
|
274
|
+
|
|
275
|
+
const isConnected = this.connection.isConnected();
|
|
276
|
+
const connections: ConnectionHealth[] = [
|
|
277
|
+
{ name: 'amqp', status: isConnected ? 'connected' : 'disconnected' }
|
|
278
|
+
];
|
|
279
|
+
|
|
280
|
+
for (const [name] of this.channels) {
|
|
281
|
+
connections.push({ name: `channel:${name}`, status: 'connected' });
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
healthy: isConnected,
|
|
286
|
+
provider: 'rabbitmq',
|
|
287
|
+
connections,
|
|
288
|
+
timestamp,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
## Phase 3: Kafka Idempotency (1 hour)
|
|
296
|
+
|
|
297
|
+
### Task 3.1: Add Idempotency Support to Kafka
|
|
298
|
+
|
|
299
|
+
**Files to Modify**:
|
|
300
|
+
- `lib/interface/queue-options.interface.ts`
|
|
301
|
+
- `lib/provider/kafka/kafka.provider.ts`
|
|
302
|
+
|
|
303
|
+
**Update KafkaQueueOptions**:
|
|
304
|
+
```typescript
|
|
305
|
+
export interface KafkaQueueOptions {
|
|
306
|
+
// ... existing options
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Idempotency store configuration (optional)
|
|
310
|
+
* Prevents duplicate processing during consumer group rebalancing
|
|
311
|
+
*/
|
|
312
|
+
idempotency?: IdempotencyStoreOptions;
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
**Update Kafka process()**:
|
|
317
|
+
```typescript
|
|
318
|
+
async process<T>(...): Promise<void> {
|
|
319
|
+
const idempotencyStore = this.options.idempotency?.store;
|
|
320
|
+
const lockTtl = this.options.idempotency?.lockTtl ?? 60000;
|
|
321
|
+
const completedTtl = this.options.idempotency?.completedTtl ?? 3600000;
|
|
322
|
+
|
|
323
|
+
// ... in eachMessage:
|
|
324
|
+
|
|
325
|
+
// Idempotency check
|
|
326
|
+
if (idempotencyStore) {
|
|
327
|
+
const canProcess = await idempotencyStore.markProcessing(job.id, lockTtl);
|
|
328
|
+
if (!canProcess) {
|
|
329
|
+
this.logger.debug(`Job ${job.id} skipped (idempotency check)`);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
try {
|
|
335
|
+
// ... existing processing logic
|
|
336
|
+
|
|
337
|
+
if (idempotencyStore) {
|
|
338
|
+
await idempotencyStore.markComplete(job.id, completedTtl);
|
|
339
|
+
}
|
|
340
|
+
} catch (error) {
|
|
341
|
+
if (idempotencyStore) {
|
|
342
|
+
await idempotencyStore.releaseLock(job.id);
|
|
343
|
+
}
|
|
344
|
+
// ... existing error handling
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
---
|
|
350
|
+
|
|
351
|
+
## Phase 4: Metrics Hooks (1 hour)
|
|
352
|
+
|
|
353
|
+
### Task 4.1: Add Optional Metrics Interface
|
|
354
|
+
|
|
355
|
+
**Files to Create/Modify**:
|
|
356
|
+
- `lib/interface/metrics.interface.ts` (new)
|
|
357
|
+
- `lib/interface/queue-options.interface.ts`
|
|
358
|
+
- All provider files
|
|
359
|
+
|
|
360
|
+
**New Interface**:
|
|
361
|
+
```typescript
|
|
362
|
+
// lib/interface/metrics.interface.ts
|
|
363
|
+
export interface QueueMetrics {
|
|
364
|
+
/**
|
|
365
|
+
* Called when a job is added to the queue
|
|
366
|
+
*/
|
|
367
|
+
jobAdded(queueName: string, jobName: string, jobId: string, opts?: { delay?: number; priority?: number }): void;
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Called when job processing starts
|
|
371
|
+
*/
|
|
372
|
+
jobStarted(queueName: string, jobName: string, jobId: string): void;
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Called when job processing completes successfully
|
|
376
|
+
*/
|
|
377
|
+
jobCompleted(queueName: string, jobName: string, jobId: string, durationMs: number): void;
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Called when job processing fails
|
|
381
|
+
*/
|
|
382
|
+
jobFailed(queueName: string, jobName: string, jobId: string, error: string, attempt: number): void;
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Called when a job is retried
|
|
386
|
+
*/
|
|
387
|
+
jobRetried(queueName: string, jobName: string, jobId: string, attempt: number, delay: number): void;
|
|
388
|
+
}
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
**Usage in Providers**:
|
|
392
|
+
```typescript
|
|
393
|
+
// In constructor, store metrics reference
|
|
394
|
+
private readonly metrics?: QueueMetrics;
|
|
395
|
+
|
|
396
|
+
constructor(options: BullMQOptions) {
|
|
397
|
+
this.metrics = options.metrics;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// In add()
|
|
401
|
+
this.metrics?.jobAdded(queueName, jobName, job.id, { delay: opts?.delay, priority: opts?.priority });
|
|
402
|
+
|
|
403
|
+
// In process()
|
|
404
|
+
this.metrics?.jobStarted(queueName, job.name, job.id);
|
|
405
|
+
const start = Date.now();
|
|
406
|
+
try {
|
|
407
|
+
const result = await processor(job);
|
|
408
|
+
this.metrics?.jobCompleted(queueName, job.name, job.id, Date.now() - start);
|
|
409
|
+
} catch (error) {
|
|
410
|
+
this.metrics?.jobFailed(queueName, job.name, job.id, error.message, job.attemptsMade);
|
|
411
|
+
}
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
---
|
|
415
|
+
|
|
416
|
+
## Phase 5: Additional Improvements (30 min)
|
|
417
|
+
|
|
418
|
+
### Task 5.1: ProcessorExplorer Error Context
|
|
419
|
+
|
|
420
|
+
**File**: `lib/explorer/processor.explorer.ts`
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
const routerHandler = async (job: IJob): Promise<any> => {
|
|
424
|
+
const namedHandler = namedHandlers.get(job.name);
|
|
425
|
+
if (namedHandler) {
|
|
426
|
+
try {
|
|
427
|
+
return await namedHandler(job);
|
|
428
|
+
} catch (error: any) {
|
|
429
|
+
// Enrich error with context
|
|
430
|
+
error.queueContext = {
|
|
431
|
+
queueName,
|
|
432
|
+
jobName: job.name,
|
|
433
|
+
jobId: job.id,
|
|
434
|
+
correlationId: job.correlationId,
|
|
435
|
+
};
|
|
436
|
+
throw error;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
// ...
|
|
440
|
+
};
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
### Task 5.2: Expose ShutdownOptions
|
|
444
|
+
|
|
445
|
+
**File**: `lib/service/queue.service.ts`
|
|
446
|
+
|
|
447
|
+
```typescript
|
|
448
|
+
constructor(
|
|
449
|
+
@Inject(QUEUE_PROVIDER) private readonly provider: IQueueProvider,
|
|
450
|
+
@Optional() @Inject(MODULE_OPTIONS_TOKEN) private readonly moduleOptions?: QueueModuleOptions,
|
|
451
|
+
) {}
|
|
452
|
+
|
|
453
|
+
async onModuleDestroy(): Promise<void> {
|
|
454
|
+
this.logger.log('Closing queue connections...');
|
|
455
|
+
await this.provider.close(this.moduleOptions?.shutdownOptions);
|
|
456
|
+
}
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
---
|
|
460
|
+
|
|
461
|
+
## Test Plan
|
|
462
|
+
|
|
463
|
+
### New E2E Tests to Create:
|
|
464
|
+
|
|
465
|
+
1. **`test/e2e/event-listener.e2e-spec.ts`**
|
|
466
|
+
- Test `on()` registers listener
|
|
467
|
+
- Test `off()` removes specific listener
|
|
468
|
+
- Test `removeAllListeners()` removes all for a queue
|
|
469
|
+
- Test listeners don't fire after removal
|
|
470
|
+
|
|
471
|
+
2. **`test/e2e/health-check.e2e-spec.ts`**
|
|
472
|
+
- Test `isHealthy()` returns true when connected
|
|
473
|
+
- Test `isHealthy()` returns false when disconnected
|
|
474
|
+
- Test latency measurement
|
|
475
|
+
|
|
476
|
+
3. **`test/e2e/kafka-correlation.e2e-spec.ts`**
|
|
477
|
+
- Test correlationId is propagated through Kafka
|
|
478
|
+
- Test correlationId persists through retries
|
|
479
|
+
|
|
480
|
+
---
|
|
481
|
+
|
|
482
|
+
## Execution Order
|
|
483
|
+
|
|
484
|
+
```
|
|
485
|
+
Phase 1 (Quick Wins) → Phase 2 (Health) → Phase 3 (Kafka Idempotency) → Phase 4 (Metrics)
|
|
486
|
+
↓ ↓ ↓ ↓
|
|
487
|
+
1-2 hours 1 hour 1 hour 1 hour
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
**Total Estimated Time**: 4-6 hours
|
|
491
|
+
|
|
492
|
+
---
|
|
493
|
+
|
|
494
|
+
## Commands for Claude Code
|
|
495
|
+
|
|
496
|
+
```bash
|
|
497
|
+
# Phase 1 - Quick wins
|
|
498
|
+
claude --dangerously-skip-permissions -p "Implement the off() method and removeAllListeners() for all queue providers as described in IMPLEMENTATION_PLAN.md Phase 1.1. Update the interface, all three providers, and QueueService. When done, run: openclaw gateway wake --text 'Phase 1.1 complete: off() method implemented' --mode now"
|
|
499
|
+
|
|
500
|
+
# Phase 1.2 - Kafka correlationId
|
|
501
|
+
claude --dangerously-skip-permissions -p "Add correlationId propagation to Kafka provider as described in IMPLEMENTATION_PLAN.md Phase 1.2. When done, run: openclaw gateway wake --text 'Phase 1.2 complete: Kafka correlationId' --mode now"
|
|
502
|
+
|
|
503
|
+
# Phase 2 - Health checks
|
|
504
|
+
claude --dangerously-skip-permissions -p "Implement health check interface and isHealthy() method for all providers as described in IMPLEMENTATION_PLAN.md Phase 2. When done, run: openclaw gateway wake --text 'Phase 2 complete: Health checks' --mode now"
|
|
505
|
+
```
|