@beignet/core 0.0.50 → 0.0.52
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/CHANGELOG.md +24 -0
- package/README.md +99 -32
- package/dist/application/index.d.ts +5 -5
- package/dist/application/index.d.ts.map +1 -1
- package/dist/application/index.js +8 -4
- package/dist/application/index.js.map +1 -1
- package/dist/events/index.d.ts +26 -2
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +84 -7
- package/dist/events/index.js.map +1 -1
- package/dist/events/payload-state.d.ts +16 -0
- package/dist/events/payload-state.d.ts.map +1 -0
- package/dist/events/payload-state.js +123 -0
- package/dist/events/payload-state.js.map +1 -0
- package/dist/events/transport.d.ts +25 -0
- package/dist/events/transport.d.ts.map +1 -0
- package/dist/events/transport.js +192 -0
- package/dist/events/transport.js.map +1 -0
- package/dist/locks/index.d.ts.map +1 -1
- package/dist/locks/index.js +0 -4
- package/dist/locks/index.js.map +1 -1
- package/dist/openapi/index.d.ts.map +1 -1
- package/dist/openapi/index.js +27 -4
- package/dist/openapi/index.js.map +1 -1
- package/dist/outbox/index.d.ts +163 -15
- package/dist/outbox/index.d.ts.map +1 -1
- package/dist/outbox/index.js +1009 -155
- package/dist/outbox/index.js.map +1 -1
- package/dist/payments/index.d.ts.map +1 -1
- package/dist/payments/index.js +0 -4
- package/dist/payments/index.js.map +1 -1
- package/dist/ports/events.d.ts +4 -1
- package/dist/ports/events.d.ts.map +1 -1
- package/dist/ports/index.d.ts +1 -1
- package/dist/ports/index.d.ts.map +1 -1
- package/dist/ports/index.js.map +1 -1
- package/dist/ports/storage.d.ts +7 -0
- package/dist/ports/storage.d.ts.map +1 -1
- package/dist/ports/storage.js +4 -0
- package/dist/ports/storage.js.map +1 -1
- package/dist/ports/testing.d.ts +16 -2
- package/dist/ports/testing.d.ts.map +1 -1
- package/dist/ports/testing.js +19 -4
- package/dist/ports/testing.js.map +1 -1
- package/dist/ports/unit-of-work.d.ts +9 -7
- package/dist/ports/unit-of-work.d.ts.map +1 -1
- package/dist/ports/unit-of-work.js +16 -7
- package/dist/ports/unit-of-work.js.map +1 -1
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/provider.d.ts +0 -38
- package/dist/providers/provider.d.ts.map +1 -1
- package/dist/providers/provider.js.map +1 -1
- package/dist/search/index.d.ts.map +1 -1
- package/dist/search/index.js +2 -6
- package/dist/search/index.js.map +1 -1
- package/dist/server/hooks/cors.d.ts +5 -0
- package/dist/server/hooks/cors.d.ts.map +1 -1
- package/dist/server/hooks/cors.js +29 -1
- package/dist/server/hooks/cors.js.map +1 -1
- package/dist/server/instrumentation.d.ts +5 -5
- package/dist/server/instrumentation.d.ts.map +1 -1
- package/dist/server/instrumentation.js +3 -4
- package/dist/server/instrumentation.js.map +1 -1
- package/dist/server/response-finalization.d.ts.map +1 -1
- package/dist/server/response-finalization.js +25 -13
- package/dist/server/response-finalization.js.map +1 -1
- package/dist/server/server.d.ts +8 -5
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/server.js +35 -12
- package/dist/server/server.js.map +1 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +12 -6
- package/dist/testing/index.js.map +1 -1
- package/dist/uploads/index.d.ts.map +1 -1
- package/dist/uploads/index.js +37 -16
- package/dist/uploads/index.js.map +1 -1
- package/package.json +2 -2
- package/skills/app-architecture/SKILL.md +16 -0
- package/src/application/index.ts +39 -7
- package/src/events/index.ts +137 -7
- package/src/events/payload-state.ts +223 -0
- package/src/events/transport.ts +242 -0
- package/src/locks/index.ts +0 -4
- package/src/openapi/index.ts +41 -3
- package/src/outbox/index.ts +1393 -178
- package/src/payments/index.ts +0 -4
- package/src/ports/events.ts +4 -1
- package/src/ports/index.ts +3 -0
- package/src/ports/storage.ts +10 -0
- package/src/ports/testing.ts +42 -4
- package/src/ports/unit-of-work.ts +33 -15
- package/src/providers/index.ts +0 -1
- package/src/providers/provider.ts +0 -40
- package/src/search/index.ts +2 -6
- package/src/server/hooks/cors.ts +42 -0
- package/src/server/instrumentation.ts +10 -8
- package/src/server/response-finalization.ts +33 -15
- package/src/server/server.ts +61 -27
- package/src/testing/index.ts +20 -9
- package/src/uploads/index.ts +34 -16
package/dist/outbox/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Durable outbox primitives for transactionally recording events and jobs that
|
|
5
5
|
* should be delivered after the owning database transaction commits.
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import { prepareEventPayloadForTransport, } from "../events/index.js";
|
|
8
8
|
import { getJobRetryDelayMs, getJobRetryMaxAttempts, parseJobPayload, SINGLE_ATTEMPT_DISPATCH, shouldRetryJob, } from "../jobs/index.js";
|
|
9
9
|
import { createProviderInstrumentation, } from "../providers/index.js";
|
|
10
10
|
import { captureTraceCarrier, parseTraceCarrier, resolveTracingPort, runWithTracing, } from "../tracing/index.js";
|
|
@@ -12,6 +12,18 @@ import { captureTraceCarrier, parseTraceCarrier, resolveTracingPort, runWithTrac
|
|
|
12
12
|
* Default lease duration for claimed outbox messages.
|
|
13
13
|
*/
|
|
14
14
|
export const DEFAULT_OUTBOX_LEASE_MS = 30_000;
|
|
15
|
+
/**
|
|
16
|
+
* Default maximum messages handled by one bounded drain pass.
|
|
17
|
+
*/
|
|
18
|
+
export const DEFAULT_OUTBOX_BATCH_SIZE = 100;
|
|
19
|
+
/**
|
|
20
|
+
* Default number of outbox messages delivered concurrently.
|
|
21
|
+
*/
|
|
22
|
+
export const DEFAULT_OUTBOX_CONCURRENCY = 1;
|
|
23
|
+
/**
|
|
24
|
+
* Default maximum time Beignet renews a claim for one delivery.
|
|
25
|
+
*/
|
|
26
|
+
export const DEFAULT_OUTBOX_MAX_ACTIVE_MS = 300_000;
|
|
15
27
|
/**
|
|
16
28
|
* Default maximum delivery attempts before a message is dead-lettered.
|
|
17
29
|
*/
|
|
@@ -52,6 +64,49 @@ export class OutboxClaimError extends Error {
|
|
|
52
64
|
this.id = args.id;
|
|
53
65
|
}
|
|
54
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Error persisted when an eligible message has exhausted its claim attempts
|
|
69
|
+
* without reaching a terminal settlement.
|
|
70
|
+
*/
|
|
71
|
+
export class OutboxAbandonedClaimError extends Error {
|
|
72
|
+
/** Message ID whose attempt budget was exhausted. */
|
|
73
|
+
id;
|
|
74
|
+
/** Number of claims already made. */
|
|
75
|
+
attempts;
|
|
76
|
+
/** Maximum permitted claim attempts. */
|
|
77
|
+
maxAttempts;
|
|
78
|
+
constructor(args) {
|
|
79
|
+
super(`Outbox message "${args.id}" exhausted ${args.maxAttempts} claim attempts without a terminal settlement.`);
|
|
80
|
+
this.name = "OutboxAbandonedClaimError";
|
|
81
|
+
this.id = args.id;
|
|
82
|
+
this.attempts = args.attempts;
|
|
83
|
+
this.maxAttempts = args.maxAttempts;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Error surfaced when an outbox worker no longer has a confirmed claim. */
|
|
87
|
+
export class OutboxLeaseLostError extends Error {
|
|
88
|
+
/** Message ID whose ownership was lost. */
|
|
89
|
+
id;
|
|
90
|
+
constructor(args) {
|
|
91
|
+
super(args.message ??
|
|
92
|
+
`Outbox claim for message "${args.id}" is no longer active.`, args.cause === undefined ? undefined : { cause: args.cause });
|
|
93
|
+
this.name = "OutboxLeaseLostError";
|
|
94
|
+
this.id = args.id;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** Error surfaced when delivery exceeds the configured active-claim window. */
|
|
98
|
+
export class OutboxMaxActiveDurationError extends Error {
|
|
99
|
+
/** Message ID whose active window elapsed. */
|
|
100
|
+
id;
|
|
101
|
+
/** Configured maximum active duration. */
|
|
102
|
+
maxActiveMs;
|
|
103
|
+
constructor(args) {
|
|
104
|
+
super(`Outbox delivery for message "${args.id}" exceeded the ${args.maxActiveMs}ms active-claim limit.`);
|
|
105
|
+
this.name = "OutboxMaxActiveDurationError";
|
|
106
|
+
this.id = args.id;
|
|
107
|
+
this.maxActiveMs = args.maxActiveMs;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
55
110
|
/**
|
|
56
111
|
* Error thrown when an outbox admin operation cannot be completed safely.
|
|
57
112
|
*/
|
|
@@ -425,9 +480,11 @@ export function createMemoryOutbox(storeOptions = {}) {
|
|
|
425
480
|
assertPositiveInteger("limit", options.limit);
|
|
426
481
|
const now = options.now ?? storeNow();
|
|
427
482
|
const leaseMs = options.leaseMs ?? DEFAULT_OUTBOX_LEASE_MS;
|
|
483
|
+
assertValidDate("now", now);
|
|
428
484
|
assertPositiveInteger("leaseMs", leaseMs);
|
|
429
485
|
const lockedUntil = new Date(now.getTime() + leaseMs);
|
|
430
486
|
const claimed = [];
|
|
487
|
+
const deadLettered = [];
|
|
431
488
|
const eligible = [...messages.values()]
|
|
432
489
|
.filter((message) => isEligible(message, now))
|
|
433
490
|
.sort((a, b) => {
|
|
@@ -438,6 +495,20 @@ export function createMemoryOutbox(storeOptions = {}) {
|
|
|
438
495
|
})
|
|
439
496
|
.slice(0, options.limit);
|
|
440
497
|
for (const message of eligible) {
|
|
498
|
+
if (message.attempts >= message.maxAttempts) {
|
|
499
|
+
message.status = "deadLettered";
|
|
500
|
+
message.lastError = serializeOutboxError(new OutboxAbandonedClaimError({
|
|
501
|
+
id: message.id,
|
|
502
|
+
attempts: message.attempts,
|
|
503
|
+
maxAttempts: message.maxAttempts,
|
|
504
|
+
}));
|
|
505
|
+
message.claimToken = null;
|
|
506
|
+
message.claimedAt = null;
|
|
507
|
+
message.lockedUntil = null;
|
|
508
|
+
message.updatedAt = cloneDate(now);
|
|
509
|
+
deadLettered.push(copyMessage(message));
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
441
512
|
message.status = "claimed";
|
|
442
513
|
message.attempts += 1;
|
|
443
514
|
message.claimToken = createStoreId();
|
|
@@ -446,13 +517,41 @@ export function createMemoryOutbox(storeOptions = {}) {
|
|
|
446
517
|
message.updatedAt = cloneDate(now);
|
|
447
518
|
claimed.push(toClaimedMessage(message));
|
|
448
519
|
}
|
|
449
|
-
return claimed;
|
|
520
|
+
return { claimed, deadLettered };
|
|
521
|
+
},
|
|
522
|
+
async renewClaim(input) {
|
|
523
|
+
assertNonEmptyString("id", input.id);
|
|
524
|
+
assertNonEmptyString("claimToken", input.claimToken);
|
|
525
|
+
const message = getClaimedOrThrow(input.id, input.claimToken);
|
|
526
|
+
const now = input.now ?? storeNow();
|
|
527
|
+
const leaseMs = input.leaseMs ?? DEFAULT_OUTBOX_LEASE_MS;
|
|
528
|
+
assertValidDate("now", now);
|
|
529
|
+
assertPositiveInteger("leaseMs", leaseMs);
|
|
530
|
+
if (message.lockedUntil === null ||
|
|
531
|
+
message.lockedUntil.getTime() <= now.getTime()) {
|
|
532
|
+
throw new OutboxClaimError({
|
|
533
|
+
id: input.id,
|
|
534
|
+
message: `Outbox message "${input.id}" no longer has an active claim to renew.`,
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
const lockedUntil = new Date(now.getTime() + leaseMs);
|
|
538
|
+
message.lockedUntil = cloneDate(lockedUntil);
|
|
539
|
+
message.updatedAt = cloneDate(now);
|
|
540
|
+
return { lockedUntil: cloneDate(lockedUntil) };
|
|
450
541
|
},
|
|
451
542
|
async markDelivered(input) {
|
|
452
543
|
assertNonEmptyString("id", input.id);
|
|
453
544
|
assertNonEmptyString("claimToken", input.claimToken);
|
|
454
545
|
const message = getClaimedOrThrow(input.id, input.claimToken);
|
|
455
546
|
const now = input.now ?? storeNow();
|
|
547
|
+
assertValidDate("now", now);
|
|
548
|
+
if (message.lockedUntil === null ||
|
|
549
|
+
message.lockedUntil.getTime() <= now.getTime()) {
|
|
550
|
+
throw new OutboxClaimError({
|
|
551
|
+
id: input.id,
|
|
552
|
+
message: `Outbox message "${input.id}" no longer has an active claim to settle.`,
|
|
553
|
+
});
|
|
554
|
+
}
|
|
456
555
|
message.status = "delivered";
|
|
457
556
|
message.deliveredAt = cloneDate(now);
|
|
458
557
|
message.claimToken = null;
|
|
@@ -465,6 +564,16 @@ export function createMemoryOutbox(storeOptions = {}) {
|
|
|
465
564
|
assertNonEmptyString("claimToken", input.claimToken);
|
|
466
565
|
const message = getClaimedOrThrow(input.id, input.claimToken);
|
|
467
566
|
const now = input.now ?? storeNow();
|
|
567
|
+
assertValidDate("now", now);
|
|
568
|
+
if (input.retryAt)
|
|
569
|
+
assertValidDate("retryAt", input.retryAt);
|
|
570
|
+
if (message.lockedUntil === null ||
|
|
571
|
+
message.lockedUntil.getTime() <= now.getTime()) {
|
|
572
|
+
throw new OutboxClaimError({
|
|
573
|
+
id: input.id,
|
|
574
|
+
message: `Outbox message "${input.id}" no longer has an active claim to settle.`,
|
|
575
|
+
});
|
|
576
|
+
}
|
|
468
577
|
message.status = input.deadLetter ? "deadLettered" : "pending";
|
|
469
578
|
message.lastError = serializeOutboxError(input.error);
|
|
470
579
|
message.availableAt = input.retryAt
|
|
@@ -505,13 +614,16 @@ export function defineOutboxRegistry(input) {
|
|
|
505
614
|
* Validate an event payload and enqueue it as an outbox message.
|
|
506
615
|
*/
|
|
507
616
|
export async function enqueueEvent(outbox, event, payload, options = {}) {
|
|
508
|
-
await
|
|
617
|
+
const prepared = await prepareEventPayloadForTransport(event, payload);
|
|
618
|
+
return await enqueueTransportEvent(outbox, event, prepared.transportValue, options);
|
|
619
|
+
}
|
|
620
|
+
async function enqueueTransportEvent(outbox, event, payload, options) {
|
|
509
621
|
const trace = parseTraceCarrier(options.trace) ?? captureTraceCarrier(options.tracing);
|
|
510
622
|
return outbox.enqueue({
|
|
511
623
|
id: options.id,
|
|
512
624
|
kind: "event",
|
|
513
625
|
name: event.name,
|
|
514
|
-
payload
|
|
626
|
+
payload,
|
|
515
627
|
trace,
|
|
516
628
|
availableAt: options.availableAt,
|
|
517
629
|
maxAttempts: options.maxAttempts,
|
|
@@ -539,9 +651,10 @@ export async function enqueueJob(outbox, job, payload, options = {}) {
|
|
|
539
651
|
export function createOutboxEventRecorder(outbox, options = {}) {
|
|
540
652
|
return {
|
|
541
653
|
async record(event, payload, publishOptions) {
|
|
542
|
-
await
|
|
654
|
+
const prepared = await prepareEventPayloadForTransport(event, payload, publishOptions);
|
|
655
|
+
await enqueueTransportEvent(outbox, event, prepared.transportValue, {
|
|
543
656
|
...options,
|
|
544
|
-
trace: publishOptions
|
|
657
|
+
trace: prepared.publishOptions.trace ?? options.trace,
|
|
545
658
|
});
|
|
546
659
|
},
|
|
547
660
|
};
|
|
@@ -612,8 +725,8 @@ async function deliverOutboxMessage(options, message, trace) {
|
|
|
612
725
|
if (!event) {
|
|
613
726
|
throw new OutboxRegistryError(`Outbox registry does not include event "${message.name}".`);
|
|
614
727
|
}
|
|
615
|
-
await
|
|
616
|
-
await options.eventBus.publish(event,
|
|
728
|
+
const prepared = await prepareEventPayloadForTransport(event, message.payload, trace ? { trace } : undefined);
|
|
729
|
+
await options.eventBus.publish(event, prepared.payload, prepared.publishOptions);
|
|
617
730
|
return;
|
|
618
731
|
}
|
|
619
732
|
if (!options.jobs) {
|
|
@@ -640,18 +753,835 @@ async function deliverOutboxMessage(options, message, trace) {
|
|
|
640
753
|
}
|
|
641
754
|
await options.jobs.dispatch(job, message.payload, trace ? { trace } : undefined);
|
|
642
755
|
}
|
|
756
|
+
const MAX_TIMER_DELAY_MS = 2_147_483_647;
|
|
757
|
+
const OUTBOX_SETTLEMENT_ATTEMPTS = 3;
|
|
758
|
+
const OUTBOX_SETTLEMENT_RETRY_DELAY_MS = 100;
|
|
759
|
+
function defaultOutboxWait(delayMs, signal) {
|
|
760
|
+
return new Promise((resolve) => {
|
|
761
|
+
if (signal.aborted) {
|
|
762
|
+
resolve();
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
let timer;
|
|
766
|
+
const finish = () => {
|
|
767
|
+
if (timer !== undefined)
|
|
768
|
+
clearTimeout(timer);
|
|
769
|
+
signal.removeEventListener("abort", finish);
|
|
770
|
+
resolve();
|
|
771
|
+
};
|
|
772
|
+
timer = setTimeout(finish, delayMs);
|
|
773
|
+
signal.addEventListener("abort", finish, { once: true });
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
function readOutboxNow(now) {
|
|
777
|
+
const value = now();
|
|
778
|
+
assertValidDate("now", value);
|
|
779
|
+
return value;
|
|
780
|
+
}
|
|
781
|
+
function assertTimerDuration(name, value) {
|
|
782
|
+
assertPositiveInteger(name, value);
|
|
783
|
+
if (value > MAX_TIMER_DELAY_MS) {
|
|
784
|
+
throw new Error(`${name} must be at most ${MAX_TIMER_DELAY_MS}`);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
function resolveDrainRuntime(options) {
|
|
788
|
+
const batchSize = options.batchSize ?? DEFAULT_OUTBOX_BATCH_SIZE;
|
|
789
|
+
const concurrency = options.concurrency ?? DEFAULT_OUTBOX_CONCURRENCY;
|
|
790
|
+
const leaseMs = options.leaseMs ?? DEFAULT_OUTBOX_LEASE_MS;
|
|
791
|
+
const heartbeatMs = options.heartbeatMs ?? Math.max(1, Math.floor(leaseMs / 3));
|
|
792
|
+
const maxActiveMs = options.maxActiveMs ?? DEFAULT_OUTBOX_MAX_ACTIVE_MS;
|
|
793
|
+
assertPositiveInteger("batchSize", batchSize);
|
|
794
|
+
assertPositiveInteger("concurrency", concurrency);
|
|
795
|
+
if (concurrency > batchSize) {
|
|
796
|
+
throw new Error("concurrency must be less than or equal to batchSize");
|
|
797
|
+
}
|
|
798
|
+
assertTimerDuration("leaseMs", leaseMs);
|
|
799
|
+
if (leaseMs < 2)
|
|
800
|
+
throw new Error("leaseMs must be at least 2");
|
|
801
|
+
assertTimerDuration("heartbeatMs", heartbeatMs);
|
|
802
|
+
if (heartbeatMs >= leaseMs) {
|
|
803
|
+
throw new Error("heartbeatMs must be shorter than leaseMs");
|
|
804
|
+
}
|
|
805
|
+
assertTimerDuration("maxActiveMs", maxActiveMs);
|
|
806
|
+
return {
|
|
807
|
+
batchSize,
|
|
808
|
+
concurrency,
|
|
809
|
+
leaseMs,
|
|
810
|
+
heartbeatMs,
|
|
811
|
+
maxActiveMs,
|
|
812
|
+
now: options.now ?? (() => new Date()),
|
|
813
|
+
wait: options.wait ?? defaultOutboxWait,
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
async function notifyLeaseFailure(options, failure) {
|
|
817
|
+
try {
|
|
818
|
+
await options.onLeaseError?.(failure);
|
|
819
|
+
}
|
|
820
|
+
catch {
|
|
821
|
+
// Lease observers cannot change delivery ownership or recovery behavior.
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
async function notifySettlementFailure(options, failure) {
|
|
825
|
+
try {
|
|
826
|
+
await options.onSettlementError?.(failure);
|
|
827
|
+
}
|
|
828
|
+
catch {
|
|
829
|
+
// Settlement observers cannot replace the unknown storage outcome.
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
async function runBoundedClaimRenewal(options) {
|
|
833
|
+
const remainingMs = options.deadline.getTime() - options.renewalAt.getTime();
|
|
834
|
+
if (remainingMs <= 0)
|
|
835
|
+
return { kind: "deadline" };
|
|
836
|
+
if (options.signal.aborted)
|
|
837
|
+
return { kind: "stopped" };
|
|
838
|
+
const waitController = new AbortController();
|
|
839
|
+
const stopWaiting = () => waitController.abort();
|
|
840
|
+
options.signal.addEventListener("abort", stopWaiting, { once: true });
|
|
841
|
+
const renewal = Promise.resolve()
|
|
842
|
+
.then(() => options.outbox.renewClaim({
|
|
843
|
+
id: options.message.id,
|
|
844
|
+
claimToken: options.message.claimToken,
|
|
845
|
+
now: options.renewalAt,
|
|
846
|
+
leaseMs: options.runtime.leaseMs,
|
|
847
|
+
}))
|
|
848
|
+
.then((result) => ({ kind: "succeeded", result }), (error) => ({ kind: "failed", error }));
|
|
849
|
+
const deadline = Promise.resolve()
|
|
850
|
+
.then(() => options.runtime.wait(remainingMs, waitController.signal))
|
|
851
|
+
.then(() => options.signal.aborted
|
|
852
|
+
? { kind: "stopped" }
|
|
853
|
+
: { kind: "deadline" }, (error) => options.signal.aborted
|
|
854
|
+
? { kind: "stopped" }
|
|
855
|
+
: { kind: "waitFailed", error });
|
|
856
|
+
const result = await Promise.race([renewal, deadline]);
|
|
857
|
+
options.signal.removeEventListener("abort", stopWaiting);
|
|
858
|
+
waitController.abort();
|
|
859
|
+
if (result.kind !== "succeeded")
|
|
860
|
+
return result;
|
|
861
|
+
try {
|
|
862
|
+
const completedAt = readOutboxNow(options.runtime.now);
|
|
863
|
+
if (completedAt.getTime() >= options.deadline.getTime()) {
|
|
864
|
+
return { kind: "deadline" };
|
|
865
|
+
}
|
|
866
|
+
assertValidDate("lockedUntil", result.result.lockedUntil);
|
|
867
|
+
if (result.result.lockedUntil.getTime() <= completedAt.getTime()) {
|
|
868
|
+
return {
|
|
869
|
+
kind: "failed",
|
|
870
|
+
error: new Error(`Outbox claim renewal for message "${options.message.id}" returned an expired lease.`),
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
return {
|
|
874
|
+
kind: "succeeded",
|
|
875
|
+
lockedUntil: cloneDate(result.result.lockedUntil),
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
catch (error) {
|
|
879
|
+
return { kind: "failed", error };
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
function createClaimHeartbeat(options, runtime, message, activeUntil) {
|
|
883
|
+
const controller = new AbortController();
|
|
884
|
+
let stopped = false;
|
|
885
|
+
let lockedUntil = cloneDate(message.lockedUntil);
|
|
886
|
+
let renewing = false;
|
|
887
|
+
let firstRenewalError;
|
|
888
|
+
let terminalFailure;
|
|
889
|
+
let resolveLost = () => { };
|
|
890
|
+
const lost = new Promise((resolve) => {
|
|
891
|
+
resolveLost = resolve;
|
|
892
|
+
});
|
|
893
|
+
const fail = (failure) => {
|
|
894
|
+
if (terminalFailure)
|
|
895
|
+
return;
|
|
896
|
+
terminalFailure = failure;
|
|
897
|
+
stopped = true;
|
|
898
|
+
controller.abort();
|
|
899
|
+
resolveLost(failure);
|
|
900
|
+
};
|
|
901
|
+
const run = async () => {
|
|
902
|
+
try {
|
|
903
|
+
let delayMs = runtime.heartbeatMs;
|
|
904
|
+
while (!stopped) {
|
|
905
|
+
try {
|
|
906
|
+
await runtime.wait(delayMs, controller.signal);
|
|
907
|
+
}
|
|
908
|
+
catch (error) {
|
|
909
|
+
if (stopped || controller.signal.aborted)
|
|
910
|
+
return;
|
|
911
|
+
fail({
|
|
912
|
+
error: new OutboxLeaseLostError({
|
|
913
|
+
id: message.id,
|
|
914
|
+
message: `Outbox heartbeat scheduling failed for message "${message.id}".`,
|
|
915
|
+
cause: error,
|
|
916
|
+
}),
|
|
917
|
+
message,
|
|
918
|
+
operation: "renewClaim",
|
|
919
|
+
state: "lost",
|
|
920
|
+
confirmedLost: false,
|
|
921
|
+
});
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
924
|
+
if (stopped || controller.signal.aborted)
|
|
925
|
+
return;
|
|
926
|
+
const renewalAt = readOutboxNow(runtime.now);
|
|
927
|
+
const renewalDeadline = new Date(Math.min(lockedUntil.getTime(), activeUntil.getTime()));
|
|
928
|
+
const activeDeadlineEndsFirst = activeUntil.getTime() <= lockedUntil.getTime();
|
|
929
|
+
if (renewalAt.getTime() >= renewalDeadline.getTime()) {
|
|
930
|
+
fail({
|
|
931
|
+
error: activeDeadlineEndsFirst
|
|
932
|
+
? new OutboxMaxActiveDurationError({
|
|
933
|
+
id: message.id,
|
|
934
|
+
maxActiveMs: runtime.maxActiveMs,
|
|
935
|
+
})
|
|
936
|
+
: new OutboxLeaseLostError({ id: message.id }),
|
|
937
|
+
message,
|
|
938
|
+
operation: activeDeadlineEndsFirst
|
|
939
|
+
? "maxActiveDuration"
|
|
940
|
+
: "renewClaim",
|
|
941
|
+
state: "lost",
|
|
942
|
+
confirmedLost: !activeDeadlineEndsFirst,
|
|
943
|
+
});
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
renewing = true;
|
|
947
|
+
let renewal;
|
|
948
|
+
try {
|
|
949
|
+
renewal = await runBoundedClaimRenewal({
|
|
950
|
+
outbox: options.outbox,
|
|
951
|
+
message,
|
|
952
|
+
runtime,
|
|
953
|
+
renewalAt,
|
|
954
|
+
deadline: renewalDeadline,
|
|
955
|
+
signal: controller.signal,
|
|
956
|
+
});
|
|
957
|
+
}
|
|
958
|
+
catch (error) {
|
|
959
|
+
renewal = { kind: "failed", error };
|
|
960
|
+
}
|
|
961
|
+
finally {
|
|
962
|
+
renewing = false;
|
|
963
|
+
}
|
|
964
|
+
if (renewal.kind === "stopped")
|
|
965
|
+
return;
|
|
966
|
+
if (renewal.kind === "waitFailed") {
|
|
967
|
+
fail({
|
|
968
|
+
error: new OutboxLeaseLostError({
|
|
969
|
+
id: message.id,
|
|
970
|
+
message: `Could not enforce the renewal deadline for outbox message "${message.id}".`,
|
|
971
|
+
cause: renewal.error,
|
|
972
|
+
}),
|
|
973
|
+
message,
|
|
974
|
+
operation: "renewClaim",
|
|
975
|
+
state: "lost",
|
|
976
|
+
confirmedLost: false,
|
|
977
|
+
});
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
if (renewal.kind === "deadline") {
|
|
981
|
+
fail({
|
|
982
|
+
error: activeDeadlineEndsFirst
|
|
983
|
+
? new OutboxMaxActiveDurationError({
|
|
984
|
+
id: message.id,
|
|
985
|
+
maxActiveMs: runtime.maxActiveMs,
|
|
986
|
+
})
|
|
987
|
+
: new OutboxLeaseLostError({
|
|
988
|
+
id: message.id,
|
|
989
|
+
message: `Outbox claim renewal for message "${message.id}" did not complete before the lease deadline.`,
|
|
990
|
+
}),
|
|
991
|
+
message,
|
|
992
|
+
operation: activeDeadlineEndsFirst
|
|
993
|
+
? "maxActiveDuration"
|
|
994
|
+
: "renewClaim",
|
|
995
|
+
state: "lost",
|
|
996
|
+
confirmedLost: false,
|
|
997
|
+
});
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
if (renewal.kind === "succeeded") {
|
|
1001
|
+
lockedUntil = renewal.lockedUntil;
|
|
1002
|
+
delayMs = runtime.heartbeatMs;
|
|
1003
|
+
continue;
|
|
1004
|
+
}
|
|
1005
|
+
const error = renewal.error;
|
|
1006
|
+
if (error instanceof OutboxClaimError) {
|
|
1007
|
+
fail({
|
|
1008
|
+
error,
|
|
1009
|
+
message,
|
|
1010
|
+
operation: "renewClaim",
|
|
1011
|
+
state: "lost",
|
|
1012
|
+
confirmedLost: true,
|
|
1013
|
+
});
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
firstRenewalError ??= error;
|
|
1017
|
+
if (stopped)
|
|
1018
|
+
return;
|
|
1019
|
+
const retryAt = readOutboxNow(runtime.now);
|
|
1020
|
+
const remainingMs = renewalDeadline.getTime() - retryAt.getTime();
|
|
1021
|
+
if (remainingMs <= 0) {
|
|
1022
|
+
fail({
|
|
1023
|
+
error: activeDeadlineEndsFirst
|
|
1024
|
+
? new OutboxMaxActiveDurationError({
|
|
1025
|
+
id: message.id,
|
|
1026
|
+
maxActiveMs: runtime.maxActiveMs,
|
|
1027
|
+
})
|
|
1028
|
+
: new OutboxLeaseLostError({
|
|
1029
|
+
id: message.id,
|
|
1030
|
+
message: `Outbox claim renewal for message "${message.id}" did not recover before the lease expired.`,
|
|
1031
|
+
cause: error,
|
|
1032
|
+
}),
|
|
1033
|
+
message,
|
|
1034
|
+
operation: activeDeadlineEndsFirst
|
|
1035
|
+
? "maxActiveDuration"
|
|
1036
|
+
: "renewClaim",
|
|
1037
|
+
state: "lost",
|
|
1038
|
+
confirmedLost: false,
|
|
1039
|
+
});
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
delayMs = Math.max(1, Math.min(runtime.heartbeatMs, Math.floor(remainingMs / 3)));
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
catch (error) {
|
|
1046
|
+
if (stopped || controller.signal.aborted)
|
|
1047
|
+
return;
|
|
1048
|
+
fail({
|
|
1049
|
+
error: new OutboxLeaseLostError({
|
|
1050
|
+
id: message.id,
|
|
1051
|
+
message: `Outbox claim renewal failed unexpectedly for message "${message.id}".`,
|
|
1052
|
+
cause: error,
|
|
1053
|
+
}),
|
|
1054
|
+
message,
|
|
1055
|
+
operation: "renewClaim",
|
|
1056
|
+
state: "lost",
|
|
1057
|
+
confirmedLost: false,
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
1061
|
+
const running = run();
|
|
1062
|
+
const stop = async (abortRenewal = true) => {
|
|
1063
|
+
stopped = true;
|
|
1064
|
+
if (abortRenewal || !renewing)
|
|
1065
|
+
controller.abort();
|
|
1066
|
+
await running;
|
|
1067
|
+
controller.abort();
|
|
1068
|
+
};
|
|
1069
|
+
return {
|
|
1070
|
+
lost,
|
|
1071
|
+
stop: () => stop(true),
|
|
1072
|
+
async stopAndExtend() {
|
|
1073
|
+
await stop(false);
|
|
1074
|
+
if (terminalFailure) {
|
|
1075
|
+
return { lockedUntil, failure: terminalFailure };
|
|
1076
|
+
}
|
|
1077
|
+
const renewalAt = readOutboxNow(runtime.now);
|
|
1078
|
+
const activeDeadlineEndsFirst = activeUntil.getTime() <= lockedUntil.getTime();
|
|
1079
|
+
const finalRenewalDeadline = new Date(Math.min(lockedUntil.getTime(), activeUntil.getTime()));
|
|
1080
|
+
if (renewalAt.getTime() >= finalRenewalDeadline.getTime()) {
|
|
1081
|
+
return {
|
|
1082
|
+
lockedUntil,
|
|
1083
|
+
failure: {
|
|
1084
|
+
error: activeDeadlineEndsFirst
|
|
1085
|
+
? new OutboxMaxActiveDurationError({
|
|
1086
|
+
id: message.id,
|
|
1087
|
+
maxActiveMs: runtime.maxActiveMs,
|
|
1088
|
+
})
|
|
1089
|
+
: new OutboxLeaseLostError({ id: message.id }),
|
|
1090
|
+
message,
|
|
1091
|
+
operation: activeDeadlineEndsFirst
|
|
1092
|
+
? "maxActiveDuration"
|
|
1093
|
+
: "renewClaim",
|
|
1094
|
+
state: "lost",
|
|
1095
|
+
confirmedLost: !activeDeadlineEndsFirst,
|
|
1096
|
+
},
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
const finalController = new AbortController();
|
|
1100
|
+
const renewal = await runBoundedClaimRenewal({
|
|
1101
|
+
outbox: options.outbox,
|
|
1102
|
+
message,
|
|
1103
|
+
runtime,
|
|
1104
|
+
renewalAt,
|
|
1105
|
+
deadline: finalRenewalDeadline,
|
|
1106
|
+
signal: finalController.signal,
|
|
1107
|
+
});
|
|
1108
|
+
finalController.abort();
|
|
1109
|
+
if (renewal.kind === "succeeded") {
|
|
1110
|
+
return {
|
|
1111
|
+
lockedUntil: renewal.lockedUntil,
|
|
1112
|
+
renewalError: firstRenewalError,
|
|
1113
|
+
renewalRecovered: firstRenewalError === undefined ? undefined : true,
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
if (renewal.kind === "failed") {
|
|
1117
|
+
if (!(renewal.error instanceof OutboxClaimError)) {
|
|
1118
|
+
const failedAt = readOutboxNow(runtime.now);
|
|
1119
|
+
if (failedAt.getTime() < lockedUntil.getTime() &&
|
|
1120
|
+
failedAt.getTime() < activeUntil.getTime()) {
|
|
1121
|
+
return {
|
|
1122
|
+
lockedUntil,
|
|
1123
|
+
renewalError: renewal.error,
|
|
1124
|
+
renewalRecovered: false,
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
if (activeDeadlineEndsFirst &&
|
|
1128
|
+
failedAt.getTime() >= activeUntil.getTime()) {
|
|
1129
|
+
return {
|
|
1130
|
+
lockedUntil,
|
|
1131
|
+
failure: {
|
|
1132
|
+
error: new OutboxMaxActiveDurationError({
|
|
1133
|
+
id: message.id,
|
|
1134
|
+
maxActiveMs: runtime.maxActiveMs,
|
|
1135
|
+
}),
|
|
1136
|
+
message,
|
|
1137
|
+
operation: "maxActiveDuration",
|
|
1138
|
+
state: "lost",
|
|
1139
|
+
confirmedLost: false,
|
|
1140
|
+
},
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
return {
|
|
1145
|
+
lockedUntil,
|
|
1146
|
+
failure: {
|
|
1147
|
+
error: renewal.error,
|
|
1148
|
+
message,
|
|
1149
|
+
operation: "renewClaim",
|
|
1150
|
+
state: "lost",
|
|
1151
|
+
confirmedLost: renewal.error instanceof OutboxClaimError,
|
|
1152
|
+
},
|
|
1153
|
+
};
|
|
1154
|
+
}
|
|
1155
|
+
return {
|
|
1156
|
+
lockedUntil,
|
|
1157
|
+
failure: {
|
|
1158
|
+
error: renewal.kind === "waitFailed"
|
|
1159
|
+
? new OutboxLeaseLostError({
|
|
1160
|
+
id: message.id,
|
|
1161
|
+
message: activeDeadlineEndsFirst
|
|
1162
|
+
? `Could not enforce the maximum active duration for outbox message "${message.id}".`
|
|
1163
|
+
: `Could not enforce the final renewal deadline for outbox message "${message.id}".`,
|
|
1164
|
+
cause: renewal.error,
|
|
1165
|
+
})
|
|
1166
|
+
: activeDeadlineEndsFirst
|
|
1167
|
+
? new OutboxMaxActiveDurationError({
|
|
1168
|
+
id: message.id,
|
|
1169
|
+
maxActiveMs: runtime.maxActiveMs,
|
|
1170
|
+
})
|
|
1171
|
+
: new OutboxLeaseLostError({
|
|
1172
|
+
id: message.id,
|
|
1173
|
+
message: `Outbox claim renewal for message "${message.id}" did not complete before settlement.`,
|
|
1174
|
+
}),
|
|
1175
|
+
message,
|
|
1176
|
+
operation: activeDeadlineEndsFirst
|
|
1177
|
+
? "maxActiveDuration"
|
|
1178
|
+
: "renewClaim",
|
|
1179
|
+
state: "lost",
|
|
1180
|
+
confirmedLost: false,
|
|
1181
|
+
},
|
|
1182
|
+
};
|
|
1183
|
+
},
|
|
1184
|
+
};
|
|
1185
|
+
}
|
|
1186
|
+
async function settleClaim(options) {
|
|
1187
|
+
let lastError;
|
|
1188
|
+
for (let attempt = 1; attempt <= OUTBOX_SETTLEMENT_ATTEMPTS; attempt += 1) {
|
|
1189
|
+
const settlementAt = readOutboxNow(options.runtime.now);
|
|
1190
|
+
const settlementDeadline = new Date(Math.min(options.lockedUntil.getTime(), options.activeUntil.getTime()));
|
|
1191
|
+
const settlement = await runBoundedSettlement({
|
|
1192
|
+
runtime: options.runtime,
|
|
1193
|
+
settlementAt,
|
|
1194
|
+
deadline: settlementDeadline,
|
|
1195
|
+
settle: options.settle,
|
|
1196
|
+
});
|
|
1197
|
+
if (settlement.kind === "succeeded") {
|
|
1198
|
+
return { ok: true };
|
|
1199
|
+
}
|
|
1200
|
+
if (settlement.kind === "deadline") {
|
|
1201
|
+
return {
|
|
1202
|
+
ok: false,
|
|
1203
|
+
error: new OutboxLeaseLostError({
|
|
1204
|
+
id: options.message.id,
|
|
1205
|
+
message: `Outbox ${options.operation} for message "${options.message.id}" did not complete before its settlement deadline.`,
|
|
1206
|
+
}),
|
|
1207
|
+
claimLost: true,
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
if (settlement.kind === "waitFailed") {
|
|
1211
|
+
return { ok: false, error: settlement.error, claimLost: false };
|
|
1212
|
+
}
|
|
1213
|
+
const error = settlement.error;
|
|
1214
|
+
lastError = error;
|
|
1215
|
+
if (error instanceof OutboxClaimError) {
|
|
1216
|
+
return { ok: false, error, claimLost: true };
|
|
1217
|
+
}
|
|
1218
|
+
const remainingMs = settlementDeadline.getTime() -
|
|
1219
|
+
readOutboxNow(options.runtime.now).getTime();
|
|
1220
|
+
if (attempt >= OUTBOX_SETTLEMENT_ATTEMPTS || remainingMs <= 0)
|
|
1221
|
+
break;
|
|
1222
|
+
const waitController = new AbortController();
|
|
1223
|
+
try {
|
|
1224
|
+
await options.runtime.wait(Math.max(1, Math.min(OUTBOX_SETTLEMENT_RETRY_DELAY_MS * 2 ** (attempt - 1), remainingMs)), waitController.signal);
|
|
1225
|
+
}
|
|
1226
|
+
catch (error) {
|
|
1227
|
+
return { ok: false, error, claimLost: false };
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
return { ok: false, error: lastError, claimLost: false };
|
|
1231
|
+
}
|
|
1232
|
+
async function runBoundedSettlement(options) {
|
|
1233
|
+
const remainingMs = options.deadline.getTime() - options.settlementAt.getTime();
|
|
1234
|
+
if (remainingMs <= 0)
|
|
1235
|
+
return { kind: "deadline" };
|
|
1236
|
+
const waitController = new AbortController();
|
|
1237
|
+
const settlement = Promise.resolve()
|
|
1238
|
+
.then(() => options.settle(options.settlementAt))
|
|
1239
|
+
.then(() => ({ kind: "succeeded" }), (error) => ({ kind: "failed", error }));
|
|
1240
|
+
const deadline = Promise.resolve()
|
|
1241
|
+
.then(() => options.runtime.wait(remainingMs, waitController.signal))
|
|
1242
|
+
.then(() => ({ kind: "deadline" }), (error) => ({ kind: "waitFailed", error }));
|
|
1243
|
+
const result = await Promise.race([settlement, deadline]);
|
|
1244
|
+
waitController.abort();
|
|
1245
|
+
return result;
|
|
1246
|
+
}
|
|
1247
|
+
async function recordDeadLetter(options, instrumentation, jobInstrumentation, message, error, details = {}) {
|
|
1248
|
+
try {
|
|
1249
|
+
await options.onDeadLetter?.(error, message);
|
|
1250
|
+
}
|
|
1251
|
+
catch {
|
|
1252
|
+
// Dead-letter observers must not change the settled message state.
|
|
1253
|
+
}
|
|
1254
|
+
instrumentation.record({
|
|
1255
|
+
type: "outbox",
|
|
1256
|
+
...options.instrumentationContext,
|
|
1257
|
+
messageId: message.id,
|
|
1258
|
+
messageKind: message.kind,
|
|
1259
|
+
messageName: message.name,
|
|
1260
|
+
status: "deadLettered",
|
|
1261
|
+
details: outboxInstrumentationDetails(message, {
|
|
1262
|
+
...details,
|
|
1263
|
+
error: serializeOutboxError(error),
|
|
1264
|
+
}),
|
|
1265
|
+
});
|
|
1266
|
+
if (message.kind === "job") {
|
|
1267
|
+
jobInstrumentation.record({
|
|
1268
|
+
type: "job",
|
|
1269
|
+
...options.instrumentationContext,
|
|
1270
|
+
jobName: message.name,
|
|
1271
|
+
status: "deadLettered",
|
|
1272
|
+
details: outboxInstrumentationDetails(message, {
|
|
1273
|
+
...details,
|
|
1274
|
+
error: serializeOutboxError(error),
|
|
1275
|
+
}),
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
async function processClaimedMessage(options, runtime, instrumentation, jobInstrumentation, tracing, message) {
|
|
1280
|
+
const emptyOutcome = () => ({
|
|
1281
|
+
delivered: 0,
|
|
1282
|
+
retried: 0,
|
|
1283
|
+
deadLettered: 0,
|
|
1284
|
+
settlementFailed: 0,
|
|
1285
|
+
leaseLost: 0,
|
|
1286
|
+
});
|
|
1287
|
+
const outcome = emptyOutcome();
|
|
1288
|
+
const startedAt = readOutboxNow(runtime.now);
|
|
1289
|
+
const activeUntil = new Date(startedAt.getTime() + runtime.maxActiveMs);
|
|
1290
|
+
const heartbeat = createClaimHeartbeat(options, runtime, message, activeUntil);
|
|
1291
|
+
const lifetimeController = new AbortController();
|
|
1292
|
+
const parentTrace = parseTraceCarrier(message.trace);
|
|
1293
|
+
const traceAttributes = {
|
|
1294
|
+
"beignet.outbox.message_kind": message.kind,
|
|
1295
|
+
"beignet.outbox.message_name": message.name,
|
|
1296
|
+
};
|
|
1297
|
+
const delivery = Promise.resolve()
|
|
1298
|
+
.then(() => runWithTracing(tracing, {
|
|
1299
|
+
name: `beignet.outbox deliver ${message.name}`,
|
|
1300
|
+
type: "outbox",
|
|
1301
|
+
kind: "consumer",
|
|
1302
|
+
parent: parentTrace,
|
|
1303
|
+
attributes: traceAttributes,
|
|
1304
|
+
metricAttributes: traceAttributes,
|
|
1305
|
+
}, (span) => deliverOutboxMessage(options, message, captureTraceCarrier(span?.context ?? parentTrace))))
|
|
1306
|
+
.then(() => ({ kind: "succeeded" }), (error) => ({ kind: "failed", error }));
|
|
1307
|
+
const maximumActive = Promise.resolve()
|
|
1308
|
+
.then(() => runtime.wait(runtime.maxActiveMs, lifetimeController.signal))
|
|
1309
|
+
.then(() => ({ kind: "maxActive" }), (error) => ({ kind: "waitFailed", error }));
|
|
1310
|
+
const leaseLost = heartbeat.lost.then((failure) => ({
|
|
1311
|
+
kind: "leaseLost",
|
|
1312
|
+
failure,
|
|
1313
|
+
}));
|
|
1314
|
+
const deliveryOutcome = await Promise.race([
|
|
1315
|
+
delivery,
|
|
1316
|
+
maximumActive,
|
|
1317
|
+
leaseLost,
|
|
1318
|
+
]);
|
|
1319
|
+
lifetimeController.abort();
|
|
1320
|
+
if (deliveryOutcome.kind === "maxActive") {
|
|
1321
|
+
await heartbeat.stop();
|
|
1322
|
+
const failure = {
|
|
1323
|
+
error: new OutboxMaxActiveDurationError({
|
|
1324
|
+
id: message.id,
|
|
1325
|
+
maxActiveMs: runtime.maxActiveMs,
|
|
1326
|
+
}),
|
|
1327
|
+
message,
|
|
1328
|
+
operation: "maxActiveDuration",
|
|
1329
|
+
state: "lost",
|
|
1330
|
+
confirmedLost: false,
|
|
1331
|
+
};
|
|
1332
|
+
await notifyLeaseFailure(options, failure);
|
|
1333
|
+
instrumentation.custom({
|
|
1334
|
+
name: "outbox.lease.lost",
|
|
1335
|
+
label: "Outbox claim no longer confirmed",
|
|
1336
|
+
summary: `Stopped renewing ${message.kind} "${message.name}" after its active-delivery limit`,
|
|
1337
|
+
details: outboxInstrumentationDetails(message, {
|
|
1338
|
+
operation: failure.operation,
|
|
1339
|
+
state: failure.state,
|
|
1340
|
+
error: serializeOutboxError(failure.error),
|
|
1341
|
+
}),
|
|
1342
|
+
});
|
|
1343
|
+
outcome.leaseLost = 1;
|
|
1344
|
+
return outcome;
|
|
1345
|
+
}
|
|
1346
|
+
if (deliveryOutcome.kind === "waitFailed") {
|
|
1347
|
+
await heartbeat.stop();
|
|
1348
|
+
const failure = {
|
|
1349
|
+
error: new OutboxLeaseLostError({
|
|
1350
|
+
id: message.id,
|
|
1351
|
+
message: `Could not enforce the active-delivery limit for outbox message "${message.id}".`,
|
|
1352
|
+
cause: deliveryOutcome.error,
|
|
1353
|
+
}),
|
|
1354
|
+
message,
|
|
1355
|
+
operation: "maxActiveDuration",
|
|
1356
|
+
state: "lost",
|
|
1357
|
+
confirmedLost: false,
|
|
1358
|
+
};
|
|
1359
|
+
await notifyLeaseFailure(options, failure);
|
|
1360
|
+
instrumentation.custom({
|
|
1361
|
+
name: "outbox.lease.lost",
|
|
1362
|
+
label: "Outbox claim no longer confirmed",
|
|
1363
|
+
summary: `Could not enforce the active-delivery limit for ${message.kind} "${message.name}"`,
|
|
1364
|
+
details: outboxInstrumentationDetails(message, {
|
|
1365
|
+
operation: failure.operation,
|
|
1366
|
+
state: failure.state,
|
|
1367
|
+
error: serializeOutboxError(failure.error),
|
|
1368
|
+
}),
|
|
1369
|
+
});
|
|
1370
|
+
outcome.leaseLost = 1;
|
|
1371
|
+
return outcome;
|
|
1372
|
+
}
|
|
1373
|
+
if (deliveryOutcome.kind === "leaseLost") {
|
|
1374
|
+
await heartbeat.stop();
|
|
1375
|
+
await notifyLeaseFailure(options, deliveryOutcome.failure);
|
|
1376
|
+
instrumentation.custom({
|
|
1377
|
+
name: "outbox.lease.lost",
|
|
1378
|
+
label: "Outbox claim lost",
|
|
1379
|
+
summary: `Could not keep the claim for ${message.kind} "${message.name}" active`,
|
|
1380
|
+
details: outboxInstrumentationDetails(message, {
|
|
1381
|
+
operation: deliveryOutcome.failure.operation,
|
|
1382
|
+
state: deliveryOutcome.failure.state,
|
|
1383
|
+
confirmedLost: deliveryOutcome.failure.confirmedLost,
|
|
1384
|
+
error: serializeOutboxError(deliveryOutcome.failure.error),
|
|
1385
|
+
}),
|
|
1386
|
+
});
|
|
1387
|
+
outcome.leaseLost = 1;
|
|
1388
|
+
return outcome;
|
|
1389
|
+
}
|
|
1390
|
+
const lease = await heartbeat.stopAndExtend();
|
|
1391
|
+
if (lease.failure) {
|
|
1392
|
+
await notifyLeaseFailure(options, lease.failure);
|
|
1393
|
+
instrumentation.custom({
|
|
1394
|
+
name: "outbox.lease.lost",
|
|
1395
|
+
label: "Outbox claim lost",
|
|
1396
|
+
summary: `Could not confirm the claim for ${message.kind} "${message.name}" before settlement`,
|
|
1397
|
+
details: outboxInstrumentationDetails(message, {
|
|
1398
|
+
operation: lease.failure.operation,
|
|
1399
|
+
state: lease.failure.state,
|
|
1400
|
+
confirmedLost: lease.failure.confirmedLost,
|
|
1401
|
+
error: serializeOutboxError(lease.failure.error),
|
|
1402
|
+
}),
|
|
1403
|
+
});
|
|
1404
|
+
outcome.leaseLost = 1;
|
|
1405
|
+
return outcome;
|
|
1406
|
+
}
|
|
1407
|
+
const reportRenewalOutcome = async () => {
|
|
1408
|
+
if (lease.renewalError === undefined)
|
|
1409
|
+
return;
|
|
1410
|
+
const failure = {
|
|
1411
|
+
error: lease.renewalError,
|
|
1412
|
+
message,
|
|
1413
|
+
operation: "renewClaim",
|
|
1414
|
+
state: lease.renewalRecovered ? "recovered" : "degraded",
|
|
1415
|
+
confirmedLost: false,
|
|
1416
|
+
};
|
|
1417
|
+
await notifyLeaseFailure(options, failure);
|
|
1418
|
+
instrumentation.custom({
|
|
1419
|
+
name: lease.renewalRecovered
|
|
1420
|
+
? "outbox.lease.renewal.recovered"
|
|
1421
|
+
: "outbox.lease.renewal.degraded",
|
|
1422
|
+
label: lease.renewalRecovered
|
|
1423
|
+
? "Outbox claim renewal recovered"
|
|
1424
|
+
: "Outbox claim renewal degraded",
|
|
1425
|
+
summary: lease.renewalRecovered
|
|
1426
|
+
? `Recovered claim renewal for ${message.kind} "${message.name}"`
|
|
1427
|
+
: `Continued ${message.kind} "${message.name}" settlement under its last confirmed lease`,
|
|
1428
|
+
details: outboxInstrumentationDetails(message, {
|
|
1429
|
+
state: failure.state,
|
|
1430
|
+
error: serializeOutboxError(lease.renewalError),
|
|
1431
|
+
}),
|
|
1432
|
+
});
|
|
1433
|
+
};
|
|
1434
|
+
if (deliveryOutcome.kind === "succeeded") {
|
|
1435
|
+
const settlement = await settleClaim({
|
|
1436
|
+
operation: "markDelivered",
|
|
1437
|
+
message,
|
|
1438
|
+
lockedUntil: lease.lockedUntil,
|
|
1439
|
+
activeUntil,
|
|
1440
|
+
runtime,
|
|
1441
|
+
settle: (now) => options.outbox.markDelivered({
|
|
1442
|
+
id: message.id,
|
|
1443
|
+
claimToken: message.claimToken,
|
|
1444
|
+
now,
|
|
1445
|
+
}),
|
|
1446
|
+
});
|
|
1447
|
+
await reportRenewalOutcome();
|
|
1448
|
+
if (!settlement.ok) {
|
|
1449
|
+
const failure = {
|
|
1450
|
+
error: settlement.error,
|
|
1451
|
+
message,
|
|
1452
|
+
operation: "markDelivered",
|
|
1453
|
+
deliverySucceeded: true,
|
|
1454
|
+
};
|
|
1455
|
+
await notifySettlementFailure(options, failure);
|
|
1456
|
+
instrumentation.custom({
|
|
1457
|
+
name: "outbox.settlement.failed",
|
|
1458
|
+
label: "Outbox settlement failed",
|
|
1459
|
+
summary: `Delivered ${message.kind} "${message.name}", but could not confirm its durable acknowledgement`,
|
|
1460
|
+
details: outboxInstrumentationDetails(message, {
|
|
1461
|
+
operation: failure.operation,
|
|
1462
|
+
deliverySucceeded: true,
|
|
1463
|
+
settlementError: serializeOutboxError(settlement.error),
|
|
1464
|
+
}),
|
|
1465
|
+
});
|
|
1466
|
+
outcome.settlementFailed = 1;
|
|
1467
|
+
if (settlement.claimLost)
|
|
1468
|
+
outcome.leaseLost = 1;
|
|
1469
|
+
return outcome;
|
|
1470
|
+
}
|
|
1471
|
+
instrumentation.record({
|
|
1472
|
+
type: "outbox",
|
|
1473
|
+
...options.instrumentationContext,
|
|
1474
|
+
messageId: message.id,
|
|
1475
|
+
messageKind: message.kind,
|
|
1476
|
+
messageName: message.name,
|
|
1477
|
+
status: "delivered",
|
|
1478
|
+
details: outboxInstrumentationDetails(message),
|
|
1479
|
+
});
|
|
1480
|
+
outcome.delivered = 1;
|
|
1481
|
+
return outcome;
|
|
1482
|
+
}
|
|
1483
|
+
const deliveryError = deliveryOutcome.error;
|
|
1484
|
+
const failedAt = readOutboxNow(runtime.now);
|
|
1485
|
+
const shouldRetry = shouldRetryOutboxMessage(options, message, deliveryError);
|
|
1486
|
+
const deadLetter = !shouldRetry;
|
|
1487
|
+
const retryDelayMs = deadLetter
|
|
1488
|
+
? 0
|
|
1489
|
+
: resolveRetryDelayMs(options, message, deliveryError, failedAt);
|
|
1490
|
+
const retryAt = deadLetter
|
|
1491
|
+
? undefined
|
|
1492
|
+
: new Date(failedAt.getTime() + retryDelayMs);
|
|
1493
|
+
const settlement = await settleClaim({
|
|
1494
|
+
operation: "markFailed",
|
|
1495
|
+
message,
|
|
1496
|
+
lockedUntil: lease.lockedUntil,
|
|
1497
|
+
activeUntil,
|
|
1498
|
+
runtime,
|
|
1499
|
+
settle: (now) => options.outbox.markFailed({
|
|
1500
|
+
id: message.id,
|
|
1501
|
+
claimToken: message.claimToken,
|
|
1502
|
+
error: deliveryError,
|
|
1503
|
+
deadLetter,
|
|
1504
|
+
now,
|
|
1505
|
+
retryAt,
|
|
1506
|
+
}),
|
|
1507
|
+
});
|
|
1508
|
+
await reportRenewalOutcome();
|
|
1509
|
+
try {
|
|
1510
|
+
await options.onError?.(deliveryError, message);
|
|
1511
|
+
}
|
|
1512
|
+
catch {
|
|
1513
|
+
// Delivery observers cannot change retry, dead-letter, or recovery state.
|
|
1514
|
+
}
|
|
1515
|
+
if (!settlement.ok) {
|
|
1516
|
+
const failure = {
|
|
1517
|
+
error: settlement.error,
|
|
1518
|
+
message,
|
|
1519
|
+
operation: "markFailed",
|
|
1520
|
+
deliverySucceeded: false,
|
|
1521
|
+
deliveryError,
|
|
1522
|
+
};
|
|
1523
|
+
await notifySettlementFailure(options, failure);
|
|
1524
|
+
instrumentation.custom({
|
|
1525
|
+
name: "outbox.settlement.failed",
|
|
1526
|
+
label: "Outbox settlement failed",
|
|
1527
|
+
summary: `Could not settle failed ${message.kind} "${message.name}"`,
|
|
1528
|
+
details: outboxInstrumentationDetails(message, {
|
|
1529
|
+
operation: failure.operation,
|
|
1530
|
+
deliverySucceeded: false,
|
|
1531
|
+
deliveryError: serializeOutboxError(deliveryError),
|
|
1532
|
+
settlementError: serializeOutboxError(settlement.error),
|
|
1533
|
+
}),
|
|
1534
|
+
});
|
|
1535
|
+
outcome.settlementFailed = 1;
|
|
1536
|
+
if (settlement.claimLost)
|
|
1537
|
+
outcome.leaseLost = 1;
|
|
1538
|
+
return outcome;
|
|
1539
|
+
}
|
|
1540
|
+
if (deadLetter) {
|
|
1541
|
+
await recordDeadLetter(options, instrumentation, jobInstrumentation, message, deliveryError);
|
|
1542
|
+
outcome.deadLettered = 1;
|
|
1543
|
+
return outcome;
|
|
1544
|
+
}
|
|
1545
|
+
instrumentation.record({
|
|
1546
|
+
type: "outbox",
|
|
1547
|
+
...options.instrumentationContext,
|
|
1548
|
+
messageId: message.id,
|
|
1549
|
+
messageKind: message.kind,
|
|
1550
|
+
messageName: message.name,
|
|
1551
|
+
status: "retryScheduled",
|
|
1552
|
+
details: outboxInstrumentationDetails(message, {
|
|
1553
|
+
retryDelayMs,
|
|
1554
|
+
retryAt: retryAt?.toISOString(),
|
|
1555
|
+
error: serializeOutboxError(deliveryError),
|
|
1556
|
+
}),
|
|
1557
|
+
});
|
|
1558
|
+
if (message.kind === "job") {
|
|
1559
|
+
jobInstrumentation.record({
|
|
1560
|
+
type: "job",
|
|
1561
|
+
...options.instrumentationContext,
|
|
1562
|
+
jobName: message.name,
|
|
1563
|
+
status: "retryScheduled",
|
|
1564
|
+
details: outboxInstrumentationDetails(message, {
|
|
1565
|
+
retryDelayMs,
|
|
1566
|
+
retryAt: retryAt?.toISOString(),
|
|
1567
|
+
error: serializeOutboxError(deliveryError),
|
|
1568
|
+
}),
|
|
1569
|
+
});
|
|
1570
|
+
}
|
|
1571
|
+
outcome.retried = 1;
|
|
1572
|
+
return outcome;
|
|
1573
|
+
}
|
|
643
1574
|
/**
|
|
644
|
-
* Claim and deliver one
|
|
1575
|
+
* Claim and deliver one bounded set of outbox messages.
|
|
645
1576
|
*
|
|
646
|
-
*
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
*
|
|
650
|
-
* then dead-lettered.
|
|
1577
|
+
* The drain claims only enough messages to fill active delivery slots and
|
|
1578
|
+
* renews each active claim until delivery settles or reaches its configured
|
|
1579
|
+
* maximum duration. It remains an at-least-once transport: a process can still
|
|
1580
|
+
* terminate after the external effect succeeds but before acknowledgement.
|
|
651
1581
|
*/
|
|
652
1582
|
export async function drainOutbox(options) {
|
|
653
|
-
const
|
|
654
|
-
|
|
1583
|
+
const runtime = resolveDrainRuntime(options);
|
|
1584
|
+
assertOutboxDrainPort(options.outbox);
|
|
655
1585
|
assertOutboxDeliveryCapabilities(options);
|
|
656
1586
|
const instrumentation = createProviderInstrumentation(options.instrumentation, {
|
|
657
1587
|
providerName: "outbox",
|
|
@@ -662,157 +1592,81 @@ export async function drainOutbox(options) {
|
|
|
662
1592
|
watcher: "jobs",
|
|
663
1593
|
});
|
|
664
1594
|
const tracing = resolveTracingPort(options.instrumentation);
|
|
665
|
-
const now = options.now ?? new Date();
|
|
666
|
-
const messages = await options.outbox.claimBatch({
|
|
667
|
-
limit: batchSize,
|
|
668
|
-
now,
|
|
669
|
-
leaseMs: options.leaseMs,
|
|
670
|
-
});
|
|
671
1595
|
const result = {
|
|
672
|
-
claimed:
|
|
1596
|
+
claimed: 0,
|
|
673
1597
|
delivered: 0,
|
|
674
1598
|
retried: 0,
|
|
675
1599
|
deadLettered: 0,
|
|
1600
|
+
abandonedDeadLettered: 0,
|
|
1601
|
+
settlementFailed: 0,
|
|
1602
|
+
leaseLost: 0,
|
|
676
1603
|
};
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
await
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
parent: parentTrace,
|
|
689
|
-
attributes: traceAttributes,
|
|
690
|
-
metricAttributes: traceAttributes,
|
|
691
|
-
}, (span) => deliverOutboxMessage(options, message, captureTraceCarrier(span?.context ?? parentTrace)));
|
|
692
|
-
await options.outbox.markDelivered({
|
|
693
|
-
id: message.id,
|
|
694
|
-
claimToken: message.claimToken,
|
|
695
|
-
now,
|
|
1604
|
+
let remaining = runtime.batchSize;
|
|
1605
|
+
let sourceExhausted = false;
|
|
1606
|
+
while (remaining > 0 && !sourceExhausted) {
|
|
1607
|
+
const active = [];
|
|
1608
|
+
const abandoned = [];
|
|
1609
|
+
while (active.length < runtime.concurrency && remaining > 0) {
|
|
1610
|
+
const limit = Math.min(runtime.concurrency - active.length, remaining);
|
|
1611
|
+
const selected = await options.outbox.claimBatch({
|
|
1612
|
+
limit,
|
|
1613
|
+
now: readOutboxNow(runtime.now),
|
|
1614
|
+
leaseMs: runtime.leaseMs,
|
|
696
1615
|
});
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
messageKind: message.kind,
|
|
702
|
-
messageName: message.name,
|
|
703
|
-
status: "delivered",
|
|
704
|
-
details: outboxInstrumentationDetails(message),
|
|
705
|
-
});
|
|
706
|
-
result.delivered += 1;
|
|
707
|
-
}
|
|
708
|
-
catch (error) {
|
|
709
|
-
try {
|
|
710
|
-
await options.onError?.(error, message);
|
|
1616
|
+
const selectedCount = selected.claimed.length + selected.deadLettered.length;
|
|
1617
|
+
if (selectedCount === 0) {
|
|
1618
|
+
sourceExhausted = true;
|
|
1619
|
+
break;
|
|
711
1620
|
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
// dead-lettered even if the observer fails.
|
|
715
|
-
}
|
|
716
|
-
const shouldRetry = shouldRetryOutboxMessage(options, message, error);
|
|
717
|
-
const deadLetter = !shouldRetry;
|
|
718
|
-
const retryDelayMs = deadLetter
|
|
719
|
-
? 0
|
|
720
|
-
: resolveRetryDelayMs(options, message, error, now);
|
|
721
|
-
try {
|
|
722
|
-
await options.outbox.markFailed({
|
|
723
|
-
id: message.id,
|
|
724
|
-
claimToken: message.claimToken,
|
|
725
|
-
error,
|
|
726
|
-
deadLetter,
|
|
727
|
-
now,
|
|
728
|
-
retryAt: deadLetter
|
|
729
|
-
? undefined
|
|
730
|
-
: new Date(now.getTime() + retryDelayMs),
|
|
731
|
-
});
|
|
732
|
-
}
|
|
733
|
-
catch (settlementError) {
|
|
734
|
-
try {
|
|
735
|
-
await options.onSettlementError?.(settlementError, message, error);
|
|
736
|
-
}
|
|
737
|
-
catch {
|
|
738
|
-
// Preserve the settlement failure when its observer also fails.
|
|
739
|
-
}
|
|
740
|
-
instrumentation.custom({
|
|
741
|
-
name: "outbox.settlement.failed",
|
|
742
|
-
label: "Outbox settlement failed",
|
|
743
|
-
summary: `Could not settle failed ${message.kind} "${message.name}"`,
|
|
744
|
-
details: outboxInstrumentationDetails(message, {
|
|
745
|
-
deliveryError: serializeOutboxError(error),
|
|
746
|
-
settlementError: serializeOutboxError(settlementError),
|
|
747
|
-
}),
|
|
748
|
-
});
|
|
749
|
-
continue;
|
|
750
|
-
}
|
|
751
|
-
if (deadLetter) {
|
|
752
|
-
try {
|
|
753
|
-
await options.onDeadLetter?.(error, message);
|
|
754
|
-
}
|
|
755
|
-
catch {
|
|
756
|
-
// Dead-letter observers must not change the settled message state.
|
|
757
|
-
}
|
|
758
|
-
instrumentation.record({
|
|
759
|
-
type: "outbox",
|
|
760
|
-
...options.instrumentationContext,
|
|
761
|
-
messageId: message.id,
|
|
762
|
-
messageKind: message.kind,
|
|
763
|
-
messageName: message.name,
|
|
764
|
-
status: "deadLettered",
|
|
765
|
-
details: outboxInstrumentationDetails(message, {
|
|
766
|
-
error: serializeOutboxError(error),
|
|
767
|
-
}),
|
|
768
|
-
});
|
|
769
|
-
if (message.kind === "job") {
|
|
770
|
-
jobInstrumentation.record({
|
|
771
|
-
type: "job",
|
|
772
|
-
...options.instrumentationContext,
|
|
773
|
-
jobName: message.name,
|
|
774
|
-
status: "deadLettered",
|
|
775
|
-
details: outboxInstrumentationDetails(message, {
|
|
776
|
-
error: serializeOutboxError(error),
|
|
777
|
-
}),
|
|
778
|
-
});
|
|
779
|
-
}
|
|
780
|
-
result.deadLettered += 1;
|
|
781
|
-
}
|
|
782
|
-
else {
|
|
783
|
-
const retryAt = new Date(now.getTime() + retryDelayMs).toISOString();
|
|
784
|
-
instrumentation.record({
|
|
785
|
-
type: "outbox",
|
|
786
|
-
...options.instrumentationContext,
|
|
787
|
-
messageId: message.id,
|
|
788
|
-
messageKind: message.kind,
|
|
789
|
-
messageName: message.name,
|
|
790
|
-
status: "retryScheduled",
|
|
791
|
-
details: outboxInstrumentationDetails(message, {
|
|
792
|
-
retryDelayMs,
|
|
793
|
-
retryAt,
|
|
794
|
-
error: serializeOutboxError(error),
|
|
795
|
-
}),
|
|
796
|
-
});
|
|
797
|
-
if (message.kind === "job") {
|
|
798
|
-
jobInstrumentation.record({
|
|
799
|
-
type: "job",
|
|
800
|
-
...options.instrumentationContext,
|
|
801
|
-
jobName: message.name,
|
|
802
|
-
status: "retryScheduled",
|
|
803
|
-
details: outboxInstrumentationDetails(message, {
|
|
804
|
-
retryDelayMs,
|
|
805
|
-
retryAt,
|
|
806
|
-
error: serializeOutboxError(error),
|
|
807
|
-
}),
|
|
808
|
-
});
|
|
809
|
-
}
|
|
810
|
-
result.retried += 1;
|
|
1621
|
+
if (selectedCount > limit) {
|
|
1622
|
+
throw new Error(`Outbox claimBatch returned ${selectedCount} messages for a limit of ${limit}.`);
|
|
811
1623
|
}
|
|
1624
|
+
remaining -= selectedCount;
|
|
1625
|
+
result.claimed += selected.claimed.length;
|
|
1626
|
+
active.push(...selected.claimed);
|
|
1627
|
+
abandoned.push(...selected.deadLettered);
|
|
1628
|
+
}
|
|
1629
|
+
// Construct active delivery promises first so their heartbeats protect
|
|
1630
|
+
// freshly claimed rows while abandoned-message observers run.
|
|
1631
|
+
const outcomePromises = active.map((message) => processClaimedMessage(options, runtime, instrumentation, jobInstrumentation, tracing, message));
|
|
1632
|
+
const abandonedPromises = abandoned.map(async (message) => {
|
|
1633
|
+
const error = new OutboxAbandonedClaimError({
|
|
1634
|
+
id: message.id,
|
|
1635
|
+
attempts: message.attempts,
|
|
1636
|
+
maxAttempts: message.maxAttempts,
|
|
1637
|
+
});
|
|
1638
|
+
await recordDeadLetter(options, instrumentation, jobInstrumentation, message, error, { abandoned: true });
|
|
1639
|
+
});
|
|
1640
|
+
const [outcomes] = await Promise.all([
|
|
1641
|
+
Promise.all(outcomePromises),
|
|
1642
|
+
Promise.all(abandonedPromises),
|
|
1643
|
+
]);
|
|
1644
|
+
result.deadLettered += abandoned.length;
|
|
1645
|
+
result.abandonedDeadLettered += abandoned.length;
|
|
1646
|
+
for (const outcome of outcomes) {
|
|
1647
|
+
result.delivered += outcome.delivered;
|
|
1648
|
+
result.retried += outcome.retried;
|
|
1649
|
+
result.deadLettered += outcome.deadLettered;
|
|
1650
|
+
result.settlementFailed += outcome.settlementFailed;
|
|
1651
|
+
result.leaseLost += outcome.leaseLost;
|
|
812
1652
|
}
|
|
813
1653
|
}
|
|
814
1654
|
return result;
|
|
815
1655
|
}
|
|
1656
|
+
function assertOutboxDrainPort(outbox) {
|
|
1657
|
+
const candidate = outbox;
|
|
1658
|
+
const missing = [
|
|
1659
|
+
"claimBatch",
|
|
1660
|
+
"renewClaim",
|
|
1661
|
+
"markDelivered",
|
|
1662
|
+
"markFailed",
|
|
1663
|
+
].filter((method) => typeof candidate[method] !== "function");
|
|
1664
|
+
if (missing.length > 0) {
|
|
1665
|
+
throw new Error(`Cannot drain this outbox: the outbox port is missing ${missing
|
|
1666
|
+
.map((method) => `${method}()`)
|
|
1667
|
+
.join(", ")}.`);
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
816
1670
|
function assertOutboxDeliveryCapabilities(options) {
|
|
817
1671
|
const missing = [];
|
|
818
1672
|
if (options.registry.events.size > 0 && !options.eventBus) {
|