@nathapp/nestjs-queue 2.1.0-rc.1 → 2.1.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 +16 -0
- package/TODO.md +15 -0
- package/dist/decorator/batch-process.decorator.d.ts +11 -0
- package/dist/decorator/batch-process.decorator.js +13 -0
- package/dist/decorator/batch-process.decorator.js.map +1 -0
- package/dist/decorator/index.d.ts +1 -0
- package/dist/decorator/index.js +1 -0
- package/dist/decorator/index.js.map +1 -1
- package/dist/enum/provider.enum.d.ts +0 -1
- package/dist/enum/provider.enum.js +0 -1
- package/dist/enum/provider.enum.js.map +1 -1
- package/dist/explorer/processor.explorer.d.ts +3 -0
- package/dist/explorer/processor.explorer.js +43 -1
- package/dist/explorer/processor.explorer.js.map +1 -1
- package/dist/interface/batch.interface.d.ts +23 -0
- package/dist/interface/batch.interface.js +3 -0
- package/dist/interface/batch.interface.js.map +1 -0
- package/dist/interface/index.d.ts +1 -0
- package/dist/interface/index.js +1 -0
- package/dist/interface/index.js.map +1 -1
- package/dist/interface/queue-options.interface.d.ts +9 -9
- package/dist/interface/queue-provider.interface.d.ts +13 -0
- package/dist/interface/queue-provider.interface.js.map +1 -1
- package/dist/module/queue.module.d.ts +5 -0
- package/dist/module/queue.module.js +64 -0
- package/dist/module/queue.module.js.map +1 -1
- package/dist/module/queue.provider.js +0 -3
- package/dist/module/queue.provider.js.map +1 -1
- package/dist/provider/bullmq/bullmq.provider.d.ts +9 -0
- package/dist/provider/bullmq/bullmq.provider.js +104 -0
- package/dist/provider/bullmq/bullmq.provider.js.map +1 -1
- package/dist/provider/index.d.ts +0 -1
- package/dist/provider/index.js +0 -1
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/kafka/kafka.provider.d.ts +7 -0
- package/dist/provider/kafka/kafka.provider.js +150 -0
- package/dist/provider/kafka/kafka.provider.js.map +1 -1
- package/dist/provider/rabbitmq/rabbitmq.provider.d.ts +9 -0
- package/dist/provider/rabbitmq/rabbitmq.provider.js +171 -0
- package/dist/provider/rabbitmq/rabbitmq.provider.js.map +1 -1
- package/dist/utils/delayed-scheduler.js +2 -1
- package/dist/utils/delayed-scheduler.js.map +1 -1
- package/docs/BATCH_PROCESSING_PLAN.md +581 -0
- package/docs/COVERAGE_IMPLEMENTATION_TASKS.md +664 -0
- package/docs/COVERAGE_RECOVERY_PLAN.md +254 -0
- package/docs/IMPLEMENTATION_PLAN_FOR_PRODUCER.md +82 -0
- package/package.json +3 -2
- package/dist/provider/redis-pubsub/index.d.ts +0 -1
- package/dist/provider/redis-pubsub/index.js +0 -18
- package/dist/provider/redis-pubsub/index.js.map +0 -1
- package/dist/provider/redis-pubsub/redis-pubsub.provider.d.ts +0 -40
- package/dist/provider/redis-pubsub/redis-pubsub.provider.js +0 -226
- package/dist/provider/redis-pubsub/redis-pubsub.provider.js.map +0 -1
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
# Batch Processing Mode Implementation Plan
|
|
2
|
+
|
|
3
|
+
**Created**: 2026-02-09
|
|
4
|
+
**Author**: Subrina
|
|
5
|
+
**Status**: Ready for Implementation
|
|
6
|
+
**Branch**: `queue-module`
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
Batch Processing Mode enables high-throughput job processing by allowing consumers to receive and process multiple jobs in a single operation. This reduces per-job overhead and is ideal for scenarios like:
|
|
13
|
+
- Bulk data imports
|
|
14
|
+
- Mass notifications
|
|
15
|
+
- ETL pipelines
|
|
16
|
+
- Log aggregation
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Architecture
|
|
21
|
+
|
|
22
|
+
### Core Concepts
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
26
|
+
│ Queue Provider │
|
|
27
|
+
├─────────────────────────────────────────────────────────────┤
|
|
28
|
+
│ Standard Mode (Current) │ Batch Mode (New) │
|
|
29
|
+
│ ───────────────────────── │ ───────────────────────── │
|
|
30
|
+
│ Job → Process → Ack │ Jobs[] → Process → BatchAck│
|
|
31
|
+
│ One at a time │ N jobs per batch │
|
|
32
|
+
│ Individual callbacks │ Batch callback │
|
|
33
|
+
└─────────────────────────────────────────────────────────────┘
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Batch Processing Flow
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
1. Consumer fetches N jobs (batchSize)
|
|
40
|
+
2. Jobs are passed to batch processor as array
|
|
41
|
+
3. Processor handles all jobs (parallel or sequential)
|
|
42
|
+
4. Single batch acknowledgment
|
|
43
|
+
5. Metrics updated for entire batch
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Phase 1: Core Batch Interface
|
|
49
|
+
|
|
50
|
+
### 1.1 New Interfaces
|
|
51
|
+
|
|
52
|
+
**File**: `lib/interface/batch.interface.ts`
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
/**
|
|
56
|
+
* Result of processing a single job in a batch
|
|
57
|
+
*/
|
|
58
|
+
export interface BatchJobResult<T = unknown> {
|
|
59
|
+
jobId: string;
|
|
60
|
+
success: boolean;
|
|
61
|
+
result?: T;
|
|
62
|
+
error?: Error;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Result of processing an entire batch
|
|
67
|
+
*/
|
|
68
|
+
export interface BatchResult<T = unknown> {
|
|
69
|
+
totalJobs: number;
|
|
70
|
+
successful: number;
|
|
71
|
+
failed: number;
|
|
72
|
+
results: BatchJobResult<T>[];
|
|
73
|
+
processingTimeMs: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Options for batch processing
|
|
78
|
+
*/
|
|
79
|
+
export interface BatchProcessorOptions {
|
|
80
|
+
/**
|
|
81
|
+
* Number of jobs to fetch per batch
|
|
82
|
+
* @default 10
|
|
83
|
+
*/
|
|
84
|
+
batchSize?: number;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Maximum time to wait for a full batch (ms)
|
|
88
|
+
* If timeout is reached, process partial batch
|
|
89
|
+
* @default 5000
|
|
90
|
+
*/
|
|
91
|
+
batchTimeout?: number;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Process jobs within batch in parallel
|
|
95
|
+
* @default true
|
|
96
|
+
*/
|
|
97
|
+
parallel?: boolean;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Maximum parallel jobs within a batch (only if parallel=true)
|
|
101
|
+
* @default batchSize
|
|
102
|
+
*/
|
|
103
|
+
maxParallel?: number;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Continue processing batch even if some jobs fail
|
|
107
|
+
* @default true
|
|
108
|
+
*/
|
|
109
|
+
continueOnError?: boolean;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Retry failed jobs from batch individually
|
|
113
|
+
* @default false
|
|
114
|
+
*/
|
|
115
|
+
retryFailedIndividually?: boolean;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Batch processor function signature
|
|
120
|
+
*/
|
|
121
|
+
export type BatchProcessor<T = unknown, R = unknown> = (
|
|
122
|
+
jobs: IJob<T>[],
|
|
123
|
+
options: BatchProcessorOptions,
|
|
124
|
+
) => Promise<BatchResult<R>>;
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### 1.2 Extend IQueueProvider Interface
|
|
128
|
+
|
|
129
|
+
**File**: `lib/interface/queue-provider.interface.ts` (additions)
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
export interface IQueueProvider {
|
|
133
|
+
// ... existing methods ...
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Register a batch processor for a queue
|
|
137
|
+
* @param queueName Queue to process
|
|
138
|
+
* @param processor Batch processor function
|
|
139
|
+
* @param options Batch processing options
|
|
140
|
+
*/
|
|
141
|
+
processBatch<T, R>(
|
|
142
|
+
queueName: string,
|
|
143
|
+
processor: BatchProcessor<T, R>,
|
|
144
|
+
options?: BatchProcessorOptions,
|
|
145
|
+
): Promise<void>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Add multiple jobs optimized for batch consumption
|
|
149
|
+
* Groups jobs for efficient batch retrieval
|
|
150
|
+
*/
|
|
151
|
+
addBatch<T>(
|
|
152
|
+
queueName: string,
|
|
153
|
+
jobs: Array<{ name: string; data: T; opts?: JobOptions }>,
|
|
154
|
+
batchId?: string,
|
|
155
|
+
): Promise<IJob<T>[]>;
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Phase 2: Provider Implementations
|
|
162
|
+
|
|
163
|
+
### 2.1 BullMQ Batch Processing
|
|
164
|
+
|
|
165
|
+
**Strategy**: Use BullMQ's native `getJobs()` with custom batch worker.
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
// lib/provider/bullmq/bullmq-batch.ts
|
|
169
|
+
|
|
170
|
+
export class BullMQBatchProcessor {
|
|
171
|
+
private batchWorker?: Worker;
|
|
172
|
+
private accumulator: Job[] = [];
|
|
173
|
+
private batchTimer?: NodeJS.Timeout;
|
|
174
|
+
|
|
175
|
+
async startBatchProcessing<T, R>(
|
|
176
|
+
queueName: string,
|
|
177
|
+
processor: BatchProcessor<T, R>,
|
|
178
|
+
options: BatchProcessorOptions,
|
|
179
|
+
): Promise<void> {
|
|
180
|
+
const { batchSize = 10, batchTimeout = 5000 } = options;
|
|
181
|
+
|
|
182
|
+
this.batchWorker = new Worker(
|
|
183
|
+
queueName,
|
|
184
|
+
async (job: Job) => {
|
|
185
|
+
this.accumulator.push(job);
|
|
186
|
+
|
|
187
|
+
// Check if batch is full
|
|
188
|
+
if (this.accumulator.length >= batchSize) {
|
|
189
|
+
await this.processBatch(processor, options);
|
|
190
|
+
} else if (!this.batchTimer) {
|
|
191
|
+
// Start timeout for partial batch
|
|
192
|
+
this.batchTimer = setTimeout(async () => {
|
|
193
|
+
if (this.accumulator.length > 0) {
|
|
194
|
+
await this.processBatch(processor, options);
|
|
195
|
+
}
|
|
196
|
+
}, batchTimeout);
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
connection: this.connection,
|
|
201
|
+
concurrency: batchSize, // Fetch up to batchSize concurrently
|
|
202
|
+
},
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private async processBatch<T, R>(
|
|
207
|
+
processor: BatchProcessor<T, R>,
|
|
208
|
+
options: BatchProcessorOptions,
|
|
209
|
+
): Promise<void> {
|
|
210
|
+
// Clear timer
|
|
211
|
+
if (this.batchTimer) {
|
|
212
|
+
clearTimeout(this.batchTimer);
|
|
213
|
+
this.batchTimer = undefined;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Grab current batch
|
|
217
|
+
const batch = [...this.accumulator];
|
|
218
|
+
this.accumulator = [];
|
|
219
|
+
|
|
220
|
+
// Map to IJob format
|
|
221
|
+
const jobs = batch.map(this.mapJob);
|
|
222
|
+
|
|
223
|
+
// Process
|
|
224
|
+
const startTime = Date.now();
|
|
225
|
+
const result = await processor(jobs, options);
|
|
226
|
+
|
|
227
|
+
// Acknowledge based on results
|
|
228
|
+
for (let i = 0; i < batch.length; i++) {
|
|
229
|
+
const jobResult = result.results[i];
|
|
230
|
+
if (jobResult.success) {
|
|
231
|
+
await batch[i].moveToCompleted(jobResult.result, batch[i].token);
|
|
232
|
+
} else if (options.retryFailedIndividually) {
|
|
233
|
+
await batch[i].moveToFailed(jobResult.error, batch[i].token);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### 2.2 Kafka Batch Processing
|
|
241
|
+
|
|
242
|
+
**Strategy**: Leverage Kafka's native batch consumption with `eachBatch`.
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
// lib/provider/kafka/kafka-batch.ts
|
|
246
|
+
|
|
247
|
+
async startBatchProcessing<T, R>(
|
|
248
|
+
queueName: string,
|
|
249
|
+
processor: BatchProcessor<T, R>,
|
|
250
|
+
options: BatchProcessorOptions,
|
|
251
|
+
): Promise<void> {
|
|
252
|
+
const { batchSize = 10 } = options;
|
|
253
|
+
|
|
254
|
+
await this.consumer.run({
|
|
255
|
+
eachBatch: async ({ batch, resolveOffset, heartbeat, commitOffsetsIfNecessary }) => {
|
|
256
|
+
const jobs: IJob<T>[] = [];
|
|
257
|
+
|
|
258
|
+
for (const message of batch.messages) {
|
|
259
|
+
const parsed = JSON.parse(message.value!.toString());
|
|
260
|
+
jobs.push(this.mapKafkaMessage(parsed, batch.topic, message.offset));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Process in chunks of batchSize
|
|
264
|
+
for (let i = 0; i < jobs.length; i += batchSize) {
|
|
265
|
+
const chunk = jobs.slice(i, i + batchSize);
|
|
266
|
+
const result = await processor(chunk, options);
|
|
267
|
+
|
|
268
|
+
// Commit offsets for successful jobs
|
|
269
|
+
const lastSuccessIndex = result.results.findLastIndex(r => r.success);
|
|
270
|
+
if (lastSuccessIndex >= 0) {
|
|
271
|
+
resolveOffset(batch.messages[i + lastSuccessIndex].offset);
|
|
272
|
+
await commitOffsetsIfNecessary();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
await heartbeat();
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### 2.3 RabbitMQ Batch Processing
|
|
283
|
+
|
|
284
|
+
**Strategy**: Use prefetch count + manual batch acknowledgment.
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
// lib/provider/rabbitmq/rabbitmq-batch.ts
|
|
288
|
+
|
|
289
|
+
async startBatchProcessing<T, R>(
|
|
290
|
+
queueName: string,
|
|
291
|
+
processor: BatchProcessor<T, R>,
|
|
292
|
+
options: BatchProcessorOptions,
|
|
293
|
+
): Promise<void> {
|
|
294
|
+
const { batchSize = 10, batchTimeout = 5000 } = options;
|
|
295
|
+
|
|
296
|
+
const channel = await this.getChannel(queueName);
|
|
297
|
+
await channel.prefetch(batchSize);
|
|
298
|
+
|
|
299
|
+
let accumulator: { job: IJob<T>; msg: Message }[] = [];
|
|
300
|
+
let batchTimer: NodeJS.Timeout | undefined;
|
|
301
|
+
|
|
302
|
+
const processBatch = async () => {
|
|
303
|
+
if (batchTimer) {
|
|
304
|
+
clearTimeout(batchTimer);
|
|
305
|
+
batchTimer = undefined;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const batch = [...accumulator];
|
|
309
|
+
accumulator = [];
|
|
310
|
+
|
|
311
|
+
const jobs = batch.map(b => b.job);
|
|
312
|
+
const result = await processor(jobs, options);
|
|
313
|
+
|
|
314
|
+
// Ack/Nack based on results
|
|
315
|
+
for (let i = 0; i < batch.length; i++) {
|
|
316
|
+
const { msg } = batch[i];
|
|
317
|
+
const jobResult = result.results[i];
|
|
318
|
+
|
|
319
|
+
if (jobResult.success) {
|
|
320
|
+
channel.ack(msg);
|
|
321
|
+
} else if (options.retryFailedIndividually) {
|
|
322
|
+
channel.nack(msg, false, true); // Requeue
|
|
323
|
+
} else {
|
|
324
|
+
channel.ack(msg); // Ack anyway if continueOnError
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
await channel.consume(queueName, async (msg) => {
|
|
330
|
+
if (!msg) return;
|
|
331
|
+
|
|
332
|
+
const job = this.parseMessage<T>(msg);
|
|
333
|
+
accumulator.push({ job, msg });
|
|
334
|
+
|
|
335
|
+
if (accumulator.length >= batchSize) {
|
|
336
|
+
await processBatch();
|
|
337
|
+
} else if (!batchTimer) {
|
|
338
|
+
batchTimer = setTimeout(processBatch, batchTimeout);
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
## Phase 3: Decorator Support
|
|
347
|
+
|
|
348
|
+
### 3.1 @BatchProcess Decorator
|
|
349
|
+
|
|
350
|
+
**File**: `lib/decorator/batch-process.decorator.ts`
|
|
351
|
+
|
|
352
|
+
```typescript
|
|
353
|
+
import { SetMetadata } from '@nestjs/common';
|
|
354
|
+
|
|
355
|
+
export const BATCH_PROCESS_METADATA = 'BATCH_PROCESS_METADATA';
|
|
356
|
+
|
|
357
|
+
export interface BatchProcessDecoratorOptions {
|
|
358
|
+
name?: string;
|
|
359
|
+
batchSize?: number;
|
|
360
|
+
batchTimeout?: number;
|
|
361
|
+
parallel?: boolean;
|
|
362
|
+
maxParallel?: number;
|
|
363
|
+
continueOnError?: boolean;
|
|
364
|
+
retryFailedIndividually?: boolean;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function BatchProcess(options: BatchProcessDecoratorOptions = {}): MethodDecorator {
|
|
368
|
+
return SetMetadata(BATCH_PROCESS_METADATA, {
|
|
369
|
+
...options,
|
|
370
|
+
methodName: undefined, // Will be set by explorer
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### 3.2 Usage Example
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
@Processor('bulk-import-queue')
|
|
379
|
+
export class BulkImportProcessor {
|
|
380
|
+
@BatchProcess({ batchSize: 100, parallel: true, maxParallel: 10 })
|
|
381
|
+
async handleBatch(jobs: IJob<ImportData>[]): Promise<BatchResult> {
|
|
382
|
+
const results: BatchJobResult[] = [];
|
|
383
|
+
|
|
384
|
+
// Process all jobs in parallel with concurrency limit
|
|
385
|
+
await pMap(jobs, async (job) => {
|
|
386
|
+
try {
|
|
387
|
+
await this.importService.import(job.data);
|
|
388
|
+
results.push({ jobId: job.id, success: true });
|
|
389
|
+
} catch (error) {
|
|
390
|
+
results.push({ jobId: job.id, success: false, error });
|
|
391
|
+
}
|
|
392
|
+
}, { concurrency: 10 });
|
|
393
|
+
|
|
394
|
+
return {
|
|
395
|
+
totalJobs: jobs.length,
|
|
396
|
+
successful: results.filter(r => r.success).length,
|
|
397
|
+
failed: results.filter(r => !r.success).length,
|
|
398
|
+
results,
|
|
399
|
+
processingTimeMs: Date.now() - startTime,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
---
|
|
406
|
+
|
|
407
|
+
## Phase 4: ProcessorExplorer Integration
|
|
408
|
+
|
|
409
|
+
### 4.1 Discover Batch Processors
|
|
410
|
+
|
|
411
|
+
**File**: `lib/explorer/processor.explorer.ts` (additions)
|
|
412
|
+
|
|
413
|
+
```typescript
|
|
414
|
+
private async registerBatchProcessors(
|
|
415
|
+
instance: any,
|
|
416
|
+
prototype: any,
|
|
417
|
+
processorMeta: ProcessorDecoratorOptions,
|
|
418
|
+
): Promise<void> {
|
|
419
|
+
const methodNames = this.metadataScanner.getAllMethodNames(prototype);
|
|
420
|
+
|
|
421
|
+
for (const methodName of methodNames) {
|
|
422
|
+
const batchMeta = this.reflector.get<BatchProcessDecoratorOptions>(
|
|
423
|
+
BATCH_PROCESS_METADATA,
|
|
424
|
+
instance[methodName],
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
if (!batchMeta) continue;
|
|
428
|
+
|
|
429
|
+
const boundMethod = instance[methodName].bind(instance);
|
|
430
|
+
|
|
431
|
+
await this.queueProvider.processBatch(
|
|
432
|
+
processorMeta.queueName,
|
|
433
|
+
boundMethod,
|
|
434
|
+
{
|
|
435
|
+
batchSize: batchMeta.batchSize ?? 10,
|
|
436
|
+
batchTimeout: batchMeta.batchTimeout ?? 5000,
|
|
437
|
+
parallel: batchMeta.parallel ?? true,
|
|
438
|
+
maxParallel: batchMeta.maxParallel,
|
|
439
|
+
continueOnError: batchMeta.continueOnError ?? true,
|
|
440
|
+
retryFailedIndividually: batchMeta.retryFailedIndividually ?? false,
|
|
441
|
+
},
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
this.logger.log(
|
|
445
|
+
`Registered batch processor: ${instance.constructor.name}.${methodName} ` +
|
|
446
|
+
`for queue "${processorMeta.queueName}" (batchSize: ${batchMeta.batchSize ?? 10})`,
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
---
|
|
453
|
+
|
|
454
|
+
## Phase 5: Metrics & Observability
|
|
455
|
+
|
|
456
|
+
### 5.1 Batch Metrics Interface
|
|
457
|
+
|
|
458
|
+
**File**: `lib/interface/metrics.interface.ts` (additions)
|
|
459
|
+
|
|
460
|
+
```typescript
|
|
461
|
+
export interface MetricsHook {
|
|
462
|
+
// ... existing hooks ...
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Called when a batch is processed
|
|
466
|
+
*/
|
|
467
|
+
batchProcessed?: (
|
|
468
|
+
queueName: string,
|
|
469
|
+
batchSize: number,
|
|
470
|
+
successful: number,
|
|
471
|
+
failed: number,
|
|
472
|
+
processingTimeMs: number,
|
|
473
|
+
) => void;
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Called when batch processing fails entirely
|
|
477
|
+
*/
|
|
478
|
+
batchFailed?: (
|
|
479
|
+
queueName: string,
|
|
480
|
+
batchSize: number,
|
|
481
|
+
error: Error,
|
|
482
|
+
) => void;
|
|
483
|
+
}
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
---
|
|
487
|
+
|
|
488
|
+
## Phase 6: Unit Tests
|
|
489
|
+
|
|
490
|
+
### 6.1 Test Cases
|
|
491
|
+
|
|
492
|
+
**File**: `test/batch-processing.spec.ts`
|
|
493
|
+
|
|
494
|
+
```typescript
|
|
495
|
+
describe('Batch Processing', () => {
|
|
496
|
+
describe('BullMQ Batch', () => {
|
|
497
|
+
it('should accumulate jobs until batchSize is reached');
|
|
498
|
+
it('should process partial batch on timeout');
|
|
499
|
+
it('should process jobs in parallel when parallel=true');
|
|
500
|
+
it('should respect maxParallel limit');
|
|
501
|
+
it('should continue on error when continueOnError=true');
|
|
502
|
+
it('should retry failed jobs individually when retryFailedIndividually=true');
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
describe('Kafka Batch', () => {
|
|
506
|
+
it('should use eachBatch for native batch consumption');
|
|
507
|
+
it('should commit offsets correctly for partial success');
|
|
508
|
+
it('should call heartbeat between chunks');
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
describe('RabbitMQ Batch', () => {
|
|
512
|
+
it('should set prefetch to batchSize');
|
|
513
|
+
it('should ack/nack based on individual job results');
|
|
514
|
+
it('should handle batch timeout');
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
describe('@BatchProcess Decorator', () => {
|
|
518
|
+
it('should register batch processor with correct options');
|
|
519
|
+
it('should call batch processor with accumulated jobs');
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
---
|
|
525
|
+
|
|
526
|
+
## Implementation Order
|
|
527
|
+
|
|
528
|
+
```
|
|
529
|
+
Phase 1: Core Batch Interface ← START HERE (1h)
|
|
530
|
+
↓
|
|
531
|
+
Phase 2: Provider Implementations (3h)
|
|
532
|
+
├── BullMQ Batch
|
|
533
|
+
├── Kafka Batch
|
|
534
|
+
└── RabbitMQ Batch
|
|
535
|
+
↓
|
|
536
|
+
Phase 3: @BatchProcess Decorator (1h)
|
|
537
|
+
↓
|
|
538
|
+
Phase 4: ProcessorExplorer Integration (1h)
|
|
539
|
+
↓
|
|
540
|
+
Phase 5: Metrics & Observability (30m)
|
|
541
|
+
↓
|
|
542
|
+
Phase 6: Unit Tests (2h)
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
**Estimated Total**: ~8.5 hours
|
|
546
|
+
|
|
547
|
+
---
|
|
548
|
+
|
|
549
|
+
## Changeset
|
|
550
|
+
|
|
551
|
+
```markdown
|
|
552
|
+
---
|
|
553
|
+
"@nathapp/nestjs-queue": minor
|
|
554
|
+
---
|
|
555
|
+
|
|
556
|
+
### Added
|
|
557
|
+
- Batch Processing Mode for high-throughput job processing
|
|
558
|
+
- `@BatchProcess` decorator with configurable batch options
|
|
559
|
+
- `processBatch()` method on all providers (BullMQ, Kafka, RabbitMQ)
|
|
560
|
+
- Batch metrics hooks (`batchProcessed`, `batchFailed`)
|
|
561
|
+
- Parallel processing within batches with concurrency control
|
|
562
|
+
- Partial batch processing on timeout
|
|
563
|
+
- Per-job retry support within failed batches
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
---
|
|
567
|
+
|
|
568
|
+
## Acceptance Criteria
|
|
569
|
+
|
|
570
|
+
- [ ] All providers support `processBatch()` method
|
|
571
|
+
- [ ] `@BatchProcess` decorator works with ProcessorExplorer
|
|
572
|
+
- [ ] Batch timeout triggers partial batch processing
|
|
573
|
+
- [ ] Parallel processing respects `maxParallel` limit
|
|
574
|
+
- [ ] Failed jobs can be retried individually
|
|
575
|
+
- [ ] Metrics hooks are called correctly
|
|
576
|
+
- [ ] Unit tests cover all scenarios
|
|
577
|
+
- [ ] E2E tests demonstrate batch processing works
|
|
578
|
+
|
|
579
|
+
---
|
|
580
|
+
|
|
581
|
+
*Ready to implement. Start with Phase 1.*
|