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
|
@@ -1,25 +1,20 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
1
2
|
import { loadEnv, readBaseConfig } from "@/index.js";
|
|
2
|
-
import {
|
|
3
|
-
import { RedisClient } from "@/index.js";
|
|
3
|
+
import type { Logger } from "@/index.js";
|
|
4
|
+
import type { RedisClient } from "@/index.js";
|
|
5
|
+
import { type StreamConsumer, type PendingMessageScanner, type StreamMessage } from "@/index.js";
|
|
6
|
+
import { BaseWorker, type ProcessResult } from "@/index.js";
|
|
4
7
|
import {
|
|
5
|
-
StreamConsumer,
|
|
6
|
-
PendingMessageScanner,
|
|
7
|
-
StreamProducer,
|
|
8
|
-
type StreamMessage,
|
|
9
|
-
} from "@/index.js";
|
|
10
|
-
import { BaseWorker } from "@/index.js";
|
|
11
|
-
import {
|
|
12
|
-
STREAMS,
|
|
13
8
|
INBOUND_STREAMS,
|
|
14
9
|
CONSUMER_GROUPS,
|
|
15
10
|
registry,
|
|
16
11
|
buildStreamEvent,
|
|
17
12
|
type NotificationCreatedPayload,
|
|
18
13
|
type NotificationEnrichedPayload,
|
|
14
|
+
type NotificationChannel,
|
|
15
|
+
type StreamEvent,
|
|
19
16
|
} from "@/index.js";
|
|
20
|
-
import { type StreamName } from "@/contracts/streams.js";
|
|
21
17
|
import { IdempotencyGuard } from "@/index.js";
|
|
22
|
-
import { createDatabase } from "@/db/index.js";
|
|
23
18
|
import {
|
|
24
19
|
UserRepository,
|
|
25
20
|
PreferenceRepository,
|
|
@@ -27,15 +22,31 @@ import {
|
|
|
27
22
|
ContactRepository,
|
|
28
23
|
} from "@/index.js";
|
|
29
24
|
import { TemplateCache } from "@/templates/index.js";
|
|
30
|
-
import { getPriorityBucket, type WorkerOptions } from "@/shared/index.js";
|
|
25
|
+
import { getPriorityBucket, globalEmitter, type WorkerOptions } from "@/shared/index.js";
|
|
31
26
|
import { startHealthReporter } from "@/workers/index.js";
|
|
27
|
+
import {
|
|
28
|
+
createPriorityProducers,
|
|
29
|
+
createStreamConsumer,
|
|
30
|
+
createWorkerRuntime,
|
|
31
|
+
shutdownWorker,
|
|
32
|
+
uniqueConsumerId,
|
|
33
|
+
} from "@/workers/bootstrap.js";
|
|
32
34
|
|
|
33
35
|
// ─── Bootstrap ─────────────────────────────────────────────────────────────
|
|
34
36
|
|
|
35
37
|
loadEnv();
|
|
36
38
|
const config = readBaseConfig();
|
|
37
39
|
|
|
38
|
-
|
|
40
|
+
/**
|
|
41
|
+
* A caller's own idempotency key (X-Idempotency-Key, or a workflow step's) is a
|
|
42
|
+
* documented 24-hour promise. Without one the key is the event's id, which only
|
|
43
|
+
* a redelivery can bring back, so the guard's shorter TTL is enough.
|
|
44
|
+
*/
|
|
45
|
+
const CALLER_KEY_TTL_SECONDS = 86_400;
|
|
46
|
+
const dedupeTtl = (raw: { idempotencyKey?: string }) =>
|
|
47
|
+
raw.idempotencyKey ? CALLER_KEY_TTL_SECONDS : undefined;
|
|
48
|
+
|
|
49
|
+
let logger: Logger;
|
|
39
50
|
let redis: RedisClient;
|
|
40
51
|
let sql: any;
|
|
41
52
|
let db: any;
|
|
@@ -61,6 +72,8 @@ export class EnricherWorker extends BaseWorker {
|
|
|
61
72
|
private readonly prefRepo: any;
|
|
62
73
|
private readonly contactRepo: any;
|
|
63
74
|
private readonly templateCache: TemplateCache;
|
|
75
|
+
/** Engine in this process, when the pipeline is fused. See `setInlineNext`. */
|
|
76
|
+
private inlineNext: ((event: StreamEvent) => Promise<void>) | null = null;
|
|
64
77
|
|
|
65
78
|
private userBatch: {
|
|
66
79
|
projectId: string;
|
|
@@ -88,11 +101,11 @@ export class EnricherWorker extends BaseWorker {
|
|
|
88
101
|
private async loadContacts(projectId: string, userIds: string[]): Promise<Map<string, any[]>> {
|
|
89
102
|
return new Promise((resolve, reject) => {
|
|
90
103
|
this.contactBatch.push({ projectId, userIds, resolve, reject });
|
|
91
|
-
if (this.contactBatch.length >=
|
|
104
|
+
if (this.contactBatch.length >= Math.min(this.concurrency, 50)) {
|
|
92
105
|
if (this.contactBatchTimer) clearTimeout(this.contactBatchTimer);
|
|
93
106
|
void this.flushContactBatch();
|
|
94
107
|
} else if (!this.contactBatchTimer) {
|
|
95
|
-
this.contactBatchTimer = setTimeout(() => void this.flushContactBatch(),
|
|
108
|
+
this.contactBatchTimer = setTimeout(() => void this.flushContactBatch(), 5);
|
|
96
109
|
}
|
|
97
110
|
});
|
|
98
111
|
}
|
|
@@ -131,10 +144,24 @@ export class EnricherWorker extends BaseWorker {
|
|
|
131
144
|
this.contactRepo = options.contactRepo;
|
|
132
145
|
this.templateCache = options.templateCache;
|
|
133
146
|
|
|
134
|
-
this.flushTimer = setInterval(() => void this.flushWorkerBuffers(),
|
|
147
|
+
this.flushTimer = setInterval(() => void this.flushWorkerBuffers(), 5);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Hands enriched events for single-user notifications straight to `next`
|
|
152
|
+
* instead of the enriched stream. Fan-outs still go through the stream: an
|
|
153
|
+
* inline chain holds this worker's slot until the last send completes, and a
|
|
154
|
+
* segment would hold one slot for thousands of them.
|
|
155
|
+
*/
|
|
156
|
+
setInlineNext(next: ((event: StreamEvent) => Promise<void>) | null): void {
|
|
157
|
+
this.inlineNext = next;
|
|
135
158
|
}
|
|
136
159
|
|
|
137
160
|
override async stop(): Promise<void> {
|
|
161
|
+
// The timers keep running until in-flight messages settle: each of them is
|
|
162
|
+
// waiting on one of these flushes, and clearing them first left the drain
|
|
163
|
+
// to the 30s stop timeout.
|
|
164
|
+
await super.stop();
|
|
138
165
|
if (this.batchTimer) {
|
|
139
166
|
clearTimeout(this.batchTimer);
|
|
140
167
|
this.batchTimer = null;
|
|
@@ -150,7 +177,6 @@ export class EnricherWorker extends BaseWorker {
|
|
|
150
177
|
await this.flushContactBatch();
|
|
151
178
|
await this.flushUserBatch();
|
|
152
179
|
await this.flushWorkerBuffers();
|
|
153
|
-
await super.stop();
|
|
154
180
|
}
|
|
155
181
|
|
|
156
182
|
private async flushWorkerBuffers(): Promise<void> {
|
|
@@ -177,13 +203,13 @@ export class EnricherWorker extends BaseWorker {
|
|
|
177
203
|
private async loadUser(projectId: string, userId: string): Promise<any> {
|
|
178
204
|
return new Promise((resolve, reject) => {
|
|
179
205
|
this.userBatch.push({ projectId, userId, resolve, reject });
|
|
180
|
-
if (this.userBatch.length >=
|
|
206
|
+
if (this.userBatch.length >= Math.min(this.concurrency, 50)) {
|
|
181
207
|
if (this.batchTimer) clearTimeout(this.batchTimer);
|
|
182
208
|
void this.flushUserBatch();
|
|
183
209
|
} else if (!this.batchTimer) {
|
|
184
210
|
this.batchTimer = setTimeout(() => {
|
|
185
211
|
void this.flushUserBatch();
|
|
186
|
-
},
|
|
212
|
+
}, 5);
|
|
187
213
|
}
|
|
188
214
|
});
|
|
189
215
|
}
|
|
@@ -214,7 +240,7 @@ export class EnricherWorker extends BaseWorker {
|
|
|
214
240
|
}
|
|
215
241
|
}
|
|
216
242
|
|
|
217
|
-
async process(message: StreamMessage): Promise<
|
|
243
|
+
async process(message: StreamMessage): Promise<ProcessResult> {
|
|
218
244
|
const { event } = message;
|
|
219
245
|
const publishPromises: Promise<void>[] = [];
|
|
220
246
|
|
|
@@ -243,10 +269,13 @@ export class EnricherWorker extends BaseWorker {
|
|
|
243
269
|
const profile = await this.userRepo.findRecordById(raw.projectId, raw.recipientId);
|
|
244
270
|
if (!profile) return;
|
|
245
271
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
272
|
+
// Opt-outs are keyed on the template's topics, as on the requested
|
|
273
|
+
// path. `event.type` is always "notification.created", which no user
|
|
274
|
+
// ever opts out of, so checking it here let every opt-out through.
|
|
275
|
+
const template = raw.templateId
|
|
276
|
+
? await this.templateCache.getCachedTemplate(raw.projectId, raw.templateId)
|
|
277
|
+
: null;
|
|
278
|
+
const topics: string[] = template?.topics ?? [];
|
|
250
279
|
|
|
251
280
|
const enrichedPayload: NotificationEnrichedPayload = {
|
|
252
281
|
projectId: raw.projectId,
|
|
@@ -262,8 +291,7 @@ export class EnricherWorker extends BaseWorker {
|
|
|
262
291
|
locale: profile.language ?? "en",
|
|
263
292
|
timezone: profile.timezone ?? "UTC",
|
|
264
293
|
preferences: {
|
|
265
|
-
optedOut:
|
|
266
|
-
optedOutTypes.has(event.type) || profile.preferences.topics?.[event.type] === false,
|
|
294
|
+
optedOut: topics.some((t) => profile.preferences.topics?.[t] === false),
|
|
267
295
|
channels: Object.entries(profile.preferences.channels ?? {})
|
|
268
296
|
.filter(([_, enabled]) => !enabled)
|
|
269
297
|
.map(([channel]) => channel as any),
|
|
@@ -293,18 +321,19 @@ export class EnricherWorker extends BaseWorker {
|
|
|
293
321
|
});
|
|
294
322
|
}),
|
|
295
323
|
);
|
|
296
|
-
this.logger.
|
|
324
|
+
this.logger.debug(
|
|
297
325
|
{ messageId: message.id, eventId: event.id, recipientId: raw.recipientId },
|
|
298
326
|
"event enriched",
|
|
299
327
|
);
|
|
300
328
|
} catch (err) {
|
|
329
|
+
await this.idempotency.unmark(dedupeId).catch(() => {});
|
|
301
330
|
throw err;
|
|
302
331
|
}
|
|
303
332
|
await Promise.all(publishPromises).catch(async (err) => {
|
|
304
333
|
await this.idempotency.unmark(dedupeId).catch(() => {});
|
|
305
334
|
throw err;
|
|
306
335
|
});
|
|
307
|
-
await this.idempotency.markProcessed(dedupeId);
|
|
336
|
+
await this.idempotency.markProcessed(dedupeId, dedupeTtl(raw));
|
|
308
337
|
return;
|
|
309
338
|
}
|
|
310
339
|
|
|
@@ -332,24 +361,16 @@ export class EnricherWorker extends BaseWorker {
|
|
|
332
361
|
|
|
333
362
|
const maxUsers = readBaseConfig().SEGMENT_MAX_USERS;
|
|
334
363
|
if (userIds.length > maxUsers) {
|
|
364
|
+
const reason = `Segment fan-out of ${userIds.length} exceeds limit of ${maxUsers}`;
|
|
335
365
|
this.logger.error(
|
|
336
366
|
{ count: userIds.length, max: maxUsers, projectId: raw.projectId, eventId: event.id },
|
|
337
367
|
"Segment fan-out exceeds maximum allowed limit",
|
|
338
368
|
);
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
{
|
|
345
|
-
projectId: raw.projectId,
|
|
346
|
-
rawEventId: event.id,
|
|
347
|
-
error: `Segment fan-out of ${userIds.length} exceeds limit of ${maxUsers}`,
|
|
348
|
-
},
|
|
349
|
-
"enricher",
|
|
350
|
-
event.metadata.traceId,
|
|
351
|
-
),
|
|
352
|
-
);
|
|
369
|
+
// Reported to the caller, not published: a `notification.failed` on
|
|
370
|
+
// the enriched stream is not a payload the engine can read, so the
|
|
371
|
+
// refusal used to vanish there without a trace.
|
|
372
|
+
globalEmitter.emit("notification:failed", event.id, reason, event.type);
|
|
373
|
+
await this.idempotency.markProcessed(dedupeId, dedupeTtl(raw));
|
|
353
374
|
return;
|
|
354
375
|
}
|
|
355
376
|
|
|
@@ -361,21 +382,17 @@ export class EnricherWorker extends BaseWorker {
|
|
|
361
382
|
: null;
|
|
362
383
|
const topics: string[] = template?.topics ?? [];
|
|
363
384
|
|
|
364
|
-
const channels =
|
|
365
|
-
raw.channels && raw.channels.length > 0 ? raw.channels :
|
|
385
|
+
const channels: NotificationChannel[] =
|
|
386
|
+
raw.channels && raw.channels.length > 0 ? raw.channels : ["email"];
|
|
366
387
|
const isFallback = (raw as any).fallback === true;
|
|
367
388
|
|
|
368
|
-
// If fallback is true, we only emit the first channel, and pass the rest in fallbackChain.
|
|
369
|
-
// If fallback is false, we emit all channels concurrently.
|
|
370
|
-
const channelsToProcess = isFallback ? [channels[0]] : channels;
|
|
371
|
-
const fallbackChain = isFallback ? channels.slice(1) : undefined;
|
|
372
|
-
|
|
373
389
|
const chunkArray = <T>(arr: T[], size: number) =>
|
|
374
390
|
Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
|
|
375
391
|
arr.slice(i * size, i * size + size),
|
|
376
392
|
);
|
|
377
393
|
|
|
378
394
|
const chunks = chunkArray(userIds, 500);
|
|
395
|
+
const inline = raw.target.type === "user" ? this.inlineNext : null;
|
|
379
396
|
|
|
380
397
|
for (const chunk of chunks) {
|
|
381
398
|
const profiles = (
|
|
@@ -387,36 +404,63 @@ export class EnricherWorker extends BaseWorker {
|
|
|
387
404
|
);
|
|
388
405
|
|
|
389
406
|
const batchedEvents: Record<
|
|
390
|
-
"critical" | "
|
|
407
|
+
"critical" | "normal" | "low",
|
|
391
408
|
Omit<any, "id" | "timestamp">[]
|
|
392
409
|
> = {
|
|
393
410
|
critical: [],
|
|
394
|
-
high: [],
|
|
395
411
|
normal: [],
|
|
396
412
|
low: [],
|
|
397
413
|
};
|
|
398
414
|
|
|
399
415
|
for (const profile of profiles) {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
416
|
+
const contacts = contactsByUser.get(profile.userId) ?? [];
|
|
417
|
+
// This user's addresses on a channel, less any that opted out of one
|
|
418
|
+
// of this template's topics.
|
|
419
|
+
const usable = (channel: string) =>
|
|
420
|
+
contacts.filter(
|
|
421
|
+
(contact: any) =>
|
|
422
|
+
contact.channel === channel &&
|
|
423
|
+
!topics.some((t) => contact.preferences?.topics?.[t] === false),
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
// Without fallback every requested channel is sent at once. With it,
|
|
427
|
+
// start at the first channel this user can be reached on and chain the
|
|
428
|
+
// rest behind it — a user with no email address still gets the SMS.
|
|
429
|
+
let userChannels: NotificationChannel[] = channels;
|
|
430
|
+
let fallbackChain: NotificationChannel[] | undefined;
|
|
431
|
+
if (isFallback) {
|
|
432
|
+
const first = channels.findIndex((c: NotificationChannel) => usable(c).length > 0);
|
|
433
|
+
if (first === -1) {
|
|
434
|
+
this.logger.info(
|
|
435
|
+
{ recipientId: profile.userId, channels },
|
|
436
|
+
"no active contact on any fallback channel",
|
|
437
|
+
);
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
userChannels = [channels[first]!];
|
|
441
|
+
fallbackChain = channels.slice(first + 1);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
for (const channel of userChannels) {
|
|
445
|
+
const channelContacts = usable(channel);
|
|
403
446
|
// Push resolves its active tokens at send time so token invalidation
|
|
404
447
|
// remains current. Other channels need one task per address.
|
|
405
|
-
const
|
|
406
|
-
channel === "push" ? [undefined] : channelContacts
|
|
407
|
-
if (
|
|
448
|
+
const resolved: ({ id: string; target: string } | undefined)[] =
|
|
449
|
+
channel === "push" ? [undefined] : channelContacts;
|
|
450
|
+
if (resolved.length === 0) {
|
|
408
451
|
this.logger.info(
|
|
409
452
|
{ recipientId: profile.userId, channel },
|
|
410
453
|
"no active contact for channel",
|
|
411
454
|
);
|
|
412
455
|
continue;
|
|
413
456
|
}
|
|
414
|
-
for (const
|
|
457
|
+
for (const contact of resolved) {
|
|
458
|
+
const destination = contact?.target;
|
|
415
459
|
const enrichedPayload: NotificationEnrichedPayload = {
|
|
416
460
|
projectId: raw.projectId,
|
|
417
461
|
rawEventId: event.id,
|
|
418
462
|
recipientId: profile.userId,
|
|
419
|
-
channel
|
|
463
|
+
channel,
|
|
420
464
|
priority: "normal",
|
|
421
465
|
templateId: raw.templateId,
|
|
422
466
|
templateVariables: raw.data,
|
|
@@ -445,7 +489,12 @@ export class EnricherWorker extends BaseWorker {
|
|
|
445
489
|
},
|
|
446
490
|
scheduledAt: raw.scheduledAt,
|
|
447
491
|
fallbackChain: fallbackChain?.length ? fallbackChain : undefined,
|
|
492
|
+
// Lets the engine send to this contact without reading the
|
|
493
|
+
// user's contacts a second time.
|
|
494
|
+
contactId: contact?.id,
|
|
495
|
+
destination,
|
|
448
496
|
campaignId: raw.campaignId,
|
|
497
|
+
workflowInstanceId: raw.workflowInstanceId,
|
|
449
498
|
};
|
|
450
499
|
|
|
451
500
|
const msgPriority = raw.priority ?? "normal";
|
|
@@ -469,17 +518,30 @@ export class EnricherWorker extends BaseWorker {
|
|
|
469
518
|
if (batchedEvents[p].length > 0) {
|
|
470
519
|
const producer = this.producers[p] ?? this.producers.normal;
|
|
471
520
|
for (const ev of batchedEvents[p]) {
|
|
521
|
+
if (inline) {
|
|
522
|
+
publishPromises.push(
|
|
523
|
+
inline({
|
|
524
|
+
...ev,
|
|
525
|
+
id: randomUUID(),
|
|
526
|
+
timestamp: new Date().toISOString(),
|
|
527
|
+
} as StreamEvent),
|
|
528
|
+
);
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
472
531
|
publishPromises.push(
|
|
473
532
|
new Promise((resolve, reject) => {
|
|
474
533
|
this.eventBuffer.push({ producer, event: ev, resolve, reject });
|
|
475
534
|
}),
|
|
476
535
|
);
|
|
477
536
|
}
|
|
537
|
+
if (this.eventBuffer.length >= Math.min(this.concurrency, 50)) {
|
|
538
|
+
void this.flushWorkerBuffers();
|
|
539
|
+
}
|
|
478
540
|
}
|
|
479
541
|
}
|
|
480
542
|
}
|
|
481
543
|
|
|
482
|
-
this.logger.
|
|
544
|
+
this.logger.debug(
|
|
483
545
|
{
|
|
484
546
|
messageId: message.id,
|
|
485
547
|
eventId: event.id,
|
|
@@ -489,6 +551,9 @@ export class EnricherWorker extends BaseWorker {
|
|
|
489
551
|
"event enriched",
|
|
490
552
|
);
|
|
491
553
|
} catch (err) {
|
|
554
|
+
// Release the marker, or a retry landing inside its TTL is taken for a
|
|
555
|
+
// duplicate and acked without ever being processed.
|
|
556
|
+
await this.idempotency.unmark(dedupeId).catch(() => {});
|
|
492
557
|
throw err;
|
|
493
558
|
}
|
|
494
559
|
|
|
@@ -497,48 +562,36 @@ export class EnricherWorker extends BaseWorker {
|
|
|
497
562
|
throw err;
|
|
498
563
|
});
|
|
499
564
|
|
|
500
|
-
await this.idempotency.markProcessed(dedupeId);
|
|
565
|
+
await this.idempotency.markProcessed(dedupeId, dedupeTtl(raw));
|
|
501
566
|
}
|
|
502
567
|
}
|
|
503
568
|
|
|
504
569
|
export async function startEnricherWorker() {
|
|
505
|
-
logger =
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
logger,
|
|
526
|
-
});
|
|
527
|
-
|
|
528
|
-
const producers = {
|
|
529
|
-
critical: new StreamProducer({
|
|
530
|
-
redis: redis.native,
|
|
531
|
-
stream: STREAMS.ENRICHED_CRITICAL,
|
|
532
|
-
logger,
|
|
533
|
-
}),
|
|
534
|
-
normal: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_NORMAL, logger }),
|
|
535
|
-
low: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_LOW, logger }),
|
|
536
|
-
};
|
|
570
|
+
({ logger, redis, sql, db } = createWorkerRuntime(config, "enricher"));
|
|
571
|
+
// A fused pipeline holds the enricher's slot through engine and delivery, so
|
|
572
|
+
// it needs delivery's concurrency, not the CPU-bound default.
|
|
573
|
+
const enricherConcurrency = config.PIPELINE_FUSED
|
|
574
|
+
? Math.max(config.WORKER_CONCURRENCY, config.DELIVERY_CONCURRENCY ?? config.WORKER_CONCURRENCY)
|
|
575
|
+
: config.WORKER_CONCURRENCY;
|
|
576
|
+
const consumerId = uniqueConsumerId("enricher");
|
|
577
|
+
({ consumer, pendingScanner } = createStreamConsumer(
|
|
578
|
+
{ redis, logger },
|
|
579
|
+
{
|
|
580
|
+
stream: INBOUND_STREAMS,
|
|
581
|
+
group: CONSUMER_GROUPS.ENRICHER,
|
|
582
|
+
consumer: consumerId,
|
|
583
|
+
batchSize: enricherConcurrency,
|
|
584
|
+
bufferAcks: true,
|
|
585
|
+
coalesceMs: 2,
|
|
586
|
+
},
|
|
587
|
+
));
|
|
588
|
+
|
|
589
|
+
const producers = createPriorityProducers(redis.native, logger, "ENRICHED");
|
|
537
590
|
|
|
538
591
|
const idempotency = new IdempotencyGuard({
|
|
539
592
|
redis: redis.native,
|
|
540
593
|
keyPrefix: "notif:processed:enricher",
|
|
541
|
-
ttlSeconds:
|
|
594
|
+
ttlSeconds: config.IDEMPOTENCY_TTL_SECONDS,
|
|
542
595
|
});
|
|
543
596
|
|
|
544
597
|
const userRepo = new UserRepository(db);
|
|
@@ -555,14 +608,12 @@ export async function startEnricherWorker() {
|
|
|
555
608
|
// 4. Load all stored preferences for this user
|
|
556
609
|
// 5. Publish notification.enriched to ENRICHED stream
|
|
557
610
|
|
|
558
|
-
// 5. Publish notification.enriched to ENRICHED stream
|
|
559
|
-
|
|
560
611
|
worker = new EnricherWorker({
|
|
561
612
|
consumer,
|
|
562
613
|
pendingScanner,
|
|
563
614
|
logger,
|
|
564
615
|
maxRetriesBeforeDlq: 5,
|
|
565
|
-
concurrency:
|
|
616
|
+
concurrency: enricherConcurrency,
|
|
566
617
|
producers,
|
|
567
618
|
idempotency,
|
|
568
619
|
userRepo,
|
|
@@ -579,16 +630,13 @@ export async function startEnricherWorker() {
|
|
|
579
630
|
await worker.start();
|
|
580
631
|
}
|
|
581
632
|
|
|
633
|
+
export function getEnricherWorker(): EnricherWorker | undefined {
|
|
634
|
+
return worker as EnricherWorker | undefined;
|
|
635
|
+
}
|
|
636
|
+
|
|
582
637
|
// ─── Shutdown ──────────────────────────────────────────────────────────────
|
|
583
638
|
|
|
584
639
|
export async function stopEnricherWorker(): Promise<void> {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
clearInterval(healthInterval);
|
|
588
|
-
healthInterval = null;
|
|
589
|
-
}
|
|
590
|
-
if (worker) await worker.stop();
|
|
591
|
-
if (sql) await sql.end();
|
|
592
|
-
if (redis) await redis.disconnect();
|
|
593
|
-
logger?.info("enricher stopped");
|
|
640
|
+
await shutdownWorker("enricher", { logger, timers: [healthInterval], worker, sql, redis });
|
|
641
|
+
healthInterval = null;
|
|
594
642
|
}
|