notifkit 0.1.16 → 0.1.18
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 +5 -1
- package/dist/{bootstrap-BMDRPjgp.mjs → bootstrap-Cs1XIp6b.mjs} +1 -1
- package/dist/{bulk-CJ5QXiPa.mjs → bulk-4pRIiG2o.mjs} +1 -1
- package/dist/index.d.mts +37 -106
- package/dist/index.mjs +2 -2
- package/dist/{main-8nP3qmCT.mjs → main-BN7ZfvxN.mjs} +5 -14
- package/dist/{main-6_bXQBDH.mjs → main-Bl0S4GDd.mjs} +3 -3
- package/dist/{main-mGIT8jv4.mjs → main-C5DiM7_r.mjs} +3 -3
- package/dist/{main-B82bsFoy.mjs → main-DVzp0DLn.mjs} +8 -58
- package/dist/{main-ZW5RaiOK.mjs → main-O9iZ6iz2.mjs} +4 -9
- package/dist/{main-CpaHF1Zw.mjs → main-Ppr7Pq1g.mjs} +35 -16
- package/dist/{main-DULD8-th.mjs → main-yL5VinQ5.mjs} +5 -12
- package/dist/{main-g1KEcXXp.mjs → main-zgxAlH3l.mjs} +23 -94
- package/dist/{schedule-or-dispatch-Cd7GLfc3.mjs → schedule-or-dispatch-BwCZWS_P.mjs} +1 -1
- package/dist/{src-BE_EUpZ6.mjs → src-C38w4jR4.mjs} +88 -170
- package/drizzle/0009_retention_time_indexes.sql +4 -0
- package/drizzle/meta/_journal.json +7 -0
- package/package.json +1 -1
- package/src/client.ts +6 -6
- package/src/config/index.ts +9 -11
- package/src/contracts/sdk.ts +9 -9
- package/src/db/bulk.ts +3 -10
- package/src/db/schema.ts +3 -0
- package/src/idempotency/index.ts +12 -53
- package/src/queue/index.ts +9 -12
- package/src/rate-limiter/api-rate-limiter.ts +8 -27
- package/src/rate-limiter/project-settings-cache.ts +4 -16
- package/src/redis/index.ts +3 -51
- package/src/repositories/contact.ts +0 -2
- package/src/server.ts +1 -4
- package/src/services/api/handlers/auth.ts +3 -11
- package/src/services/api/handlers/campaigns.ts +4 -15
- package/src/services/api/handlers/events.ts +1 -2
- package/src/services/api/handlers/notifications.ts +6 -16
- package/src/services/api/handlers/projects.ts +1 -3
- package/src/services/api/handlers/shared.ts +7 -29
- package/src/services/api/handlers/system.ts +1 -1
- package/src/services/api/handlers/unsubscribe.ts +7 -30
- package/src/services/api/handlers/users.ts +1 -2
- package/src/services/api/http.ts +5 -10
- package/src/services/api/main.ts +27 -63
- package/src/services/api/webhooks.ts +8 -19
- package/src/services/auth/index.ts +2 -21
- package/src/services/delivery/main.ts +25 -89
- package/src/services/engine/main.ts +33 -69
- package/src/services/engine/schedule-or-dispatch.ts +7 -4
- package/src/services/enricher/main.ts +19 -49
- package/src/services/events/main.ts +3 -12
- package/src/services/scheduler/main.ts +26 -32
- package/src/services/scheduler/retention.ts +47 -0
- package/src/services/scheduler/send-gate.ts +6 -17
- package/src/services/workflow/duration.ts +1 -6
- package/src/services/workflow/main.ts +3 -9
- package/src/services/workflow/timers.ts +2 -1
- package/src/shared/batch-processor.ts +3 -7
- package/src/shared/utils.ts +2 -8
- package/src/transport/index.ts +0 -4
- package/src/unsubscribe/index.ts +3 -11
- package/src/workers/index.ts +30 -23
package/src/workers/index.ts
CHANGED
|
@@ -4,8 +4,6 @@ import { globalEmitter, AsyncSemaphore } from "@/shared/index.js";
|
|
|
4
4
|
import { metrics } from "@/metrics/index.js";
|
|
5
5
|
export * from "./health.js";
|
|
6
6
|
|
|
7
|
-
// ─── Types ─────────────────────────────────────────────────────────────────
|
|
8
|
-
|
|
9
7
|
export class NonRetryableError extends Error {
|
|
10
8
|
readonly nonRetryable = true;
|
|
11
9
|
constructor(message: string) {
|
|
@@ -44,8 +42,6 @@ export interface WorkerOptions {
|
|
|
44
42
|
maxRetriesBeforeDlq?: number;
|
|
45
43
|
}
|
|
46
44
|
|
|
47
|
-
// ─── BaseWorker ────────────────────────────────────────────────────────────
|
|
48
|
-
|
|
49
45
|
export abstract class BaseWorker {
|
|
50
46
|
protected readonly logger: Logger;
|
|
51
47
|
|
|
@@ -193,21 +189,13 @@ export abstract class BaseWorker {
|
|
|
193
189
|
}
|
|
194
190
|
|
|
195
191
|
/**
|
|
196
|
-
* Processes an event
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
* The caller is still holding its own stream entry, and does not ack it until
|
|
200
|
-
* this returns. A thrown error therefore leaves that entry pending and the
|
|
201
|
-
* whole chain is replayed from the upstream stage; the idempotency keys each
|
|
202
|
-
* stage already checks make the replay safe. Non-retryable failures go to the
|
|
203
|
-
* dead-letter stream here, as they would have from this worker's own stream,
|
|
204
|
-
* so they do not send the upstream message there instead.
|
|
192
|
+
* Processes an event from an upstream stage in this process, skipping the stream.
|
|
193
|
+
* A throw leaves the caller's entry pending, so the chain replays from upstream.
|
|
194
|
+
* Non-retryable failures are dead-lettered here so the upstream message isn't.
|
|
205
195
|
*/
|
|
206
196
|
async processInline(event: StreamEvent): Promise<void> {
|
|
207
197
|
try {
|
|
208
|
-
// `{ ack: false }
|
|
209
|
-
// own stream entry is what gets retried if it fails, so there is nothing
|
|
210
|
-
// for the caller to wait on.
|
|
198
|
+
// `{ ack: false }`: another holder owns it and its own entry gets retried.
|
|
211
199
|
await this.process({ id: event.id, event, deliveryCount: 1, inline: true }, 1);
|
|
212
200
|
this.processedCount += 1;
|
|
213
201
|
this.lastProcessedAt = new Date().toISOString();
|
|
@@ -258,7 +246,7 @@ export abstract class BaseWorker {
|
|
|
258
246
|
return;
|
|
259
247
|
}
|
|
260
248
|
|
|
261
|
-
const result = await this.
|
|
249
|
+
const result = await this.processHoldingLease(message, retryCount);
|
|
262
250
|
if (result && (result as any).ack === false) {
|
|
263
251
|
this.logger.debug(
|
|
264
252
|
{ messageId: message.id, eventType: message.event.type },
|
|
@@ -312,12 +300,31 @@ export abstract class BaseWorker {
|
|
|
312
300
|
}
|
|
313
301
|
|
|
314
302
|
/**
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
303
|
+
* Keeps the entry's idle time under the reclaim threshold while `process` runs,
|
|
304
|
+
* so a long fan-out isn't reclaimed mid-run. If this process dies, the touches stop.
|
|
305
|
+
*/
|
|
306
|
+
private async processHoldingLease(message: StreamMessage, attempt: number) {
|
|
307
|
+
const stream = message.stream;
|
|
308
|
+
const timer = stream
|
|
309
|
+
? setInterval(
|
|
310
|
+
() => {
|
|
311
|
+
this.pendingScanner.touch(message.id, stream).catch((err: unknown) => {
|
|
312
|
+
this.logger.warn({ err, messageId: message.id }, "failed to extend message lease");
|
|
313
|
+
});
|
|
314
|
+
},
|
|
315
|
+
Math.max(Math.floor(this.recoveryIntervalMs / 2), 10),
|
|
316
|
+
)
|
|
317
|
+
: undefined;
|
|
318
|
+
try {
|
|
319
|
+
return await this.process(message, attempt);
|
|
320
|
+
} finally {
|
|
321
|
+
clearInterval(timer);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* `recoveryIntervalMs` is the idle time before a claim, not the scan period.
|
|
327
|
+
* Scan on start and several times per interval so crash-looping workers still recover.
|
|
321
328
|
*/
|
|
322
329
|
private startRecoveryLoop(): void {
|
|
323
330
|
const scan = () => {
|