notifkit 0.1.10 → 0.1.12
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/README.md +37 -1
- package/dashboard/dist/assets/{index-7mvXb4bS.js → index-Bsrmo9LE.js} +1 -1
- package/dashboard/dist/assets/index-s-SXGIwv.css +1 -0
- package/dashboard/dist/index.html +2 -2
- package/dist/bootstrap-omGPIBny.mjs +89 -0
- package/dist/bulk-C4hTcnLu.mjs +77 -0
- package/dist/index.d.mts +1160 -8886
- package/dist/index.mjs +2 -38
- package/dist/{main-C9s-2ArH.mjs → main-B-AZ-xoN.mjs} +49 -75
- package/dist/main-B5BwjIMu.mjs +811 -0
- package/dist/{main-B3iQrl3t.mjs → main-BU27VjDC.mjs} +149 -134
- package/dist/main-Bes11Ad3.mjs +312 -0
- package/dist/{main-BiFiOX9D.mjs → main-BtnGmyaT.mjs} +119 -112
- package/dist/{main-39DtKrX4.mjs → main-BxAeC_k6.mjs} +395 -184
- package/dist/{main-Dz2sUh3c.mjs → main-DAXO-MiH.mjs} +57 -54
- package/dist/{main-BgmT1MPe.mjs → main-DYGCl7Ja.mjs} +135 -79
- package/dist/{src-QiTu5e_a.mjs → src-CIPTzq9i.mjs} +1351 -661
- package/drizzle/0006_message_logs_redundant_indexes.sql +7 -0
- package/drizzle/0007_workflow_status_canceled.sql +3 -0
- package/drizzle/meta/_journal.json +14 -0
- package/package.json +21 -18
- package/scripts/create-admin.mjs +8 -1
- package/scripts/migrate.mjs +43 -0
- package/src/client.ts +15 -5
- package/src/config/index.ts +37 -3
- package/src/contracts/events/notification-ai-pending.ts +12 -0
- package/src/contracts/events/notification-delivered.ts +7 -0
- package/src/contracts/events/notification-dispatched.ts +21 -2
- package/src/contracts/events/notification-enriched.ts +16 -0
- package/src/contracts/events/notification-failed.ts +2 -0
- package/src/contracts/events/notification-requested.ts +2 -0
- package/src/contracts/registry.ts +2 -2
- package/src/contracts/sdk.ts +77 -29
- package/src/db/bulk.ts +132 -0
- package/src/db/index.ts +174 -7
- package/src/db/schema.ts +3 -50
- package/src/idempotency/index.ts +60 -6
- package/src/logger/index.ts +0 -12
- package/src/queue/index.ts +255 -76
- package/src/rate-limiter/index.ts +217 -16
- package/src/redis/index.ts +265 -0
- package/src/repositories/index.ts +171 -82
- package/src/server.ts +64 -41
- package/src/services/ai/main.ts +83 -70
- package/src/services/api/admin-static.ts +41 -12
- package/src/services/api/handlers.ts +357 -159
- package/src/services/api/http.ts +34 -7
- package/src/services/api/main.ts +104 -67
- package/src/services/api/router.ts +18 -5
- package/src/services/delivery/main.ts +495 -230
- package/src/services/delivery/throttle.ts +42 -59
- package/src/services/engine/main.ts +206 -152
- package/src/services/enricher/main.ts +155 -107
- package/src/services/events/main.ts +100 -55
- package/src/services/scheduler/main.ts +151 -79
- package/src/services/scheduler/send-gate.ts +117 -0
- package/src/services/workflow/main.ts +220 -100
- package/src/shared/batch-processor.ts +65 -34
- package/src/shared/dataloader.ts +58 -22
- package/src/shared/events.ts +3 -0
- package/src/shared/index.ts +0 -1
- package/src/shared/utils.ts +2 -47
- package/src/templates/index.ts +0 -67
- package/src/transport/index.ts +6 -0
- package/src/workers/bootstrap.ts +124 -0
- package/src/workers/index.ts +99 -24
- package/src/workflows/sdk.ts +2 -0
- package/dashboard/dist/assets/index-o7L-f3-c.css +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/main-39DtKrX4.mjs.map +0 -1
- package/dist/main-B3iQrl3t.mjs.map +0 -1
- package/dist/main-BM8DH3v3.mjs +0 -632
- package/dist/main-BM8DH3v3.mjs.map +0 -1
- package/dist/main-BgmT1MPe.mjs.map +0 -1
- package/dist/main-BiFiOX9D.mjs.map +0 -1
- package/dist/main-C9s-2ArH.mjs.map +0 -1
- package/dist/main-Cqo3rSb5.mjs +0 -224
- package/dist/main-Cqo3rSb5.mjs.map +0 -1
- package/dist/main-Dz2sUh3c.mjs.map +0 -1
- package/dist/src-QiTu5e_a.mjs.map +0 -1
package/src/queue/index.ts
CHANGED
|
@@ -17,6 +17,14 @@ export interface StreamMessage {
|
|
|
17
17
|
event: StreamEvent;
|
|
18
18
|
/** Stream this message was read from. Required to ack/claim against the right one. */
|
|
19
19
|
stream?: string;
|
|
20
|
+
/** Redis Stream PEL delivery count (1 on initial read, >1 on autoclaim/reclaim). */
|
|
21
|
+
deliveryCount?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Handed over in-process by an upstream stage (see BaseWorker.processInline)
|
|
24
|
+
* rather than read from a stream. The upstream stage still holds its own
|
|
25
|
+
* stream entry and lock for the duration.
|
|
26
|
+
*/
|
|
27
|
+
inline?: boolean;
|
|
20
28
|
}
|
|
21
29
|
|
|
22
30
|
export interface PendingEntry {
|
|
@@ -65,11 +73,20 @@ export interface StreamProducerOptions {
|
|
|
65
73
|
maxLen?: number;
|
|
66
74
|
}
|
|
67
75
|
|
|
76
|
+
interface PendingPublishItem {
|
|
77
|
+
event: StreamEvent;
|
|
78
|
+
resolve: (messageId: string) => void;
|
|
79
|
+
reject: (err: unknown) => void;
|
|
80
|
+
}
|
|
81
|
+
|
|
68
82
|
export class StreamProducer {
|
|
69
83
|
private readonly redis: Redis;
|
|
70
84
|
private readonly stream: StreamName;
|
|
71
85
|
private readonly logger?: Logger;
|
|
72
86
|
private readonly maxLen: number;
|
|
87
|
+
private pendingQueue: PendingPublishItem[] = [];
|
|
88
|
+
private flushScheduled = false;
|
|
89
|
+
private lastMaxLenCheck = 0;
|
|
73
90
|
|
|
74
91
|
constructor({ redis, stream, logger, maxLen }: StreamProducerOptions) {
|
|
75
92
|
this.redis = redis;
|
|
@@ -79,51 +96,113 @@ export class StreamProducer {
|
|
|
79
96
|
}
|
|
80
97
|
|
|
81
98
|
async publish(partial: Omit<StreamEvent, "id" | "timestamp">): Promise<string> {
|
|
82
|
-
|
|
99
|
+
return this.enqueue({
|
|
83
100
|
...partial,
|
|
84
101
|
id: crypto.randomUUID(),
|
|
85
102
|
timestamp: new Date().toISOString(),
|
|
86
|
-
};
|
|
103
|
+
});
|
|
104
|
+
}
|
|
87
105
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
106
|
+
private enqueue(event: StreamEvent): Promise<string> {
|
|
107
|
+
return new Promise<string>((resolve, reject) => {
|
|
108
|
+
this.pendingQueue.push({ event, resolve, reject });
|
|
109
|
+
if (this.pendingQueue.length >= 100) {
|
|
110
|
+
void this.flush();
|
|
111
|
+
} else if (!this.flushScheduled) {
|
|
112
|
+
this.flushScheduled = true;
|
|
113
|
+
setImmediate(() => {
|
|
114
|
+
void this.flush();
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
97
119
|
|
|
98
|
-
|
|
120
|
+
private async flush(): Promise<void> {
|
|
121
|
+
this.flushScheduled = false;
|
|
122
|
+
const batch = this.pendingQueue;
|
|
123
|
+
this.pendingQueue = [];
|
|
124
|
+
if (batch.length === 0) return;
|
|
99
125
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
126
|
+
try {
|
|
127
|
+
if (typeof this.redis.pipeline === "function") {
|
|
128
|
+
const pipeline = this.redis.pipeline();
|
|
129
|
+
for (let i = 0; i < batch.length; i++) {
|
|
130
|
+
const item = batch[i]!;
|
|
131
|
+
if (i === batch.length - 1) {
|
|
132
|
+
pipeline.xadd(
|
|
133
|
+
this.stream,
|
|
134
|
+
"MAXLEN",
|
|
135
|
+
"~",
|
|
136
|
+
String(this.maxLen),
|
|
137
|
+
"*",
|
|
138
|
+
"data",
|
|
139
|
+
JSON.stringify(item.event),
|
|
140
|
+
);
|
|
141
|
+
} else {
|
|
142
|
+
pipeline.xadd(this.stream, "*", "data", JSON.stringify(item.event));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const results = await pipeline.exec();
|
|
147
|
+
if (!results) throw new Error(`Pipeline execution failed for ${this.stream}`);
|
|
148
|
+
|
|
149
|
+
for (let i = 0; i < results.length; i++) {
|
|
150
|
+
const [err, msgId] = results[i] ?? [];
|
|
151
|
+
if (err) {
|
|
152
|
+
batch[i]!.reject(err);
|
|
153
|
+
} else {
|
|
154
|
+
batch[i]!.resolve(msgId as string);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
for (let i = 0; i < batch.length; i++) {
|
|
159
|
+
const item = batch[i]!;
|
|
160
|
+
try {
|
|
161
|
+
const msgId = await this.redis.xadd(
|
|
162
|
+
this.stream,
|
|
163
|
+
"MAXLEN",
|
|
164
|
+
"~",
|
|
165
|
+
String(this.maxLen),
|
|
166
|
+
"*",
|
|
167
|
+
"data",
|
|
168
|
+
JSON.stringify(item.event),
|
|
169
|
+
);
|
|
170
|
+
if (!msgId) throw new Error(`XADD to ${this.stream} returned null`);
|
|
171
|
+
item.resolve(msgId);
|
|
172
|
+
} catch (err) {
|
|
173
|
+
item.reject(err);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
104
177
|
|
|
105
|
-
|
|
178
|
+
this.monitorMaxLen(this.stream).catch(() => {});
|
|
179
|
+
} catch (err) {
|
|
180
|
+
for (const item of batch) {
|
|
181
|
+
item.reject(err);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
106
184
|
}
|
|
107
185
|
|
|
108
186
|
private async monitorMaxLen(stream: string) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const len = await this.redis.xlen(stream);
|
|
113
|
-
metrics.queueSize.set({ stream }, len);
|
|
114
|
-
|
|
115
|
-
if (len > this.maxLen * 0.8) {
|
|
116
|
-
this.logger?.warn(
|
|
117
|
-
{ stream, len, maxLen: this.maxLen },
|
|
118
|
-
"stream is nearing MAXLEN limit (80%+)",
|
|
119
|
-
);
|
|
120
|
-
}
|
|
187
|
+
const now = Date.now();
|
|
188
|
+
if (now - this.lastMaxLenCheck < 5000) return;
|
|
189
|
+
this.lastMaxLenCheck = now;
|
|
121
190
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
191
|
+
try {
|
|
192
|
+
const len = await this.redis.xlen(stream);
|
|
193
|
+
metrics.queueSize.set({ stream }, len);
|
|
194
|
+
|
|
195
|
+
if (len > this.maxLen * 0.8) {
|
|
196
|
+
this.logger?.warn(
|
|
197
|
+
{ stream, len, maxLen: this.maxLen },
|
|
198
|
+
"stream is nearing MAXLEN limit (80%+)",
|
|
199
|
+
);
|
|
126
200
|
}
|
|
201
|
+
|
|
202
|
+
const dlqLen = await this.redis.xlen(STREAMS.DEAD_LETTER);
|
|
203
|
+
metrics.queueSize.set({ stream: STREAMS.DEAD_LETTER }, dlqLen);
|
|
204
|
+
} catch (err) {
|
|
205
|
+
this.logger?.debug({ err }, "failed to monitor stream length");
|
|
127
206
|
}
|
|
128
207
|
}
|
|
129
208
|
|
|
@@ -132,47 +211,18 @@ export class StreamProducer {
|
|
|
132
211
|
): Promise<{ messageIds: string[]; eventIds: string[] }> {
|
|
133
212
|
if (partials.length === 0) return { messageIds: [], eventIds: [] };
|
|
134
213
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
214
|
+
// The ids returned must be the ids the events are written with: callers
|
|
215
|
+
// record them as notification ids.
|
|
138
216
|
const eventIds: string[] = [];
|
|
217
|
+
const promises: Promise<string>[] = [];
|
|
139
218
|
|
|
140
219
|
for (const partial of partials) {
|
|
141
220
|
const id = crypto.randomUUID();
|
|
142
221
|
eventIds.push(id);
|
|
143
|
-
|
|
144
|
-
...partial,
|
|
145
|
-
id,
|
|
146
|
-
timestamp,
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
pipeline.xadd(
|
|
150
|
-
this.stream,
|
|
151
|
-
"MAXLEN",
|
|
152
|
-
"~",
|
|
153
|
-
String(this.maxLen),
|
|
154
|
-
"*",
|
|
155
|
-
"data",
|
|
156
|
-
JSON.stringify(event),
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const results = await pipeline.exec();
|
|
161
|
-
if (!results) throw new Error(`Pipeline execution failed for ${this.stream}`);
|
|
162
|
-
|
|
163
|
-
const messageIds: string[] = [];
|
|
164
|
-
for (let i = 0; i < results.length; i++) {
|
|
165
|
-
const result = results[i];
|
|
166
|
-
if (!result) throw new Error("Pipeline result is undefined");
|
|
167
|
-
const [err, msgId] = result;
|
|
168
|
-
if (err) throw err;
|
|
169
|
-
messageIds.push(msgId as string);
|
|
222
|
+
promises.push(this.enqueue({ ...partial, id, timestamp: new Date().toISOString() }));
|
|
170
223
|
}
|
|
171
224
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
this.monitorMaxLen(this.stream).catch(() => {});
|
|
175
|
-
|
|
225
|
+
const messageIds = await Promise.all(promises);
|
|
176
226
|
return { messageIds, eventIds };
|
|
177
227
|
}
|
|
178
228
|
}
|
|
@@ -188,6 +238,15 @@ export interface StreamConsumerOptions {
|
|
|
188
238
|
logger?: Logger;
|
|
189
239
|
batchSize?: number;
|
|
190
240
|
blockMs?: number;
|
|
241
|
+
bufferAcks?: boolean;
|
|
242
|
+
ackFlushMs?: number;
|
|
243
|
+
/**
|
|
244
|
+
* After a read that came back short of `batchSize`, wait this long before the
|
|
245
|
+
* next one. A consumer blocked on an idle stream wakes on every XADD, so under
|
|
246
|
+
* moderate load it reads a message or two per round trip; a short pause lets a
|
|
247
|
+
* batch accumulate. Full reads (a backlog) never wait. Default 0.
|
|
248
|
+
*/
|
|
249
|
+
coalesceMs?: number;
|
|
191
250
|
}
|
|
192
251
|
|
|
193
252
|
type XReadGroupResult = Array<[string, Array<[string, string[] | null]>]> | null;
|
|
@@ -202,6 +261,11 @@ export class StreamConsumer {
|
|
|
202
261
|
private readonly logger?: Logger;
|
|
203
262
|
private readonly batchSize: number;
|
|
204
263
|
private readonly blockMs: number;
|
|
264
|
+
private readonly bufferAcks: boolean;
|
|
265
|
+
private readonly ackFlushMs: number;
|
|
266
|
+
private readonly coalesceMs: number;
|
|
267
|
+
private ackBuffers = new Map<string, string[]>();
|
|
268
|
+
private ackTimer: NodeJS.Timeout | null = null;
|
|
205
269
|
private running = false;
|
|
206
270
|
|
|
207
271
|
constructor({
|
|
@@ -213,9 +277,12 @@ export class StreamConsumer {
|
|
|
213
277
|
logger,
|
|
214
278
|
batchSize = 10,
|
|
215
279
|
blockMs = 5_000,
|
|
280
|
+
bufferAcks = false,
|
|
281
|
+
ackFlushMs = 5,
|
|
282
|
+
coalesceMs = 0,
|
|
216
283
|
}: StreamConsumerOptions) {
|
|
217
284
|
this.redis = redis;
|
|
218
|
-
this.blockingRedis = redis.duplicate();
|
|
285
|
+
this.blockingRedis = redis.duplicate({ enableAutoPipelining: false });
|
|
219
286
|
this.streams = Array.isArray(stream) ? stream : [stream];
|
|
220
287
|
this.group = group;
|
|
221
288
|
this.consumer = consumer;
|
|
@@ -223,14 +290,17 @@ export class StreamConsumer {
|
|
|
223
290
|
this.logger = logger;
|
|
224
291
|
this.batchSize = batchSize;
|
|
225
292
|
this.blockMs = blockMs;
|
|
293
|
+
this.bufferAcks = bufferAcks;
|
|
294
|
+
this.ackFlushMs = ackFlushMs;
|
|
295
|
+
this.coalesceMs = coalesceMs;
|
|
226
296
|
}
|
|
227
297
|
|
|
228
298
|
async ensureGroup(): Promise<void> {
|
|
229
299
|
for (const s of this.streams) {
|
|
230
300
|
try {
|
|
231
301
|
// Start from the beginning so events published before the first worker
|
|
232
|
-
// comes online are not silently skipped.
|
|
233
|
-
//
|
|
302
|
+
// comes online are not silently skipped. Entries are deleted as they are
|
|
303
|
+
// acked (see ackAndDelete), so what is left is only unprocessed work.
|
|
234
304
|
await this.redis.xgroup("CREATE", s, this.group, "0", "MKSTREAM");
|
|
235
305
|
this.logger?.info({ stream: s, group: this.group }, "consumer group created");
|
|
236
306
|
} catch (err) {
|
|
@@ -308,15 +378,19 @@ export class StreamConsumer {
|
|
|
308
378
|
for (const [id, fields] of messages) {
|
|
309
379
|
const msg = parseMessage(id, fields, this.logger);
|
|
310
380
|
if (!msg) {
|
|
311
|
-
await this.
|
|
381
|
+
await this.ackAndDelete(streamName, [id]);
|
|
312
382
|
continue;
|
|
313
383
|
}
|
|
314
384
|
// Attach original stream name for dynamic acking
|
|
315
385
|
msg.stream = streamName as StreamName;
|
|
386
|
+
msg.deliveryCount = 1;
|
|
316
387
|
batch.push(msg);
|
|
317
388
|
}
|
|
318
389
|
}
|
|
319
390
|
if (batch.length > 0) yield batch;
|
|
391
|
+
if (this.coalesceMs > 0 && batch.length < this.batchSize && this.running) {
|
|
392
|
+
await new Promise((resolve) => setTimeout(resolve, this.coalesceMs));
|
|
393
|
+
}
|
|
320
394
|
} catch (err) {
|
|
321
395
|
if (
|
|
322
396
|
!this.running &&
|
|
@@ -325,6 +399,21 @@ export class StreamConsumer {
|
|
|
325
399
|
) {
|
|
326
400
|
break; // Expected during graceful shutdown
|
|
327
401
|
}
|
|
402
|
+
// The group, or the stream itself, is gone: Redis was flushed, restarted
|
|
403
|
+
// without persistence, or failed over to an empty replica. Backing off
|
|
404
|
+
// would retry the same read forever, so recreate it and keep reading.
|
|
405
|
+
if (err instanceof Error && err.message.startsWith("NOGROUP")) {
|
|
406
|
+
try {
|
|
407
|
+
await this.ensureGroup();
|
|
408
|
+
this.logger?.warn(
|
|
409
|
+
{ group: this.group, streams: this.streams },
|
|
410
|
+
"consumer group was missing — recreated it",
|
|
411
|
+
);
|
|
412
|
+
continue;
|
|
413
|
+
} catch (recreateErr) {
|
|
414
|
+
this.logger?.error({ err: recreateErr }, "failed to recreate consumer group");
|
|
415
|
+
}
|
|
416
|
+
}
|
|
328
417
|
this.logger?.error({ err }, "error reading from stream");
|
|
329
418
|
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
330
419
|
retryDelay = Math.min(retryDelay * 2, 30_000); // Exponential backoff up to 30s
|
|
@@ -336,11 +425,97 @@ export class StreamConsumer {
|
|
|
336
425
|
const s = stream ?? this.streams[0]!;
|
|
337
426
|
const ids = Array.isArray(messageId) ? messageId : [messageId];
|
|
338
427
|
if (ids.length === 0) return;
|
|
339
|
-
|
|
340
|
-
this.
|
|
428
|
+
|
|
429
|
+
if (!this.bufferAcks) {
|
|
430
|
+
await this.ackAndDelete(s, ids);
|
|
431
|
+
this.logger?.debug({ stream: s, count: ids.length }, "messages acknowledged");
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
let buf = this.ackBuffers.get(s);
|
|
436
|
+
if (!buf) {
|
|
437
|
+
buf = [];
|
|
438
|
+
this.ackBuffers.set(s, buf);
|
|
439
|
+
}
|
|
440
|
+
buf.push(...ids);
|
|
441
|
+
|
|
442
|
+
if (buf.length >= this.batchSize) {
|
|
443
|
+
await this.flushAcksForStream(s);
|
|
444
|
+
} else if (!this.ackTimer) {
|
|
445
|
+
this.ackTimer = setTimeout(() => {
|
|
446
|
+
this.ackTimer = null;
|
|
447
|
+
void this.flushAcks();
|
|
448
|
+
}, this.ackFlushMs);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Acks and then deletes the entries. Every stream is read by exactly one
|
|
454
|
+
* consumer group, so an acked entry is never read again; leaving it in place
|
|
455
|
+
* holds Redis memory until MAXLEN trims it, and a group recreated from "0"
|
|
456
|
+
* (ensureGroup) would process it a second time. Both commands are issued
|
|
457
|
+
* before either is awaited, so auto-pipelining sends them in one round trip,
|
|
458
|
+
* and Redis runs them in order.
|
|
459
|
+
*/
|
|
460
|
+
private async ackAndDelete(stream: string, ids: string[]): Promise<void> {
|
|
461
|
+
await Promise.all([
|
|
462
|
+
this.redis.xack(stream, this.group, ...ids),
|
|
463
|
+
this.redis.xdel(stream, ...ids),
|
|
464
|
+
]);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
async flushAcksForStream(stream: string): Promise<void> {
|
|
468
|
+
const ids = this.ackBuffers.get(stream);
|
|
469
|
+
if (!ids || ids.length === 0) return;
|
|
470
|
+
this.ackBuffers.set(stream, []);
|
|
471
|
+
try {
|
|
472
|
+
await this.ackAndDelete(stream, ids);
|
|
473
|
+
this.logger?.debug({ stream, count: ids.length }, "buffered messages acknowledged");
|
|
474
|
+
} catch (err) {
|
|
475
|
+
this.logger?.error({ err, stream, count: ids.length }, "failed to flush buffered acks");
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
async flushAcks(): Promise<void> {
|
|
480
|
+
if (this.ackTimer) {
|
|
481
|
+
clearTimeout(this.ackTimer);
|
|
482
|
+
this.ackTimer = null;
|
|
483
|
+
}
|
|
484
|
+
const streams = Array.from(this.ackBuffers.keys());
|
|
485
|
+
await Promise.all(streams.map((s) => this.flushAcksForStream(s)));
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Writes an event that never had a stream entry of its own (one handed
|
|
490
|
+
* straight to a worker in-process) to the dead-letter stream, recorded
|
|
491
|
+
* against this consumer's stream so a replay lands where it would have been
|
|
492
|
+
* read from.
|
|
493
|
+
*/
|
|
494
|
+
async deadLetter(event: StreamEvent, reason?: string): Promise<void> {
|
|
495
|
+
if (!this.dlqStream) {
|
|
496
|
+
this.logger?.error(
|
|
497
|
+
{ eventId: event.id },
|
|
498
|
+
"no dead-letter stream configured — dropping event",
|
|
499
|
+
);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
await this.redis.xadd(
|
|
503
|
+
this.dlqStream,
|
|
504
|
+
"*",
|
|
505
|
+
"data",
|
|
506
|
+
JSON.stringify({
|
|
507
|
+
...event,
|
|
508
|
+
dlq: { originalStream: this.streams[0]!, ackedAt: new Date().toISOString(), reason },
|
|
509
|
+
}),
|
|
510
|
+
);
|
|
341
511
|
}
|
|
342
512
|
|
|
343
|
-
async nack(
|
|
513
|
+
async nack(
|
|
514
|
+
messageId: string,
|
|
515
|
+
event: StreamEvent,
|
|
516
|
+
stream?: string,
|
|
517
|
+
reason?: string,
|
|
518
|
+
): Promise<void> {
|
|
344
519
|
const s = stream ?? this.streams[0]!;
|
|
345
520
|
if (this.dlqStream) {
|
|
346
521
|
// Sequential rather than pipelined, and in this order: the ack is what
|
|
@@ -355,7 +530,7 @@ export class StreamConsumer {
|
|
|
355
530
|
"data",
|
|
356
531
|
JSON.stringify({
|
|
357
532
|
...event,
|
|
358
|
-
dlq: { originalStream: s, ackedAt: new Date().toISOString() },
|
|
533
|
+
dlq: { originalStream: s, ackedAt: new Date().toISOString(), reason },
|
|
359
534
|
}),
|
|
360
535
|
);
|
|
361
536
|
|
|
@@ -363,7 +538,7 @@ export class StreamConsumer {
|
|
|
363
538
|
throw new Error(`XADD to dead-letter stream ${this.dlqStream} returned null`);
|
|
364
539
|
}
|
|
365
540
|
|
|
366
|
-
await this.
|
|
541
|
+
await this.ackAndDelete(s, [messageId]);
|
|
367
542
|
this.logger?.warn(
|
|
368
543
|
{ stream: s, dlqStream: this.dlqStream, messageId, eventId: event.id, dlqId },
|
|
369
544
|
"message moved to dead-letter queue and acked",
|
|
@@ -375,6 +550,7 @@ export class StreamConsumer {
|
|
|
375
550
|
|
|
376
551
|
async stop(): Promise<void> {
|
|
377
552
|
this.running = false;
|
|
553
|
+
await this.flushAcks().catch(() => {});
|
|
378
554
|
try {
|
|
379
555
|
await this.blockingRedis.quit();
|
|
380
556
|
} catch (err: any) {
|
|
@@ -464,6 +640,7 @@ export class PendingMessageScanner {
|
|
|
464
640
|
if (toClaim.length === 0) continue;
|
|
465
641
|
|
|
466
642
|
const ids = toClaim.map((p) => p.id);
|
|
643
|
+
const countsById = new Map<string, number>(toClaim.map((p) => [p.id, p.deliveryCount]));
|
|
467
644
|
// Let Redis arbitrate rather than claiming unconditionally. Two scanners
|
|
468
645
|
// routinely list the same entry, and with a min-idle of 0 both claims
|
|
469
646
|
// succeed and the message is processed twice; with the threshold applied
|
|
@@ -484,6 +661,8 @@ export class PendingMessageScanner {
|
|
|
484
661
|
const msg = parseMessage(id, fields, this.logger);
|
|
485
662
|
if (msg) {
|
|
486
663
|
msg.stream = s;
|
|
664
|
+
const preClaimCount = countsById.get(id) ?? 1;
|
|
665
|
+
msg.deliveryCount = preClaimCount + 1;
|
|
487
666
|
recovered.push(msg);
|
|
488
667
|
}
|
|
489
668
|
}
|