@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,748 @@
|
|
|
1
|
+
# Implementation Plan: Code Review Findings
|
|
2
|
+
|
|
3
|
+
**Created**: 2026-02-09
|
|
4
|
+
**Based on**: FINAL_CODE_REVIEW.md
|
|
5
|
+
**Branch**: `queue-module`
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
This plan addresses the critical findings from the code review, prioritized by risk and impact.
|
|
12
|
+
|
|
13
|
+
| Phase | Items | Effort | Risk Mitigation |
|
|
14
|
+
|-------|-------|--------|-----------------|
|
|
15
|
+
| 0 | **Unit Test Coverage (80% threshold)** | 4h | Quality gate |
|
|
16
|
+
| 1 | In-Memory Idempotency Store | 2h | P1 - Duplicate processing |
|
|
17
|
+
| 2 | Idempotency Warning + Default Store | 1h | P1 - Production safety |
|
|
18
|
+
| 3 | RabbitMQ Original Job ID Fix | 1h | P2 - Retry idempotency |
|
|
19
|
+
| 4 | Lock Renewal for Long Jobs | 3h | P2 - Lock expiry duplicates |
|
|
20
|
+
| 5 | Persistent Delay Store (Optional) | 4h | P1 - Delayed job loss |
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Phase 0: Unit Test Coverage (PRIORITY)
|
|
25
|
+
|
|
26
|
+
### Goal
|
|
27
|
+
Meet 80% coverage threshold for statements, branches, lines, and functions.
|
|
28
|
+
|
|
29
|
+
### Current Coverage Gaps
|
|
30
|
+
|
|
31
|
+
| File | Current | Target | Lines to Cover |
|
|
32
|
+
|------|---------|--------|----------------|
|
|
33
|
+
| `utils/delayed-scheduler.ts` | 3.63% | 80% | 36-175 |
|
|
34
|
+
| `utils/redis-idempotency-store.ts` | 0% | 80% | 1-115 |
|
|
35
|
+
| `utils/sanitize.ts` | 0% | 80% | 4-85 |
|
|
36
|
+
| `utils/validation.ts` | 0% | 80% | 1-109 |
|
|
37
|
+
| `utils/errors.ts` | 46.66% | 80% | 16,26-27,40-46,55-56,95,104-105,113-126,140-156 |
|
|
38
|
+
| `explorer/processor.explorer.ts` | 66.66% | 80% | 65,71,181,210-233,249-277,281-302,324-329,368-381 |
|
|
39
|
+
| `provider/bullmq/bullmq.provider.ts` | 68.57% | 80% | 94,140,160-161,273-307,etc. |
|
|
40
|
+
| `service/queue.service.ts` | 80.64% | 80% | ✅ (minor gaps: 178-187,203-217) |
|
|
41
|
+
|
|
42
|
+
### Implementation
|
|
43
|
+
|
|
44
|
+
#### 0.1 `utils/sanitize.ts` Tests (0% → 80%)
|
|
45
|
+
|
|
46
|
+
**File**: `test/utils/sanitize.spec.ts`
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
describe('sanitize utilities', () => {
|
|
50
|
+
describe('sanitizeCorrelationId', () => {
|
|
51
|
+
it('should pass valid UUIDs');
|
|
52
|
+
it('should pass valid alphanumeric IDs');
|
|
53
|
+
it('should reject IDs with special characters');
|
|
54
|
+
it('should reject IDs exceeding max length');
|
|
55
|
+
it('should handle empty strings');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### 0.2 `utils/validation.ts` Tests (0% → 80%)
|
|
61
|
+
|
|
62
|
+
**File**: `test/utils/validation.spec.ts`
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
describe('validation utilities', () => {
|
|
66
|
+
describe('validateJobStructure', () => {
|
|
67
|
+
it('should pass valid job with id, name, data');
|
|
68
|
+
it('should reject job missing id');
|
|
69
|
+
it('should reject job missing name');
|
|
70
|
+
it('should reject job with invalid data type');
|
|
71
|
+
it('should handle nested data objects');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### 0.3 `utils/redis-idempotency-store.ts` Tests (0% → 80%)
|
|
77
|
+
|
|
78
|
+
**File**: `test/utils/redis-idempotency-store.spec.ts`
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// Mock ioredis
|
|
82
|
+
jest.mock('ioredis');
|
|
83
|
+
|
|
84
|
+
describe('RedisIdempotencyStore', () => {
|
|
85
|
+
describe('markProcessing', () => {
|
|
86
|
+
it('should acquire lock with SET NX');
|
|
87
|
+
it('should return false if lock exists');
|
|
88
|
+
it('should set TTL correctly');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('markComplete', () => {
|
|
92
|
+
it('should store completion marker');
|
|
93
|
+
it('should set result TTL');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('isProcessed', () => {
|
|
97
|
+
it('should return true if completed');
|
|
98
|
+
it('should return false if not found');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('releaseLock', () => {
|
|
102
|
+
it('should delete lock key');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('close', () => {
|
|
106
|
+
it('should disconnect Redis client');
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### 0.4 `utils/delayed-scheduler.ts` Tests (3.63% → 80%)
|
|
112
|
+
|
|
113
|
+
**File**: `test/utils/delayed-scheduler.spec.ts`
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
describe('DelayedJobScheduler', () => {
|
|
117
|
+
beforeEach(() => jest.useFakeTimers());
|
|
118
|
+
afterEach(() => jest.useRealTimers());
|
|
119
|
+
|
|
120
|
+
describe('schedule', () => {
|
|
121
|
+
it('should schedule job for future execution');
|
|
122
|
+
it('should execute job after delay');
|
|
123
|
+
it('should handle multiple jobs');
|
|
124
|
+
it('should deduplicate same job ID');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('cancel', () => {
|
|
128
|
+
it('should cancel scheduled job');
|
|
129
|
+
it('should handle non-existent job');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe('shutdown', () => {
|
|
133
|
+
it('should clear all pending jobs');
|
|
134
|
+
it('should execute pending if executeImmediately: true');
|
|
135
|
+
it('should discard pending if executeImmediately: false');
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
#### 0.5 `utils/errors.ts` Tests (46.66% → 80%)
|
|
141
|
+
|
|
142
|
+
**File**: `test/utils/errors.spec.ts`
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
describe('error utilities', () => {
|
|
146
|
+
describe('QueueError', () => {
|
|
147
|
+
it('should create error with code');
|
|
148
|
+
it('should include cause if provided');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe('CircuitBreakerOpenError', () => {
|
|
152
|
+
it('should create with queue name');
|
|
153
|
+
it('should include retry info');
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe('isRetryableError', () => {
|
|
157
|
+
it('should return true for network errors');
|
|
158
|
+
it('should return false for validation errors');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('ShutdownOptions validation', () => {
|
|
162
|
+
it('should validate timeout');
|
|
163
|
+
it('should validate force flag');
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### 0.6 `explorer/processor.explorer.ts` Additional Tests (66% → 80%)
|
|
169
|
+
|
|
170
|
+
**File**: `test/processor.explorer.spec.ts` (extend existing)
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
describe('ProcessorExplorer (additional)', () => {
|
|
174
|
+
describe('circuit breaker integration', () => {
|
|
175
|
+
it('should open circuit after threshold failures');
|
|
176
|
+
it('should reject jobs when circuit is open');
|
|
177
|
+
it('should half-open after reset timeout');
|
|
178
|
+
it('should close after successful recovery');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe('error handling', () => {
|
|
182
|
+
it('should handle processor throwing errors');
|
|
183
|
+
it('should handle async processor rejection');
|
|
184
|
+
it('should call onModuleDestroy cleanup');
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe('resetCircuitBreaker', () => {
|
|
188
|
+
it('should reset single queue breaker');
|
|
189
|
+
it('should handle non-existent queue');
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe('resetAllCircuitBreakers', () => {
|
|
193
|
+
it('should reset all queue breakers');
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### 0.7 `provider/bullmq/bullmq.provider.ts` Additional Tests (68% → 80%)
|
|
199
|
+
|
|
200
|
+
**File**: `test/provider/bullmq.provider.spec.ts` (extend existing)
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
describe('BullMQProvider (additional)', () => {
|
|
204
|
+
describe('event handling', () => {
|
|
205
|
+
it('should emit completed event');
|
|
206
|
+
it('should emit failed event');
|
|
207
|
+
it('should emit progress event');
|
|
208
|
+
it('should filter events by jobName');
|
|
209
|
+
it('should filter events by jobId');
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('off', () => {
|
|
213
|
+
it('should remove specific listener');
|
|
214
|
+
it('should handle non-existent listener');
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe('removeAllListeners', () => {
|
|
218
|
+
it('should remove all listeners for queue');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe('isHealthy', () => {
|
|
222
|
+
it('should return healthy when connected');
|
|
223
|
+
it('should return unhealthy when disconnected');
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe('close', () => {
|
|
227
|
+
it('should close all queues');
|
|
228
|
+
it('should close all workers');
|
|
229
|
+
it('should close all QueueEvents');
|
|
230
|
+
it('should clear listener registry');
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Test Execution
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
# Run with coverage
|
|
239
|
+
npm run test:cov
|
|
240
|
+
|
|
241
|
+
# Target specific files
|
|
242
|
+
npm test -- --coverage --collectCoverageFrom='lib/utils/**/*.ts'
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Acceptance Criteria
|
|
246
|
+
|
|
247
|
+
- [ ] All utils files ≥80% coverage
|
|
248
|
+
- [ ] `processor.explorer.ts` ≥80% coverage
|
|
249
|
+
- [ ] `bullmq.provider.ts` ≥80% coverage
|
|
250
|
+
- [ ] Global coverage ≥80% for all metrics
|
|
251
|
+
- [ ] No test regressions
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## Phase 1: In-Memory Idempotency Store (Single Pod)
|
|
256
|
+
|
|
257
|
+
### Goal
|
|
258
|
+
Provide a zero-config idempotency store for single-pod deployments and development.
|
|
259
|
+
|
|
260
|
+
### Implementation
|
|
261
|
+
|
|
262
|
+
#### 1.1 Create `InMemoryIdempotencyStore`
|
|
263
|
+
|
|
264
|
+
**File**: `lib/store/in-memory-idempotency.store.ts`
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
import { IdempotencyStore } from '../interface/idempotency.interface';
|
|
268
|
+
|
|
269
|
+
interface LockEntry {
|
|
270
|
+
expiresAt: number;
|
|
271
|
+
completed: boolean;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export class InMemoryIdempotencyStore implements IdempotencyStore {
|
|
275
|
+
private readonly locks = new Map<string, LockEntry>();
|
|
276
|
+
private cleanupInterval?: NodeJS.Timeout;
|
|
277
|
+
|
|
278
|
+
constructor(private readonly cleanupIntervalMs: number = 60_000) {
|
|
279
|
+
this.startCleanup();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async markProcessing(jobId: string, lockTtlMs: number): Promise<boolean> {
|
|
283
|
+
const now = Date.now();
|
|
284
|
+
const existing = this.locks.get(jobId);
|
|
285
|
+
|
|
286
|
+
// Already completed - skip
|
|
287
|
+
if (existing?.completed && existing.expiresAt > now) {
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Lock active - skip
|
|
292
|
+
if (existing && !existing.completed && existing.expiresAt > now) {
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Acquire lock
|
|
297
|
+
this.locks.set(jobId, {
|
|
298
|
+
expiresAt: now + lockTtlMs,
|
|
299
|
+
completed: false,
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
async markComplete(jobId: string, completedTtlMs: number): Promise<void> {
|
|
306
|
+
this.locks.set(jobId, {
|
|
307
|
+
expiresAt: Date.now() + completedTtlMs,
|
|
308
|
+
completed: true,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async releaseLock(jobId: string): Promise<void> {
|
|
313
|
+
this.locks.delete(jobId);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
async extendLock(jobId: string, additionalTtlMs: number): Promise<boolean> {
|
|
317
|
+
const existing = this.locks.get(jobId);
|
|
318
|
+
if (!existing || existing.completed) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
existing.expiresAt = Date.now() + additionalTtlMs;
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private startCleanup(): void {
|
|
327
|
+
this.cleanupInterval = setInterval(() => {
|
|
328
|
+
const now = Date.now();
|
|
329
|
+
for (const [jobId, entry] of this.locks) {
|
|
330
|
+
if (entry.expiresAt < now) {
|
|
331
|
+
this.locks.delete(jobId);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}, this.cleanupIntervalMs);
|
|
335
|
+
|
|
336
|
+
// Don't prevent process exit
|
|
337
|
+
this.cleanupInterval.unref();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async close(): Promise<void> {
|
|
341
|
+
if (this.cleanupInterval) {
|
|
342
|
+
clearInterval(this.cleanupInterval);
|
|
343
|
+
this.cleanupInterval = undefined;
|
|
344
|
+
}
|
|
345
|
+
this.locks.clear();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// For testing
|
|
349
|
+
get size(): number {
|
|
350
|
+
return this.locks.size;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
#### 1.2 Update IdempotencyStore Interface
|
|
356
|
+
|
|
357
|
+
**File**: `lib/interface/idempotency.interface.ts` (add `extendLock`)
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
export interface IdempotencyStore {
|
|
361
|
+
markProcessing(jobId: string, lockTtlMs: number): Promise<boolean>;
|
|
362
|
+
markComplete(jobId: string, completedTtlMs: number): Promise<void>;
|
|
363
|
+
releaseLock(jobId: string): Promise<void>;
|
|
364
|
+
extendLock?(jobId: string, additionalTtlMs: number): Promise<boolean>; // New - optional
|
|
365
|
+
close?(): Promise<void>; // New - optional cleanup
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
#### 1.3 Export from Package
|
|
370
|
+
|
|
371
|
+
**File**: `lib/index.ts`
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
export { InMemoryIdempotencyStore } from './store/in-memory-idempotency.store';
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
#### 1.4 Unit Tests
|
|
378
|
+
|
|
379
|
+
**File**: `test/store/in-memory-idempotency.store.spec.ts`
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
describe('InMemoryIdempotencyStore', () => {
|
|
383
|
+
let store: InMemoryIdempotencyStore;
|
|
384
|
+
|
|
385
|
+
beforeEach(() => {
|
|
386
|
+
store = new InMemoryIdempotencyStore(100); // Fast cleanup for tests
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
afterEach(async () => {
|
|
390
|
+
await store.close();
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
describe('markProcessing', () => {
|
|
394
|
+
it('should acquire lock for new job', async () => {
|
|
395
|
+
expect(await store.markProcessing('job-1', 5000)).toBe(true);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it('should reject duplicate lock', async () => {
|
|
399
|
+
await store.markProcessing('job-1', 5000);
|
|
400
|
+
expect(await store.markProcessing('job-1', 5000)).toBe(false);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it('should allow reprocessing after lock expires', async () => {
|
|
404
|
+
await store.markProcessing('job-1', 50);
|
|
405
|
+
await new Promise(r => setTimeout(r, 100));
|
|
406
|
+
expect(await store.markProcessing('job-1', 5000)).toBe(true);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it('should reject completed jobs', async () => {
|
|
410
|
+
await store.markProcessing('job-1', 5000);
|
|
411
|
+
await store.markComplete('job-1', 60000);
|
|
412
|
+
expect(await store.markProcessing('job-1', 5000)).toBe(false);
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
describe('extendLock', () => {
|
|
417
|
+
it('should extend active lock', async () => {
|
|
418
|
+
await store.markProcessing('job-1', 100);
|
|
419
|
+
expect(await store.extendLock('job-1', 5000)).toBe(true);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('should fail for non-existent lock', async () => {
|
|
423
|
+
expect(await store.extendLock('job-1', 5000)).toBe(false);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it('should fail for completed job', async () => {
|
|
427
|
+
await store.markProcessing('job-1', 5000);
|
|
428
|
+
await store.markComplete('job-1', 60000);
|
|
429
|
+
expect(await store.extendLock('job-1', 5000)).toBe(false);
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
describe('cleanup', () => {
|
|
434
|
+
it('should remove expired entries', async () => {
|
|
435
|
+
await store.markProcessing('job-1', 50);
|
|
436
|
+
expect(store.size).toBe(1);
|
|
437
|
+
await new Promise(r => setTimeout(r, 200));
|
|
438
|
+
expect(store.size).toBe(0);
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
---
|
|
445
|
+
|
|
446
|
+
## Phase 2: Idempotency Warning + Default Store
|
|
447
|
+
|
|
448
|
+
### Goal
|
|
449
|
+
Warn users when no idempotency store is configured; optionally auto-create in-memory store for dev mode.
|
|
450
|
+
|
|
451
|
+
### Implementation
|
|
452
|
+
|
|
453
|
+
#### 2.1 Add Warning in Providers
|
|
454
|
+
|
|
455
|
+
**All providers** (`bullmq.provider.ts`, `kafka.provider.ts`, `rabbitmq.provider.ts`, `redis-pubsub.provider.ts`):
|
|
456
|
+
|
|
457
|
+
```typescript
|
|
458
|
+
constructor(
|
|
459
|
+
@Inject(QUEUE_OPTIONS) private readonly options: QueueOptions,
|
|
460
|
+
private readonly logger: Logger,
|
|
461
|
+
) {
|
|
462
|
+
// ... existing code ...
|
|
463
|
+
|
|
464
|
+
if (!this.options.idempotency?.store) {
|
|
465
|
+
this.logger.warn(
|
|
466
|
+
'No idempotency store configured. ' +
|
|
467
|
+
'Duplicate job processing may occur during failures. ' +
|
|
468
|
+
'For production, configure an idempotency store.'
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
#### 2.2 Add `useInMemoryIdempotency` Option
|
|
475
|
+
|
|
476
|
+
**File**: `lib/interface/queue-module.interface.ts`
|
|
477
|
+
|
|
478
|
+
```typescript
|
|
479
|
+
export interface QueueModuleOptions {
|
|
480
|
+
// ... existing options ...
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Use in-memory idempotency store (single-pod only).
|
|
484
|
+
* Automatically configures InMemoryIdempotencyStore if no custom store provided.
|
|
485
|
+
* @default false
|
|
486
|
+
*/
|
|
487
|
+
useInMemoryIdempotency?: boolean;
|
|
488
|
+
}
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
#### 2.3 Auto-configure in Module
|
|
492
|
+
|
|
493
|
+
**File**: `lib/module/queue.module.ts`
|
|
494
|
+
|
|
495
|
+
```typescript
|
|
496
|
+
static forRoot(options: QueueModuleOptions): DynamicModule {
|
|
497
|
+
// Auto-configure in-memory store if requested
|
|
498
|
+
if (options.useInMemoryIdempotency && !options.idempotency?.store) {
|
|
499
|
+
options.idempotency = {
|
|
500
|
+
...options.idempotency,
|
|
501
|
+
store: new InMemoryIdempotencyStore(),
|
|
502
|
+
lockTtl: options.idempotency?.lockTtl ?? 30_000,
|
|
503
|
+
completedTtl: options.idempotency?.completedTtl ?? 3600_000,
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// ... rest of module setup ...
|
|
508
|
+
}
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
---
|
|
512
|
+
|
|
513
|
+
## Phase 3: RabbitMQ Original Job ID Fix
|
|
514
|
+
|
|
515
|
+
### Goal
|
|
516
|
+
Ensure idempotency tracking works correctly across retries.
|
|
517
|
+
|
|
518
|
+
### Implementation
|
|
519
|
+
|
|
520
|
+
#### 3.1 Preserve Original Job ID
|
|
521
|
+
|
|
522
|
+
**File**: `lib/provider/rabbitmq/rabbitmq.provider.ts`
|
|
523
|
+
|
|
524
|
+
```typescript
|
|
525
|
+
// In retry logic
|
|
526
|
+
private async retryJob(
|
|
527
|
+
queueName: string,
|
|
528
|
+
job: Job<unknown>,
|
|
529
|
+
error: Error,
|
|
530
|
+
channel: ChannelWrapper,
|
|
531
|
+
msg: Message,
|
|
532
|
+
): Promise<void> {
|
|
533
|
+
const attemptsMade = (job.attemptsMade ?? 0) + 1;
|
|
534
|
+
|
|
535
|
+
// Preserve original job ID for idempotency
|
|
536
|
+
const originalJobId = job.headers?.['x-original-job-id'] ?? job.id;
|
|
537
|
+
|
|
538
|
+
const retryJobOpts: JobOptions = {
|
|
539
|
+
...job.opts,
|
|
540
|
+
jobId: `${originalJobId}-retry-${attemptsMade}`,
|
|
541
|
+
headers: {
|
|
542
|
+
...job.headers,
|
|
543
|
+
'x-original-job-id': originalJobId, // Always preserve
|
|
544
|
+
'x-retry-count': attemptsMade.toString(),
|
|
545
|
+
},
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
// ... rest of retry logic ...
|
|
549
|
+
}
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
#### 3.2 Use Original ID for Idempotency Check
|
|
553
|
+
|
|
554
|
+
```typescript
|
|
555
|
+
// In process() message handler
|
|
556
|
+
private getIdempotencyKey(job: Job<unknown>): string {
|
|
557
|
+
// Use original job ID if this is a retry
|
|
558
|
+
return job.headers?.['x-original-job-id'] ?? job.id;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// Usage in process loop
|
|
562
|
+
const idempotencyKey = this.getIdempotencyKey(job);
|
|
563
|
+
const canProcess = await idempotencyStore.markProcessing(idempotencyKey, lockTtl);
|
|
564
|
+
if (!canProcess) {
|
|
565
|
+
this.logger.debug(`Skipping duplicate job: ${idempotencyKey}`);
|
|
566
|
+
channel.ack(msg);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// ... process ...
|
|
571
|
+
|
|
572
|
+
await idempotencyStore.markComplete(idempotencyKey, completedTtl);
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
#### 3.3 Apply Same Pattern to All Providers
|
|
576
|
+
|
|
577
|
+
Update Kafka and Redis-PubSub providers similarly.
|
|
578
|
+
|
|
579
|
+
---
|
|
580
|
+
|
|
581
|
+
## Phase 4: Lock Renewal for Long Jobs
|
|
582
|
+
|
|
583
|
+
### Goal
|
|
584
|
+
Prevent lock expiry during long-running jobs causing duplicate processing.
|
|
585
|
+
|
|
586
|
+
### Implementation
|
|
587
|
+
|
|
588
|
+
#### 4.1 Add Lock Renewal Option
|
|
589
|
+
|
|
590
|
+
**File**: `lib/interface/idempotency.interface.ts`
|
|
591
|
+
|
|
592
|
+
```typescript
|
|
593
|
+
export interface IdempotencyOptions {
|
|
594
|
+
store?: IdempotencyStore;
|
|
595
|
+
lockTtl?: number; // Default: 30000ms
|
|
596
|
+
completedTtl?: number; // Default: 3600000ms
|
|
597
|
+
renewalInterval?: number; // New: renewal interval (default: lockTtl / 2)
|
|
598
|
+
enableRenewal?: boolean; // New: enable auto-renewal (default: true)
|
|
599
|
+
}
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
#### 4.2 Create Lock Renewal Helper
|
|
603
|
+
|
|
604
|
+
**File**: `lib/util/lock-renewal.util.ts`
|
|
605
|
+
|
|
606
|
+
```typescript
|
|
607
|
+
export class LockRenewal {
|
|
608
|
+
private interval?: NodeJS.Timeout;
|
|
609
|
+
|
|
610
|
+
constructor(
|
|
611
|
+
private readonly store: IdempotencyStore,
|
|
612
|
+
private readonly jobId: string,
|
|
613
|
+
private readonly lockTtl: number,
|
|
614
|
+
private readonly renewalInterval: number,
|
|
615
|
+
) {}
|
|
616
|
+
|
|
617
|
+
start(): void {
|
|
618
|
+
if (!this.store.extendLock) {
|
|
619
|
+
return; // Store doesn't support extension
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
this.interval = setInterval(async () => {
|
|
623
|
+
try {
|
|
624
|
+
const extended = await this.store.extendLock!(this.jobId, this.lockTtl);
|
|
625
|
+
if (!extended) {
|
|
626
|
+
this.stop();
|
|
627
|
+
}
|
|
628
|
+
} catch (error) {
|
|
629
|
+
// Log but don't throw - job should continue
|
|
630
|
+
}
|
|
631
|
+
}, this.renewalInterval);
|
|
632
|
+
|
|
633
|
+
this.interval.unref();
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
stop(): void {
|
|
637
|
+
if (this.interval) {
|
|
638
|
+
clearInterval(this.interval);
|
|
639
|
+
this.interval = undefined;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
#### 4.3 Integrate into Providers
|
|
646
|
+
|
|
647
|
+
```typescript
|
|
648
|
+
// In job processing
|
|
649
|
+
const lockRenewal = new LockRenewal(
|
|
650
|
+
idempotencyStore,
|
|
651
|
+
idempotencyKey,
|
|
652
|
+
lockTtl,
|
|
653
|
+
lockTtl / 2, // Renew at half TTL
|
|
654
|
+
);
|
|
655
|
+
lockRenewal.start();
|
|
656
|
+
|
|
657
|
+
try {
|
|
658
|
+
await this.processJob(job);
|
|
659
|
+
await idempotencyStore.markComplete(idempotencyKey, completedTtl);
|
|
660
|
+
} finally {
|
|
661
|
+
lockRenewal.stop();
|
|
662
|
+
}
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
---
|
|
666
|
+
|
|
667
|
+
## Phase 5: Persistent Delay Store (Optional Enhancement)
|
|
668
|
+
|
|
669
|
+
### Goal
|
|
670
|
+
Ensure delayed jobs survive pod restarts for Kafka/RabbitMQ.
|
|
671
|
+
|
|
672
|
+
### Options
|
|
673
|
+
|
|
674
|
+
#### Option A: Redis-Backed Delay Store
|
|
675
|
+
```typescript
|
|
676
|
+
export class RedisDelayStore implements DelayStore {
|
|
677
|
+
// Store delayed jobs in Redis sorted set
|
|
678
|
+
// Poll and execute when delay expires
|
|
679
|
+
}
|
|
680
|
+
```
|
|
681
|
+
|
|
682
|
+
#### Option B: Use RabbitMQ Delayed Message Plugin
|
|
683
|
+
- Requires `rabbitmq_delayed_message_exchange` plugin
|
|
684
|
+
- Native broker-level delays
|
|
685
|
+
|
|
686
|
+
#### Option C: Route Delayed Jobs to BullMQ
|
|
687
|
+
- Use BullMQ for any job with `delay` option
|
|
688
|
+
- Hybrid approach
|
|
689
|
+
|
|
690
|
+
**Recommendation**: Document Option B for RabbitMQ users; implement Option A as separate package.
|
|
691
|
+
|
|
692
|
+
---
|
|
693
|
+
|
|
694
|
+
## Implementation Order
|
|
695
|
+
|
|
696
|
+
```
|
|
697
|
+
Phase 1: InMemoryIdempotencyStore ← START HERE
|
|
698
|
+
↓
|
|
699
|
+
Phase 2: Warning + useInMemoryIdempotency
|
|
700
|
+
↓
|
|
701
|
+
Phase 3: RabbitMQ Original Job ID
|
|
702
|
+
↓
|
|
703
|
+
Phase 4: Lock Renewal
|
|
704
|
+
↓
|
|
705
|
+
Phase 5: Persistent Delay (future)
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
---
|
|
709
|
+
|
|
710
|
+
## Changeset
|
|
711
|
+
|
|
712
|
+
After implementation, create changeset:
|
|
713
|
+
|
|
714
|
+
```bash
|
|
715
|
+
pnpm changeset
|
|
716
|
+
```
|
|
717
|
+
|
|
718
|
+
```markdown
|
|
719
|
+
---
|
|
720
|
+
"@nathapp/nestjs-queue": minor
|
|
721
|
+
---
|
|
722
|
+
|
|
723
|
+
### Added
|
|
724
|
+
- `InMemoryIdempotencyStore` for single-pod deployments
|
|
725
|
+
- `useInMemoryIdempotency` module option for zero-config setup
|
|
726
|
+
- Lock renewal support via `extendLock()` method
|
|
727
|
+
- Warning when no idempotency store is configured
|
|
728
|
+
|
|
729
|
+
### Fixed
|
|
730
|
+
- RabbitMQ retry now preserves original job ID for idempotency tracking
|
|
731
|
+
- Lock renewal prevents duplicate processing for long-running jobs
|
|
732
|
+
```
|
|
733
|
+
|
|
734
|
+
---
|
|
735
|
+
|
|
736
|
+
## Acceptance Criteria
|
|
737
|
+
|
|
738
|
+
- [ ] `InMemoryIdempotencyStore` passes all unit tests
|
|
739
|
+
- [ ] Warning logged when no idempotency store configured
|
|
740
|
+
- [ ] `useInMemoryIdempotency: true` auto-configures store
|
|
741
|
+
- [ ] RabbitMQ retries use original job ID for dedup
|
|
742
|
+
- [ ] Lock renewal extends lock during long jobs
|
|
743
|
+
- [ ] All 123 existing unit tests still pass
|
|
744
|
+
- [ ] E2E tests pass with in-memory store
|
|
745
|
+
|
|
746
|
+
---
|
|
747
|
+
|
|
748
|
+
*Ready to implement. Start with Phase 1.*
|