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.
Files changed (81) hide show
  1. package/README.md +37 -1
  2. package/dashboard/dist/assets/{index-7mvXb4bS.js → index-Bsrmo9LE.js} +1 -1
  3. package/dashboard/dist/assets/index-s-SXGIwv.css +1 -0
  4. package/dashboard/dist/index.html +2 -2
  5. package/dist/bootstrap-omGPIBny.mjs +89 -0
  6. package/dist/bulk-C4hTcnLu.mjs +77 -0
  7. package/dist/index.d.mts +1160 -8886
  8. package/dist/index.mjs +2 -38
  9. package/dist/{main-C9s-2ArH.mjs → main-B-AZ-xoN.mjs} +49 -75
  10. package/dist/main-B5BwjIMu.mjs +811 -0
  11. package/dist/{main-B3iQrl3t.mjs → main-BU27VjDC.mjs} +149 -134
  12. package/dist/main-Bes11Ad3.mjs +312 -0
  13. package/dist/{main-BiFiOX9D.mjs → main-BtnGmyaT.mjs} +119 -112
  14. package/dist/{main-39DtKrX4.mjs → main-BxAeC_k6.mjs} +395 -184
  15. package/dist/{main-Dz2sUh3c.mjs → main-DAXO-MiH.mjs} +57 -54
  16. package/dist/{main-BgmT1MPe.mjs → main-DYGCl7Ja.mjs} +135 -79
  17. package/dist/{src-QiTu5e_a.mjs → src-CIPTzq9i.mjs} +1351 -661
  18. package/drizzle/0006_message_logs_redundant_indexes.sql +7 -0
  19. package/drizzle/0007_workflow_status_canceled.sql +3 -0
  20. package/drizzle/meta/_journal.json +14 -0
  21. package/package.json +21 -18
  22. package/scripts/create-admin.mjs +8 -1
  23. package/scripts/migrate.mjs +43 -0
  24. package/src/client.ts +15 -5
  25. package/src/config/index.ts +37 -3
  26. package/src/contracts/events/notification-ai-pending.ts +12 -0
  27. package/src/contracts/events/notification-delivered.ts +7 -0
  28. package/src/contracts/events/notification-dispatched.ts +21 -2
  29. package/src/contracts/events/notification-enriched.ts +16 -0
  30. package/src/contracts/events/notification-failed.ts +2 -0
  31. package/src/contracts/events/notification-requested.ts +2 -0
  32. package/src/contracts/registry.ts +2 -2
  33. package/src/contracts/sdk.ts +77 -29
  34. package/src/db/bulk.ts +132 -0
  35. package/src/db/index.ts +174 -7
  36. package/src/db/schema.ts +3 -50
  37. package/src/idempotency/index.ts +60 -6
  38. package/src/logger/index.ts +0 -12
  39. package/src/queue/index.ts +255 -76
  40. package/src/rate-limiter/index.ts +217 -16
  41. package/src/redis/index.ts +265 -0
  42. package/src/repositories/index.ts +171 -82
  43. package/src/server.ts +64 -41
  44. package/src/services/ai/main.ts +83 -70
  45. package/src/services/api/admin-static.ts +41 -12
  46. package/src/services/api/handlers.ts +357 -159
  47. package/src/services/api/http.ts +34 -7
  48. package/src/services/api/main.ts +104 -67
  49. package/src/services/api/router.ts +18 -5
  50. package/src/services/delivery/main.ts +495 -230
  51. package/src/services/delivery/throttle.ts +42 -59
  52. package/src/services/engine/main.ts +206 -152
  53. package/src/services/enricher/main.ts +155 -107
  54. package/src/services/events/main.ts +100 -55
  55. package/src/services/scheduler/main.ts +151 -79
  56. package/src/services/scheduler/send-gate.ts +117 -0
  57. package/src/services/workflow/main.ts +220 -100
  58. package/src/shared/batch-processor.ts +65 -34
  59. package/src/shared/dataloader.ts +58 -22
  60. package/src/shared/events.ts +3 -0
  61. package/src/shared/index.ts +0 -1
  62. package/src/shared/utils.ts +2 -47
  63. package/src/templates/index.ts +0 -67
  64. package/src/transport/index.ts +6 -0
  65. package/src/workers/bootstrap.ts +124 -0
  66. package/src/workers/index.ts +99 -24
  67. package/src/workflows/sdk.ts +2 -0
  68. package/dashboard/dist/assets/index-o7L-f3-c.css +0 -1
  69. package/dist/index.d.mts.map +0 -1
  70. package/dist/index.mjs.map +0 -1
  71. package/dist/main-39DtKrX4.mjs.map +0 -1
  72. package/dist/main-B3iQrl3t.mjs.map +0 -1
  73. package/dist/main-BM8DH3v3.mjs +0 -632
  74. package/dist/main-BM8DH3v3.mjs.map +0 -1
  75. package/dist/main-BgmT1MPe.mjs.map +0 -1
  76. package/dist/main-BiFiOX9D.mjs.map +0 -1
  77. package/dist/main-C9s-2ArH.mjs.map +0 -1
  78. package/dist/main-Cqo3rSb5.mjs +0 -224
  79. package/dist/main-Cqo3rSb5.mjs.map +0 -1
  80. package/dist/main-Dz2sUh3c.mjs.map +0 -1
  81. package/dist/src-QiTu5e_a.mjs.map +0 -1
@@ -1,632 +0,0 @@
1
- import { $ as metrics, A as RedisClient, G as getPriorityBucket, J as globalEmitter, Qt as CONSUMER_GROUPS, R as CircuitBreaker, S as ContactRepository, X as StreamConsumer, Y as PendingMessageScanner, Z as StreamProducer, Zt as buildStreamEvent, _n as parseConfig, at as createDatabase, c as BaseWorker, cn as registry, dt as scheduledPayloads, gn as loadEnv, it as IdempotencyGuard, l as NonRetryableError, mn as baseConfigSchema, rn as STREAMS, st as deliveryOutbox, tn as OUTBOUND_STREAMS, tt as createLogger, u as startHealthReporter, z as BatchProcessor } from "./src-QiTu5e_a.mjs";
2
- import { transportRegistry } from "./index.mjs";
3
- //#region src/services/delivery/throttle.ts
4
- const LUA_THROTTLE = `
5
- local key = KEYS[1]
6
- local now = tonumber(ARGV[1])
7
- local windowSeconds = tonumber(ARGV[2])
8
- local limit = tonumber(ARGV[3])
9
- local member = ARGV[4]
10
-
11
- local clearBefore = now - (windowSeconds * 1000)
12
-
13
- -- Cleanup expired scores
14
- redis.call('ZREMRANGEBYSCORE', key, 0, clearBefore)
15
-
16
- -- Get current count
17
- local count = redis.call('ZCARD', key)
18
-
19
- if count >= limit then
20
- -- Find the oldest score
21
- local oldest = redis.call('ZRANGE', key, 0, 0, 'WITHSCORES')
22
- if oldest and oldest[2] then
23
- return {0, tonumber(oldest[2])}
24
- end
25
- return {0, now}
26
- end
27
-
28
- -- Add new request
29
- redis.call('ZADD', key, now, member)
30
- redis.call('EXPIRE', key, windowSeconds * 2)
31
- return {1, 0}
32
- `;
33
- async function throttleProvider(redis, channel, config, logger) {
34
- const key = `rate-limit:provider:${channel}`;
35
- const now = Date.now();
36
- const zmember = `${now}:${Math.random()}`;
37
- const result = await redis.eval(LUA_THROTTLE, 1, key, now.toString(), config.windowSeconds.toString(), config.limit.toString(), zmember);
38
- const allowed = result[0] === 1;
39
- const oldestTimestamp = result[1];
40
- let retryAfterMs = 0;
41
- if (!allowed) {
42
- retryAfterMs = Math.max(0, oldestTimestamp + config.windowSeconds * 1e3 - now);
43
- logger.warn({
44
- channel,
45
- limit: config.limit,
46
- windowSeconds: config.windowSeconds,
47
- retryAfterMs
48
- }, "Provider rate limit hit — task must be rescheduled");
49
- }
50
- return {
51
- allowed,
52
- retryAfterMs
53
- };
54
- }
55
- //#endregion
56
- //#region src/services/delivery/main.ts
57
- const deliveryConfigSchema = baseConfigSchema.extend({});
58
- loadEnv();
59
- const config = parseConfig(deliveryConfigSchema, process.env);
60
- let logger;
61
- let redis;
62
- let sql;
63
- let db;
64
- let consumer;
65
- let pendingScanner;
66
- let worker;
67
- let scheduledProducer;
68
- let enrichedProducers;
69
- let healthInterval = null;
70
- global._telemetry = global._telemetry || {
71
- count: 0,
72
- insert: 0,
73
- provider: 0,
74
- flush: 0,
75
- ack: 0,
76
- dequeue: 0,
77
- dbupdate: 0,
78
- flushCount: 0
79
- };
80
- var DeliveryWorker = class extends BaseWorker {
81
- transportRegistry;
82
- idempotency;
83
- redisCli;
84
- scheduledProducer;
85
- enrichedProducers;
86
- contactRepo;
87
- eventsProducer;
88
- globalEmitter;
89
- db;
90
- eventProcessor;
91
- outboxUpdateProcessor;
92
- outboxInsertProcessor;
93
- breakers = /* @__PURE__ */ new Map();
94
- constructor(options) {
95
- super(options);
96
- this.transportRegistry = options.transportRegistry;
97
- this.idempotency = options.idempotency;
98
- this.redisCli = options.redis;
99
- this.scheduledProducer = options.scheduledProducer;
100
- this.enrichedProducers = options.enrichedProducers;
101
- this.contactRepo = options.contactRepo;
102
- this.eventsProducer = options.eventsProducer;
103
- this.globalEmitter = options.globalEmitter;
104
- this.db = options.db;
105
- this.eventProcessor = new BatchProcessor(1e3, 100, async (events) => {
106
- await this.eventsProducer.publishBatch(events);
107
- return events.map(() => void 0);
108
- });
109
- this.outboxUpdateProcessor = new BatchProcessor(500, 100, async (updates) => {
110
- const { sql } = await import("drizzle-orm");
111
- const values = updates.map((update) => ({
112
- taskId: update.taskId,
113
- channel: update.channel,
114
- destination: update.destination,
115
- providerMessageId: update.providerMessageId
116
- }));
117
- const tDbUpdateStart = Date.now();
118
- await this.db.insert(deliveryOutbox).values(values).onConflictDoUpdate({
119
- target: [
120
- deliveryOutbox.taskId,
121
- deliveryOutbox.channel,
122
- deliveryOutbox.destination
123
- ],
124
- set: { providerMessageId: sql`EXCLUDED.provider_message_id` }
125
- }).catch((e) => this.logger.error({ err: e }, "background update failed"));
126
- global._telemetry.dbupdate += Date.now() - tDbUpdateStart;
127
- global._telemetry.flushCount++;
128
- return updates.map(() => void 0);
129
- });
130
- this.outboxInsertProcessor = new BatchProcessor(500, 10, async (tasks) => {
131
- const values = tasks.map((task) => ({
132
- taskId: task.taskId,
133
- channel: task.channel,
134
- destination: task.destination
135
- }));
136
- await this.db.insert(deliveryOutbox).values(values).onConflictDoNothing();
137
- return tasks.map(() => true);
138
- });
139
- }
140
- async stop() {
141
- await Promise.all([
142
- this.eventProcessor.flush(),
143
- this.outboxUpdateProcessor.flush(),
144
- this.outboxInsertProcessor.flush()
145
- ]);
146
- await super.stop();
147
- }
148
- getBreaker(name) {
149
- let breaker = this.breakers.get(name);
150
- if (!breaker) {
151
- breaker = new CircuitBreaker({
152
- failureThreshold: 5,
153
- resetTimeoutMs: 3e4
154
- });
155
- this.breakers.set(name, breaker);
156
- }
157
- return breaker;
158
- }
159
- async process(message, attempt = 1) {
160
- const { event } = message;
161
- const publishPromises = [];
162
- const payloadResult = registry.safeParsePayload("notification.dispatched", event.payload);
163
- if (!payloadResult.success) {
164
- this.logger.warn({
165
- messageId: message.id,
166
- issues: payloadResult.error.issues
167
- }, "invalid notification.dispatched payload — skipping");
168
- return;
169
- }
170
- const task = payloadResult.data;
171
- const fallbackToNextChannel = async (reason) => {
172
- if (task.fallbackChain && task.fallbackChain.length > 0 && task.recipient) {
173
- const nextChannel = task.fallbackChain[0];
174
- const remainingChain = task.fallbackChain.slice(1);
175
- const fallbackPayload = {
176
- projectId: task.projectId,
177
- rawEventId: task.enrichedEventId,
178
- recipientId: task.recipientId,
179
- channel: nextChannel,
180
- priority: task.priority,
181
- templateId: task.templateId,
182
- templateVariables: task.templateVariables,
183
- aiPrompts: task.aiPrompts,
184
- recipient: task.recipient,
185
- scheduledAt: void 0,
186
- fallbackChain: remainingChain.length > 0 ? remainingChain : void 0
187
- };
188
- const p = getPriorityBucket(task.priority);
189
- await (this.enrichedProducers[p] ?? this.enrichedProducers["normal"]).publish(buildStreamEvent("notification.enriched", fallbackPayload, "delivery", event.metadata.traceId));
190
- this.logger.info({
191
- taskId: task.taskId,
192
- reason,
193
- nextChannel,
194
- traceId: event.metadata.traceId
195
- }, "Delivery failed completely, rolling over to next channel in fallback chain");
196
- return true;
197
- }
198
- return false;
199
- };
200
- const transports = this.transportRegistry.getAll(task.channel);
201
- if (transports.length === 0) {
202
- this.logger.warn({
203
- taskId: task.taskId,
204
- channel: task.channel
205
- }, "no transport registered for channel — dropping");
206
- if (!await fallbackToNextChannel("no_transport")) {
207
- this.globalEmitter.emit("delivery:failed", task.taskId, "no transport", task.channel, task.projectId);
208
- await this.eventProcessor.add(buildStreamEvent("notification.failed", {
209
- projectId: task.projectId,
210
- taskId: task.taskId,
211
- enrichedEventId: task.enrichedEventId,
212
- recipientId: task.recipientId,
213
- channel: task.channel,
214
- failureReason: "no transport registered for channel",
215
- failureCode: "no_transport",
216
- retryable: false,
217
- attempt,
218
- templateId: task.templateId,
219
- workflowInstanceId: event.metadata.source === "workflow" ? event.metadata.traceId : void 0,
220
- campaignId: task.campaignId
221
- }, "delivery", event.metadata.traceId));
222
- }
223
- return;
224
- }
225
- const limitConfig = transports[0].limits;
226
- if (limitConfig) {
227
- const { allowed, retryAfterMs } = await throttleProvider(this.redisCli, task.channel, limitConfig, this.logger);
228
- if (!allowed) {
229
- task.throttleAttemptCount = (task.throttleAttemptCount ?? 0) + 1;
230
- const maxAttempts = task.deliveryOptions?.maxAttempts ?? 3;
231
- if (task.throttleAttemptCount > maxAttempts) {
232
- this.logger.warn({
233
- messageId: message.id,
234
- taskId: task.taskId,
235
- attempts: task.throttleAttemptCount
236
- }, "provider rate limit max attempts exceeded");
237
- if (!await fallbackToNextChannel("provider_throttle_exceeded")) {
238
- this.globalEmitter.emit("delivery:failed", task.taskId, "provider throttle exceeded", task.channel, task.projectId);
239
- await this.eventProcessor.add(buildStreamEvent("notification.failed", {
240
- projectId: task.projectId,
241
- taskId: task.taskId,
242
- enrichedEventId: task.enrichedEventId,
243
- recipientId: task.recipientId,
244
- channel: task.channel,
245
- failureReason: "provider rate limit max attempts exceeded",
246
- failureCode: "provider_throttle_exceeded",
247
- retryable: false,
248
- attempt,
249
- templateId: task.templateId,
250
- workflowInstanceId: event.metadata.source === "workflow" ? event.metadata.traceId : void 0,
251
- campaignId: task.campaignId
252
- }, "delivery", event.metadata.traceId));
253
- }
254
- return;
255
- }
256
- const { sql } = await import("drizzle-orm");
257
- await this.db.insert(scheduledPayloads).values({
258
- taskId: task.taskId,
259
- payload: task
260
- }).onConflictDoUpdate({
261
- target: scheduledPayloads.taskId,
262
- set: { payload: sql`EXCLUDED.payload` }
263
- });
264
- await this.scheduledProducer.publish(buildStreamEvent("notification.scheduled", {
265
- projectId: task.projectId,
266
- enrichedEventId: task.enrichedEventId,
267
- taskId: task.taskId,
268
- scheduledAt: new Date(Date.now() + retryAfterMs).toISOString(),
269
- throttleAttemptCount: task.throttleAttemptCount
270
- }, "delivery", `${task.taskId}:throttle:${Date.now()}`));
271
- return;
272
- }
273
- }
274
- const tInsertStart = Date.now();
275
- const idempotencyKey = task.taskId;
276
- if (!await this.idempotency.checkAndMark(idempotencyKey, 60)) {
277
- this.logger.info({
278
- messageId: message.id,
279
- taskId: task.taskId,
280
- channel: task.channel,
281
- attempt
282
- }, "duplicate delivery — skipping");
283
- return;
284
- }
285
- try {
286
- await this.outboxInsertProcessor.add(task);
287
- const insertTime = Date.now() - tInsertStart;
288
- publishPromises.push(this.eventProcessor.add(buildStreamEvent("notification.dispatched", {
289
- projectId: task.projectId,
290
- taskId: task.taskId,
291
- enrichedEventId: task.enrichedEventId,
292
- recipientId: task.recipientId,
293
- channel: task.channel,
294
- templateId: task.templateId,
295
- attempt,
296
- workflowInstanceId: event.metadata.source === "workflow" ? event.metadata.traceId : void 0,
297
- campaignId: task.campaignId
298
- }, "delivery", event.metadata.traceId)));
299
- if (task.channel === "push") {
300
- let lastResult = {
301
- success: false,
302
- error: "No transports"
303
- };
304
- for (const transport of transports) try {
305
- const tProv = Date.now();
306
- lastResult = await this.getBreaker(`${task.channel}:${transport.constructor.name}`).execute(async () => {
307
- const timeoutMs = task.deliveryOptions?.timeoutMs ?? 1e4;
308
- const controller = new AbortController();
309
- const timeout = setTimeout(() => {
310
- controller.abort(/* @__PURE__ */ new Error(`Transport timeout after ${timeoutMs}ms`));
311
- }, timeoutMs);
312
- try {
313
- task.signal = controller.signal;
314
- const res = await Promise.race([transport.send(task), new Promise((_, reject) => {
315
- if (controller.signal.aborted) return reject(controller.signal.reason);
316
- controller.signal.addEventListener("abort", () => reject(controller.signal.reason));
317
- })]);
318
- if (!res.success && !res.invalidToken) throw new Error(res.error ?? "Transport failed");
319
- return res;
320
- } finally {
321
- clearTimeout(timeout);
322
- }
323
- });
324
- global._telemetry.provider += Date.now() - tProv;
325
- if (lastResult.success || lastResult.invalidToken) break;
326
- } catch (err) {
327
- lastResult = {
328
- success: false,
329
- error: err.message
330
- };
331
- }
332
- if (lastResult.invalidToken) {
333
- await this.contactRepo.deactivate(task.projectId, task.recipientId, task.channel, task.destination);
334
- this.logger.info({
335
- channel: task.channel,
336
- target: task.destination
337
- }, "deactivated invalid contact target");
338
- this.globalEmitter.emit("delivery:failed", task.taskId, "invalidToken", task.channel, task.projectId);
339
- metrics.deliveryFailed.inc({
340
- channel: task.channel,
341
- reason: "invalid_token"
342
- });
343
- publishPromises.push(this.eventProcessor.add(buildStreamEvent("notification.failed", {
344
- projectId: task.projectId,
345
- taskId: task.taskId,
346
- enrichedEventId: task.enrichedEventId,
347
- recipientId: task.recipientId,
348
- channel: task.channel,
349
- failureReason: "invalid_token",
350
- failureCode: "push_failure",
351
- retryable: false,
352
- attempt,
353
- templateId: task.templateId,
354
- workflowInstanceId: event.metadata.source === "workflow" ? event.metadata.traceId : void 0,
355
- campaignId: task.campaignId
356
- }, "delivery", event.metadata.traceId)));
357
- if (!await fallbackToNextChannel("invalid_token")) throw new NonRetryableError("Push delivery failed: invalid token");
358
- } else if (lastResult.success) {
359
- const providerMessageId = lastResult.providerMessageId || "push-success";
360
- publishPromises.push(this.outboxUpdateProcessor.add({
361
- taskId: task.taskId,
362
- channel: task.channel,
363
- destination: task.destination,
364
- providerMessageId
365
- }));
366
- this.logger.debug({
367
- taskId: task.taskId,
368
- messageId: providerMessageId
369
- }, "push delivered");
370
- this.globalEmitter.emit("delivery:delivered", task.taskId, providerMessageId, task.channel, task.projectId);
371
- metrics.deliverySuccess.inc({ channel: task.channel });
372
- publishPromises.push(this.eventProcessor.add(buildStreamEvent("notification.delivered", {
373
- projectId: task.projectId,
374
- taskId: task.taskId,
375
- enrichedEventId: task.enrichedEventId,
376
- channel: task.channel,
377
- deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
378
- providerMessageId,
379
- templateId: task.templateId,
380
- workflowInstanceId: event.metadata.source === "workflow" ? event.metadata.traceId : void 0,
381
- campaignId: task.campaignId
382
- }, "delivery", event.metadata.traceId)));
383
- } else {
384
- this.logger.warn({
385
- taskId: task.taskId,
386
- error: lastResult.error
387
- }, "push delivery failed");
388
- this.globalEmitter.emit("delivery:failed", task.taskId, lastResult.error ?? "push delivery failed", task.channel, task.projectId);
389
- metrics.deliveryFailed.inc({
390
- channel: task.channel,
391
- reason: "push_error"
392
- });
393
- publishPromises.push(this.eventProcessor.add(buildStreamEvent("notification.failed", {
394
- projectId: task.projectId,
395
- taskId: task.taskId,
396
- enrichedEventId: task.enrichedEventId,
397
- recipientId: task.recipientId,
398
- channel: task.channel,
399
- failureReason: lastResult.error ?? "push delivery failed",
400
- failureCode: "push_failure",
401
- retryable: false,
402
- attempt,
403
- templateId: task.templateId,
404
- workflowInstanceId: event.metadata.source === "workflow" ? event.metadata.traceId : void 0,
405
- campaignId: task.campaignId
406
- }, "delivery", event.metadata.traceId)));
407
- if (!await fallbackToNextChannel("push_delivery_failed")) throw new NonRetryableError(lastResult.error ?? "Push delivery failed");
408
- }
409
- } else {
410
- let result = {
411
- success: false,
412
- error: "No transports"
413
- };
414
- for (const transport of transports) try {
415
- const tProv = Date.now();
416
- result = await this.getBreaker(`${task.channel}:${transport.constructor.name}`).execute(async () => {
417
- const timeoutMs = task.deliveryOptions?.timeoutMs ?? 1e4;
418
- const controller = new AbortController();
419
- const timeout = setTimeout(() => {
420
- controller.abort(/* @__PURE__ */ new Error(`Transport timeout after ${timeoutMs}ms`));
421
- }, timeoutMs);
422
- try {
423
- task.signal = controller.signal;
424
- const res = await Promise.race([transport.send(task), new Promise((_, reject) => {
425
- if (controller.signal.aborted) return reject(controller.signal.reason);
426
- controller.signal.addEventListener("abort", () => reject(controller.signal.reason));
427
- })]);
428
- if (!res.success) throw new Error(res.error ?? "Transport failed");
429
- return res;
430
- } finally {
431
- clearTimeout(timeout);
432
- }
433
- });
434
- global._telemetry.provider += Date.now() - tProv;
435
- if (result.success) break;
436
- } catch (err) {
437
- result = {
438
- success: false,
439
- error: err.message
440
- };
441
- }
442
- if (result.success) {
443
- const providerMessageId = result.providerMessageId || "success";
444
- publishPromises.push(this.outboxUpdateProcessor.add({
445
- taskId: task.taskId,
446
- channel: task.channel,
447
- destination: task.destination,
448
- providerMessageId
449
- }));
450
- this.globalEmitter.emit("delivery:delivered", task.taskId, providerMessageId, task.channel, task.projectId);
451
- metrics.deliverySuccess.inc({ channel: task.channel });
452
- publishPromises.push(this.eventProcessor.add(buildStreamEvent("notification.delivered", {
453
- projectId: task.projectId,
454
- taskId: task.taskId,
455
- enrichedEventId: task.enrichedEventId,
456
- channel: task.channel,
457
- deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
458
- providerMessageId,
459
- templateId: task.templateId,
460
- workflowInstanceId: event.metadata.source === "workflow" ? event.metadata.traceId : void 0,
461
- campaignId: task.campaignId
462
- }, "delivery", event.metadata.traceId)));
463
- } else publishPromises.push(this.eventProcessor.add(buildStreamEvent("notification.failed", {
464
- projectId: task.projectId,
465
- taskId: task.taskId,
466
- enrichedEventId: task.enrichedEventId,
467
- channel: task.channel,
468
- failureReason: result.error ?? "delivery failed completely",
469
- failureCode: "provider_error",
470
- retryable: false,
471
- attempt,
472
- templateId: task.templateId,
473
- workflowInstanceId: event.metadata.source === "workflow" ? event.metadata.traceId : void 0,
474
- campaignId: task.campaignId
475
- }, "delivery", event.metadata.traceId)));
476
- if (!result.success) {
477
- this.logger.warn({
478
- taskId: task.taskId,
479
- channel: task.channel,
480
- error: result.error
481
- }, "delivery failed completely across providers");
482
- this.globalEmitter.emit("delivery:failed", task.taskId, result.error ?? "delivery failed completely", task.channel, task.projectId);
483
- metrics.deliveryFailed.inc({
484
- channel: task.channel,
485
- reason: "provider_error"
486
- });
487
- if (!await fallbackToNextChannel("all_providers_failed")) throw new NonRetryableError(result.error ?? "delivery failed");
488
- } else this.logger.info({
489
- taskId: task.taskId,
490
- channel: task.channel,
491
- messageId: result.providerMessageId
492
- }, "notification delivered");
493
- }
494
- const tFlushStart = Date.now();
495
- await Promise.all(publishPromises).catch((err) => {
496
- this.logger.error({
497
- err,
498
- taskId: task.taskId
499
- }, "failed to publish post-dispatch events, swallowing error to prevent duplicate delivery");
500
- });
501
- const flushTime = Date.now() - tFlushStart;
502
- await this.idempotency.markProcessed(idempotencyKey);
503
- const t = global._telemetry = global._telemetry || {
504
- count: 0,
505
- insert: 0,
506
- provider: 0,
507
- flush: 0,
508
- ack: 0
509
- };
510
- t.count++;
511
- t.insert += insertTime;
512
- t.flush += flushTime;
513
- if (t.count % 1e3 === 0) {
514
- this.logger.debug(`[Metrics 1000 msgs] Dequeue: ${t.dequeue / 1e3}ms, DB Insert: ${t.insert / 1e3}ms, Provider: ${t.provider / 1e3}ms, Wait for Flush: ${t.flush / 1e3}ms, Ack: ${t.ack / 1e3}ms | DB Update (avg per flush): ${t.dbupdate / Math.max(1, t.flushCount)}ms`);
515
- t.count = 0;
516
- t.insert = 0;
517
- t.provider = 0;
518
- t.flush = 0;
519
- t.dequeue = 0;
520
- t.ack = 0;
521
- t.dbupdate = 0;
522
- t.flushCount = 0;
523
- }
524
- } catch (err) {
525
- await this.idempotency.unmark(idempotencyKey).catch(() => {});
526
- throw err;
527
- }
528
- }
529
- };
530
- async function startDeliveryWorker() {
531
- logger = createLogger({
532
- name: "delivery",
533
- level: config.LOG_LEVEL
534
- });
535
- redis = new RedisClient({
536
- url: config.REDIS_URL,
537
- name: "delivery",
538
- logger
539
- });
540
- const dbData = createDatabase({
541
- url: config.DATABASE_URL,
542
- applicationName: "delivery",
543
- logger
544
- });
545
- sql = dbData.sql;
546
- db = dbData.db;
547
- consumer = new StreamConsumer({
548
- redis: redis.native,
549
- stream: OUTBOUND_STREAMS,
550
- group: CONSUMER_GROUPS.DELIVERY,
551
- consumer: `delivery-${process.pid}`,
552
- dlqStream: STREAMS.DEAD_LETTER,
553
- batchSize: config.WORKER_CONCURRENCY,
554
- logger
555
- });
556
- scheduledProducer = new StreamProducer({
557
- redis: redis.native,
558
- stream: STREAMS.SCHEDULED,
559
- logger
560
- });
561
- pendingScanner = new PendingMessageScanner({
562
- redis: redis.native,
563
- stream: OUTBOUND_STREAMS,
564
- group: CONSUMER_GROUPS.DELIVERY,
565
- consumer: `delivery-${process.pid}`,
566
- logger
567
- });
568
- enrichedProducers = {
569
- critical: new StreamProducer({
570
- redis: redis.native,
571
- stream: STREAMS.ENRICHED_CRITICAL,
572
- logger
573
- }),
574
- normal: new StreamProducer({
575
- redis: redis.native,
576
- stream: STREAMS.ENRICHED_NORMAL,
577
- logger
578
- }),
579
- low: new StreamProducer({
580
- redis: redis.native,
581
- stream: STREAMS.ENRICHED_LOW,
582
- logger
583
- })
584
- };
585
- const contactRepo = new ContactRepository(db);
586
- const idempotency = new IdempotencyGuard({
587
- redis: redis.native,
588
- keyPrefix: "notif:processed:delivery",
589
- ttlSeconds: 86400
590
- });
591
- const eventsProducer = new StreamProducer({
592
- redis: redis.native,
593
- stream: STREAMS.EVENTS_INBOUND,
594
- logger
595
- });
596
- worker = new DeliveryWorker({
597
- consumer,
598
- pendingScanner,
599
- logger,
600
- concurrency: config.WORKER_CONCURRENCY,
601
- transportRegistry,
602
- idempotency,
603
- redis: redis.native,
604
- scheduledProducer,
605
- enrichedProducers,
606
- contactRepo,
607
- eventsProducer,
608
- globalEmitter,
609
- db
610
- });
611
- healthInterval = startHealthReporter("delivery", worker, redis, logger);
612
- logger.info({
613
- env: config.NODE_ENV,
614
- channels: transportRegistry.registeredChannels()
615
- }, "delivery starting");
616
- await worker.start();
617
- }
618
- async function stopDeliveryWorker() {
619
- logger?.info("shutdown initiated");
620
- if (healthInterval) {
621
- clearInterval(healthInterval);
622
- healthInterval = null;
623
- }
624
- if (worker) await worker.stop();
625
- if (sql) await sql.end();
626
- if (redis) await redis.disconnect();
627
- logger?.info("delivery stopped");
628
- }
629
- //#endregion
630
- export { startDeliveryWorker, stopDeliveryWorker };
631
-
632
- //# sourceMappingURL=main-BM8DH3v3.mjs.map