@nathapp/nestjs-outbox 3.3.0 → 4.0.1
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/package.json +11 -11
- package/.eslintrc.js +0 -8
- package/.nax/features/nestjs-outbox/.nax-acceptance.test.ts +0 -878
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -15
- package/lib/constant/index.ts +0 -1
- package/lib/constant/outbox.constants.ts +0 -3
- package/lib/exception/index.ts +0 -3
- package/lib/exception/outbox-exception-code.enum.ts +0 -4
- package/lib/exception/outbox-invalid-event.exception.ts +0 -9
- package/lib/exception/outbox-misconfigured.exception.ts +0 -9
- package/lib/index.ts +0 -6
- package/lib/interface/index.ts +0 -18
- package/lib/interface/outbox-module-options.interface.ts +0 -18
- package/lib/interface/outbox-publisher.interface.ts +0 -5
- package/lib/interface/outbox-record.interface.ts +0 -23
- package/lib/interface/outbox-status.enum.ts +0 -6
- package/lib/interface/outbox-store.interface.ts +0 -23
- package/lib/module/index.ts +0 -1
- package/lib/module/outbox.module.ts +0 -64
- package/lib/provider/in-memory-outbox-store.ts +0 -114
- package/lib/provider/index.ts +0 -4
- package/lib/provider/outbox-relay.ts +0 -147
- package/lib/provider/outbox.service.ts +0 -70
- package/lib/provider/resolve-outbox-options.ts +0 -51
- package/lib/util/compute-backoff.ts +0 -4
- package/lib/util/index.ts +0 -1
- package/test/barrel/index.spec.ts +0 -45
- package/test/constant/outbox.constants.spec.ts +0 -20
- package/test/e2e/outbox.module.spec.ts +0 -371
- package/test/exception/outbox.exceptions.spec.ts +0 -38
- package/test/interface/outbox-status.enum.spec.ts +0 -19
- package/test/jest-nax-setup.js +0 -31
- package/test/jest-nax.json +0 -30
- package/test/jest-test.json +0 -28
- package/test/module/outbox.module.spec.ts +0 -139
- package/test/provider/in-memory-outbox-store.spec.ts +0 -216
- package/test/provider/outbox-relay.spec.ts +0 -565
- package/test/provider/outbox-service.spec.ts +0 -229
- package/test/provider/resolve-outbox-options.spec.ts +0 -50
- package/test/util/compute-backoff.spec.ts +0 -25
- package/tsconfig.json +0 -9
package/.turbo/turbo-build.log
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# @nathapp/nestjs-outbox
|
|
2
|
-
|
|
3
|
-
## 3.3.0
|
|
4
|
-
|
|
5
|
-
### Minor Changes
|
|
6
|
-
|
|
7
|
-
- a27440f: 3.3.0 milestone release. The `@nathapp/nestjs-*` packages now version and publish in lockstep at a unified version.
|
|
8
|
-
|
|
9
|
-
- **nestjs-csrf**: first public release — stateless double-submit-cookie CSRF guard (zero-dep HMAC, Express + Fastify, Bearer coexist skip, `@SkipCsrf()`, auto-binds `APP_GUARD`).
|
|
10
|
-
- **nestjs-health**: first public release — terminus aggregator over cache/prisma/queue with k8s liveness/readiness probes via a dynamic controller factory.
|
|
11
|
-
- **nestjs-idempotency**: first public release — `@Idempotent()` interceptor (zero-dep sha256 fingerprint, Express + Fastify, 409-in-flight, 2xx replay, fingerprint mismatch 422, fail-closed on store error).
|
|
12
|
-
- **nestjs-observability**: first public release — Prometheus RED metrics + OpenTelemetry tracing joined to the logging ALS; `initTracing()` pre-bootstrap hook.
|
|
13
|
-
- **nestjs-outbox**: first public release — transactional outbox relay (at-least-once via `ITransactionManager`, lease-claim + backoff, dead-letter).
|
|
14
|
-
- **nestjs-storage**: migrated from aws-sdk v2 to v3 (zero runtime dep via `import type` + lazy `loadPackage`); NFSStorage stub replaced with an S3-style HTTP file-service client.
|
|
15
|
-
- **nestjs-cache**: `delByPattern` now routes through Redis SCAN instead of an O(n) key sweep, with glob-detection and multi-tier iterator support.
|
package/lib/constant/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './outbox.constants';
|
package/lib/exception/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { HttpStatus } from '@nestjs/common';
|
|
2
|
-
import { AppException } from '@nathapp/nestjs-common';
|
|
3
|
-
import { OutboxExceptionCode } from './outbox-exception-code.enum';
|
|
4
|
-
|
|
5
|
-
export class OutboxInvalidEventException extends AppException {
|
|
6
|
-
constructor() {
|
|
7
|
-
super(OutboxExceptionCode.INVALID_EVENT_TYPE, undefined, undefined, HttpStatus.BAD_REQUEST);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { HttpStatus } from '@nestjs/common';
|
|
2
|
-
import { AppException } from '@nathapp/nestjs-common';
|
|
3
|
-
import { OutboxExceptionCode } from './outbox-exception-code.enum';
|
|
4
|
-
|
|
5
|
-
export class OutboxMisconfiguredException extends AppException {
|
|
6
|
-
constructor() {
|
|
7
|
-
super(OutboxExceptionCode.PUBLISHER_NOT_CONFIGURED, undefined, undefined, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
8
|
-
}
|
|
9
|
-
}
|
package/lib/index.ts
DELETED
package/lib/interface/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { OutboxRecord, OutboxEventInput } from './outbox-record.interface';
|
|
2
|
-
import { OutboxStatus } from './outbox-status.enum';
|
|
3
|
-
import { IOutboxStore } from './outbox-store.interface';
|
|
4
|
-
import { IOutboxPublisher } from './outbox-publisher.interface';
|
|
5
|
-
import {
|
|
6
|
-
OutboxRelayOptions,
|
|
7
|
-
OutboxModuleOptions,
|
|
8
|
-
} from './outbox-module-options.interface';
|
|
9
|
-
|
|
10
|
-
export {
|
|
11
|
-
OutboxStatus,
|
|
12
|
-
OutboxRecord,
|
|
13
|
-
OutboxEventInput,
|
|
14
|
-
IOutboxStore,
|
|
15
|
-
IOutboxPublisher,
|
|
16
|
-
OutboxRelayOptions,
|
|
17
|
-
OutboxModuleOptions,
|
|
18
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { IOutboxStore } from './outbox-store.interface';
|
|
2
|
-
import { IOutboxPublisher } from './outbox-publisher.interface';
|
|
3
|
-
|
|
4
|
-
export interface OutboxRelayOptions {
|
|
5
|
-
enabled?: boolean;
|
|
6
|
-
pollIntervalMs?: number;
|
|
7
|
-
batchSize?: number;
|
|
8
|
-
leaseMs?: number;
|
|
9
|
-
maxAttempts?: number;
|
|
10
|
-
backoffBaseMs?: number;
|
|
11
|
-
backoffCapMs?: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface OutboxModuleOptions {
|
|
15
|
-
store?: IOutboxStore;
|
|
16
|
-
publisher?: IOutboxPublisher;
|
|
17
|
-
relay?: OutboxRelayOptions;
|
|
18
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { OutboxStatus } from './outbox-status.enum';
|
|
2
|
-
|
|
3
|
-
export interface OutboxRecord {
|
|
4
|
-
id: string;
|
|
5
|
-
type: string;
|
|
6
|
-
payload: unknown;
|
|
7
|
-
status: OutboxStatus;
|
|
8
|
-
attempts: number;
|
|
9
|
-
createdAt: Date;
|
|
10
|
-
nextAttemptAt: Date;
|
|
11
|
-
leaseUntil?: Date;
|
|
12
|
-
owner?: string;
|
|
13
|
-
publishedAt?: Date;
|
|
14
|
-
headers?: Record<string, string>;
|
|
15
|
-
metadata?: Record<string, unknown>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface OutboxEventInput {
|
|
19
|
-
type: string;
|
|
20
|
-
payload: unknown;
|
|
21
|
-
headers?: Record<string, string>;
|
|
22
|
-
metadata?: Record<string, unknown>;
|
|
23
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { OutboxRecord } from './outbox-record.interface';
|
|
2
|
-
|
|
3
|
-
export interface IOutboxStore {
|
|
4
|
-
save(record: OutboxRecord, client: unknown): Promise<void>;
|
|
5
|
-
|
|
6
|
-
claimBatch(
|
|
7
|
-
limit: number,
|
|
8
|
-
leaseMs: number,
|
|
9
|
-
now: Date,
|
|
10
|
-
owner: string,
|
|
11
|
-
): Promise<OutboxRecord[]>;
|
|
12
|
-
|
|
13
|
-
markPublished(id: string, publishedAt: Date, owner: string): Promise<void>;
|
|
14
|
-
|
|
15
|
-
markRetry(
|
|
16
|
-
id: string,
|
|
17
|
-
attempts: number,
|
|
18
|
-
nextAttemptAt: Date,
|
|
19
|
-
owner: string,
|
|
20
|
-
): Promise<void>;
|
|
21
|
-
|
|
22
|
-
markDead(id: string, attempts: number, owner: string): Promise<void>;
|
|
23
|
-
}
|
package/lib/module/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './outbox.module';
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { DynamicModule, Module, Provider } from '@nestjs/common';
|
|
2
|
-
import { ConfigurableModuleClass } from '@nathapp/nestjs-common';
|
|
3
|
-
import { OutboxModuleOptions } from '../interface/outbox-module-options.interface';
|
|
4
|
-
import { OutboxRelay } from '../provider/outbox-relay';
|
|
5
|
-
import { OutboxService } from '../provider/outbox.service';
|
|
6
|
-
import { resolveOutboxOptions, ResolvedOutboxOptions } from '../provider/resolve-outbox-options';
|
|
7
|
-
import {
|
|
8
|
-
OUTBOX_MODULE_OPTIONS,
|
|
9
|
-
OUTBOX_PUBLISHER,
|
|
10
|
-
OUTBOX_STORE,
|
|
11
|
-
} from '../constant/outbox.constants';
|
|
12
|
-
|
|
13
|
-
export type OutboxRegisterAsyncOptions = Parameters<
|
|
14
|
-
ReturnType<typeof ConfigurableModuleClass<OutboxModuleOptions>>['registerAsync']
|
|
15
|
-
>[0];
|
|
16
|
-
|
|
17
|
-
const OUTBOX_RESOLVED_OPTIONS = Symbol.for('OUTBOX_RESOLVED_OPTIONS');
|
|
18
|
-
|
|
19
|
-
@Module({})
|
|
20
|
-
export class OutboxModule extends ConfigurableModuleClass<OutboxModuleOptions>(
|
|
21
|
-
OUTBOX_MODULE_OPTIONS,
|
|
22
|
-
) {
|
|
23
|
-
static register(options?: OutboxModuleOptions): DynamicModule {
|
|
24
|
-
return buildDynamicModule(super.register(options));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
static registerAsync(options: OutboxRegisterAsyncOptions): DynamicModule {
|
|
28
|
-
return buildDynamicModule(super.registerAsync(options));
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const buildDynamicModule = (base: DynamicModule): DynamicModule => ({
|
|
33
|
-
...base,
|
|
34
|
-
module: OutboxModule,
|
|
35
|
-
global: true,
|
|
36
|
-
providers: [...(base.providers ?? []), ...outboxOptionProviders(), OutboxService, OutboxRelay],
|
|
37
|
-
exports: [
|
|
38
|
-
...(base.exports ?? []),
|
|
39
|
-
OUTBOX_MODULE_OPTIONS,
|
|
40
|
-
OUTBOX_STORE,
|
|
41
|
-
OUTBOX_PUBLISHER,
|
|
42
|
-
OutboxService,
|
|
43
|
-
OutboxRelay,
|
|
44
|
-
],
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const outboxOptionProviders = (): Provider[] => [
|
|
48
|
-
{
|
|
49
|
-
provide: OUTBOX_RESOLVED_OPTIONS,
|
|
50
|
-
useFactory: (raw: OutboxModuleOptions | undefined): ResolvedOutboxOptions =>
|
|
51
|
-
resolveOutboxOptions(raw),
|
|
52
|
-
inject: [OUTBOX_MODULE_OPTIONS],
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
provide: OUTBOX_STORE,
|
|
56
|
-
useFactory: (resolved: ResolvedOutboxOptions) => resolved.store,
|
|
57
|
-
inject: [OUTBOX_RESOLVED_OPTIONS],
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
provide: OUTBOX_PUBLISHER,
|
|
61
|
-
useFactory: (resolved: ResolvedOutboxOptions) => resolved.publisher,
|
|
62
|
-
inject: [OUTBOX_RESOLVED_OPTIONS],
|
|
63
|
-
},
|
|
64
|
-
];
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { IOutboxStore } from '../interface/outbox-store.interface';
|
|
2
|
-
import { OutboxRecord } from '../interface/outbox-record.interface';
|
|
3
|
-
import { OutboxStatus } from '../interface/outbox-status.enum';
|
|
4
|
-
|
|
5
|
-
export class InMemoryOutboxStore implements IOutboxStore {
|
|
6
|
-
private readonly map = new Map<string, OutboxRecord>();
|
|
7
|
-
|
|
8
|
-
async save(record: OutboxRecord, _client: unknown): Promise<void> {
|
|
9
|
-
this.map.set(record.id, { ...record });
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
async claimBatch(
|
|
13
|
-
limit: number,
|
|
14
|
-
leaseMs: number,
|
|
15
|
-
now: Date,
|
|
16
|
-
owner: string,
|
|
17
|
-
): Promise<OutboxRecord[]> {
|
|
18
|
-
const leaseUntil = new Date(now.getTime() + leaseMs);
|
|
19
|
-
const claimed: OutboxRecord[] = [];
|
|
20
|
-
|
|
21
|
-
for (const record of this.map.values()) {
|
|
22
|
-
if (claimed.length >= limit) {
|
|
23
|
-
break;
|
|
24
|
-
}
|
|
25
|
-
if (!this.isClaimable(record, now)) {
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
const updated: OutboxRecord = {
|
|
29
|
-
...record,
|
|
30
|
-
status: OutboxStatus.PROCESSING,
|
|
31
|
-
owner,
|
|
32
|
-
leaseUntil,
|
|
33
|
-
};
|
|
34
|
-
this.map.set(record.id, updated);
|
|
35
|
-
claimed.push({ ...updated });
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return claimed;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async markPublished(id: string, publishedAt: Date, owner: string): Promise<void> {
|
|
42
|
-
const existing = this.assertOwned(id, owner);
|
|
43
|
-
if (existing === undefined) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
this.map.set(id, {
|
|
47
|
-
...existing,
|
|
48
|
-
status: OutboxStatus.PUBLISHED,
|
|
49
|
-
publishedAt,
|
|
50
|
-
leaseUntil: undefined,
|
|
51
|
-
owner: undefined,
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async markRetry(
|
|
56
|
-
id: string,
|
|
57
|
-
attempts: number,
|
|
58
|
-
nextAttemptAt: Date,
|
|
59
|
-
owner: string,
|
|
60
|
-
): Promise<void> {
|
|
61
|
-
const existing = this.assertOwned(id, owner);
|
|
62
|
-
if (existing === undefined) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
this.map.set(id, {
|
|
66
|
-
...existing,
|
|
67
|
-
status: OutboxStatus.PENDING,
|
|
68
|
-
attempts,
|
|
69
|
-
nextAttemptAt,
|
|
70
|
-
leaseUntil: undefined,
|
|
71
|
-
owner: undefined,
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
async markDead(id: string, attempts: number, owner: string): Promise<void> {
|
|
76
|
-
const existing = this.assertOwned(id, owner);
|
|
77
|
-
if (existing === undefined) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
this.map.set(id, {
|
|
81
|
-
...existing,
|
|
82
|
-
status: OutboxStatus.DEAD,
|
|
83
|
-
attempts,
|
|
84
|
-
leaseUntil: undefined,
|
|
85
|
-
owner: undefined,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private assertOwned(id: string, owner: string): OutboxRecord | undefined {
|
|
90
|
-
const existing = this.map.get(id);
|
|
91
|
-
if (existing === undefined) {
|
|
92
|
-
return undefined;
|
|
93
|
-
}
|
|
94
|
-
if (existing.owner !== owner) {
|
|
95
|
-
throw new Error(
|
|
96
|
-
`outbox record "${id}" is no longer held by lease owner "${owner}" (lease reclaimed)`,
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
return existing;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
private isClaimable(record: OutboxRecord, now: Date): boolean {
|
|
103
|
-
if (record.status === OutboxStatus.PUBLISHED || record.status === OutboxStatus.DEAD) {
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
if (record.status === OutboxStatus.PENDING) {
|
|
107
|
-
return record.nextAttemptAt.getTime() <= now.getTime();
|
|
108
|
-
}
|
|
109
|
-
if (record.status === OutboxStatus.PROCESSING) {
|
|
110
|
-
return record.leaseUntil !== undefined && record.leaseUntil.getTime() <= now.getTime();
|
|
111
|
-
}
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
}
|
package/lib/provider/index.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Inject,
|
|
3
|
-
Injectable,
|
|
4
|
-
Logger,
|
|
5
|
-
OnApplicationBootstrap,
|
|
6
|
-
OnModuleDestroy,
|
|
7
|
-
Optional,
|
|
8
|
-
} from '@nestjs/common';
|
|
9
|
-
import {
|
|
10
|
-
CLOCK,
|
|
11
|
-
IClock,
|
|
12
|
-
ID_GENERATOR,
|
|
13
|
-
IIdGenerator,
|
|
14
|
-
SystemClock,
|
|
15
|
-
UuidIdGenerator,
|
|
16
|
-
} from '@nathapp/nestjs-common';
|
|
17
|
-
import {
|
|
18
|
-
OUTBOX_MODULE_OPTIONS,
|
|
19
|
-
OUTBOX_PUBLISHER,
|
|
20
|
-
OUTBOX_STORE,
|
|
21
|
-
} from '../constant/outbox.constants';
|
|
22
|
-
import { OutboxMisconfiguredException } from '../exception/outbox-misconfigured.exception';
|
|
23
|
-
import { IOutboxPublisher } from '../interface/outbox-publisher.interface';
|
|
24
|
-
import { OutboxModuleOptions } from '../interface/outbox-module-options.interface';
|
|
25
|
-
import { IOutboxStore } from '../interface/outbox-store.interface';
|
|
26
|
-
import { resolveOutboxOptions, ResolvedOutboxRelayOptions } from './resolve-outbox-options';
|
|
27
|
-
import { computeBackoff } from '../util/compute-backoff';
|
|
28
|
-
|
|
29
|
-
@Injectable()
|
|
30
|
-
export class OutboxRelay implements OnApplicationBootstrap, OnModuleDestroy {
|
|
31
|
-
private readonly clock: IClock;
|
|
32
|
-
private readonly idGenerator: IIdGenerator;
|
|
33
|
-
private readonly store: IOutboxStore;
|
|
34
|
-
private readonly publisher: IOutboxPublisher | undefined;
|
|
35
|
-
private readonly options: ResolvedOutboxRelayOptions;
|
|
36
|
-
private inFlight: Promise<void> | null = null;
|
|
37
|
-
private interval: ReturnType<typeof setInterval> | null = null;
|
|
38
|
-
private readonly logger = new Logger(OutboxRelay.name);
|
|
39
|
-
|
|
40
|
-
constructor(
|
|
41
|
-
@Inject(OUTBOX_MODULE_OPTIONS) rawOptions: OutboxModuleOptions | undefined,
|
|
42
|
-
@Inject(OUTBOX_STORE) store: IOutboxStore,
|
|
43
|
-
@Optional() @Inject(OUTBOX_PUBLISHER) publisher?: IOutboxPublisher,
|
|
44
|
-
@Optional() @Inject(CLOCK) clock?: IClock,
|
|
45
|
-
@Optional() @Inject(ID_GENERATOR) idGenerator?: IIdGenerator,
|
|
46
|
-
) {
|
|
47
|
-
this.store = store;
|
|
48
|
-
this.publisher = rawOptions?.publisher ?? publisher;
|
|
49
|
-
this.options = resolveOutboxOptions(rawOptions).relay;
|
|
50
|
-
this.clock = clock ?? new SystemClock();
|
|
51
|
-
this.idGenerator = idGenerator ?? new UuidIdGenerator();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async dispatchPendingBatch(): Promise<void> {
|
|
55
|
-
if (this.publisher === undefined) {
|
|
56
|
-
throw new OutboxMisconfiguredException();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (this.inFlight !== null) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const publisher = this.publisher;
|
|
64
|
-
const cycle = this.runCycle(publisher);
|
|
65
|
-
this.inFlight = cycle;
|
|
66
|
-
try {
|
|
67
|
-
await cycle;
|
|
68
|
-
} finally {
|
|
69
|
-
this.inFlight = null;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
private async runCycle(publisher: IOutboxPublisher): Promise<void> {
|
|
74
|
-
const now = this.clock.now();
|
|
75
|
-
const owner = this.idGenerator.generate();
|
|
76
|
-
const batch = await this.store.claimBatch(
|
|
77
|
-
this.options.batchSize,
|
|
78
|
-
this.options.leaseMs,
|
|
79
|
-
now,
|
|
80
|
-
owner,
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
if (batch.length === 0) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
for (const record of batch) {
|
|
88
|
-
try {
|
|
89
|
-
await publisher.publish(record);
|
|
90
|
-
try {
|
|
91
|
-
await this.store.markPublished(record.id, this.clock.now(), owner);
|
|
92
|
-
} catch {
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
} catch {
|
|
96
|
-
const attempts = record.attempts + 1;
|
|
97
|
-
try {
|
|
98
|
-
if (attempts >= this.options.maxAttempts) {
|
|
99
|
-
await this.store.markDead(record.id, attempts, owner);
|
|
100
|
-
} else {
|
|
101
|
-
const failureNow = this.clock.now();
|
|
102
|
-
const backoffMs = computeBackoff(
|
|
103
|
-
attempts,
|
|
104
|
-
this.options.backoffBaseMs,
|
|
105
|
-
this.options.backoffCapMs,
|
|
106
|
-
);
|
|
107
|
-
const nextAttemptAt = new Date(failureNow.getTime() + backoffMs);
|
|
108
|
-
await this.store.markRetry(record.id, attempts, nextAttemptAt, owner);
|
|
109
|
-
}
|
|
110
|
-
} catch {
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
async onApplicationBootstrap(): Promise<void> {
|
|
118
|
-
if (!this.options.enabled) {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
if (this.publisher === undefined) {
|
|
122
|
-
throw new OutboxMisconfiguredException();
|
|
123
|
-
}
|
|
124
|
-
this.interval = setInterval(() => {
|
|
125
|
-
this.dispatchPendingBatch().catch((err: unknown) => {
|
|
126
|
-
this.logger.error(
|
|
127
|
-
`Outbox relay cycle failed: ${(err as Error)?.message ?? err}`,
|
|
128
|
-
(err as Error)?.stack,
|
|
129
|
-
);
|
|
130
|
-
});
|
|
131
|
-
}, this.options.pollIntervalMs);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async onModuleDestroy(): Promise<void> {
|
|
135
|
-
if (this.interval !== null) {
|
|
136
|
-
clearInterval(this.interval);
|
|
137
|
-
this.interval = null;
|
|
138
|
-
}
|
|
139
|
-
if (this.inFlight !== null) {
|
|
140
|
-
try {
|
|
141
|
-
await this.inFlight;
|
|
142
|
-
} catch {
|
|
143
|
-
// swallow — cycle errors are already logged by the interval tick
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
2
|
-
import {
|
|
3
|
-
CLOCK,
|
|
4
|
-
IClock,
|
|
5
|
-
ID_GENERATOR,
|
|
6
|
-
IIdGenerator,
|
|
7
|
-
SystemClock,
|
|
8
|
-
UuidIdGenerator,
|
|
9
|
-
} from '@nathapp/nestjs-common';
|
|
10
|
-
import {
|
|
11
|
-
ITransactionManager,
|
|
12
|
-
TRANSACTION_MANAGER,
|
|
13
|
-
} from '@nathapp/nestjs-data';
|
|
14
|
-
import { OUTBOX_STORE } from '../constant/outbox.constants';
|
|
15
|
-
import { OutboxInvalidEventException } from '../exception/outbox-invalid-event.exception';
|
|
16
|
-
import {
|
|
17
|
-
OutboxEventInput,
|
|
18
|
-
OutboxRecord,
|
|
19
|
-
} from '../interface/outbox-record.interface';
|
|
20
|
-
import { OutboxStatus } from '../interface/outbox-status.enum';
|
|
21
|
-
import { IOutboxStore } from '../interface/outbox-store.interface';
|
|
22
|
-
|
|
23
|
-
@Injectable()
|
|
24
|
-
export class OutboxService {
|
|
25
|
-
private readonly clock: IClock;
|
|
26
|
-
private readonly idGenerator: IIdGenerator;
|
|
27
|
-
|
|
28
|
-
constructor(
|
|
29
|
-
@Inject(OUTBOX_STORE)
|
|
30
|
-
private readonly store: IOutboxStore,
|
|
31
|
-
@Optional() @Inject(TRANSACTION_MANAGER)
|
|
32
|
-
private readonly txManager?: ITransactionManager,
|
|
33
|
-
@Optional() @Inject(CLOCK) clock?: IClock,
|
|
34
|
-
@Optional() @Inject(ID_GENERATOR) idGenerator?: IIdGenerator,
|
|
35
|
-
) {
|
|
36
|
-
this.clock = clock ?? new SystemClock();
|
|
37
|
-
this.idGenerator = idGenerator ?? new UuidIdGenerator();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async record(event: OutboxEventInput): Promise<OutboxRecord> {
|
|
41
|
-
if (event === null || typeof event !== 'object') {
|
|
42
|
-
throw new OutboxInvalidEventException();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const rawType = (event as { type?: unknown }).type;
|
|
46
|
-
if (typeof rawType !== 'string' || rawType.trim().length === 0) {
|
|
47
|
-
throw new OutboxInvalidEventException();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const type = rawType.trim();
|
|
51
|
-
|
|
52
|
-
const now = this.clock.now();
|
|
53
|
-
const record: OutboxRecord = {
|
|
54
|
-
id: this.idGenerator.generate(),
|
|
55
|
-
type,
|
|
56
|
-
payload: (event as OutboxEventInput).payload,
|
|
57
|
-
status: OutboxStatus.PENDING,
|
|
58
|
-
attempts: 0,
|
|
59
|
-
createdAt: now,
|
|
60
|
-
nextAttemptAt: now,
|
|
61
|
-
headers: event.headers,
|
|
62
|
-
metadata: event.metadata,
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const client = this.txManager?.getClient();
|
|
66
|
-
await this.store.save(record, client);
|
|
67
|
-
|
|
68
|
-
return record;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { OutboxModuleOptions } from '../interface/outbox-module-options.interface';
|
|
2
|
-
import { IOutboxStore } from '../interface/outbox-store.interface';
|
|
3
|
-
import { IOutboxPublisher } from '../interface/outbox-publisher.interface';
|
|
4
|
-
import { InMemoryOutboxStore } from './in-memory-outbox-store';
|
|
5
|
-
|
|
6
|
-
export interface ResolvedOutboxRelayOptions {
|
|
7
|
-
enabled: boolean;
|
|
8
|
-
pollIntervalMs: number;
|
|
9
|
-
batchSize: number;
|
|
10
|
-
leaseMs: number;
|
|
11
|
-
maxAttempts: number;
|
|
12
|
-
backoffBaseMs: number;
|
|
13
|
-
backoffCapMs: number;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ResolvedOutboxOptions {
|
|
17
|
-
store: IOutboxStore;
|
|
18
|
-
publisher: IOutboxPublisher | undefined;
|
|
19
|
-
relay: ResolvedOutboxRelayOptions;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const OUTBOX_DEFAULTS = {
|
|
23
|
-
relay: {
|
|
24
|
-
enabled: true,
|
|
25
|
-
pollIntervalMs: 5000,
|
|
26
|
-
batchSize: 10,
|
|
27
|
-
leaseMs: 30000,
|
|
28
|
-
maxAttempts: 10,
|
|
29
|
-
backoffBaseMs: 1000,
|
|
30
|
-
backoffCapMs: 300000,
|
|
31
|
-
},
|
|
32
|
-
} as const;
|
|
33
|
-
|
|
34
|
-
export function resolveOutboxOptions(
|
|
35
|
-
options: OutboxModuleOptions | undefined,
|
|
36
|
-
): ResolvedOutboxOptions {
|
|
37
|
-
const relay = options?.relay ?? {};
|
|
38
|
-
return {
|
|
39
|
-
store: options?.store ?? new InMemoryOutboxStore(),
|
|
40
|
-
publisher: options?.publisher,
|
|
41
|
-
relay: {
|
|
42
|
-
enabled: relay.enabled ?? OUTBOX_DEFAULTS.relay.enabled,
|
|
43
|
-
pollIntervalMs: relay.pollIntervalMs ?? OUTBOX_DEFAULTS.relay.pollIntervalMs,
|
|
44
|
-
batchSize: relay.batchSize ?? OUTBOX_DEFAULTS.relay.batchSize,
|
|
45
|
-
leaseMs: relay.leaseMs ?? OUTBOX_DEFAULTS.relay.leaseMs,
|
|
46
|
-
maxAttempts: relay.maxAttempts ?? OUTBOX_DEFAULTS.relay.maxAttempts,
|
|
47
|
-
backoffBaseMs: relay.backoffBaseMs ?? OUTBOX_DEFAULTS.relay.backoffBaseMs,
|
|
48
|
-
backoffCapMs: relay.backoffCapMs ?? OUTBOX_DEFAULTS.relay.backoffCapMs,
|
|
49
|
-
},
|
|
50
|
-
};
|
|
51
|
-
}
|
package/lib/util/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './compute-backoff';
|