Skip to content

Commit 1d0ef62

Browse files
committed
AP-5046 Readme + extra comments.
1 parent d460478 commit 1d0ef62

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

packages/outbox-core/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
11
# outbox-core
22

3-
WIP
3+
Main package that contains the core functionality of the Outbox pattern to provide "at least once" delivery semantics for messages.
4+
5+
## Installation
6+
7+
```bash
8+
npm i -S outbox-core
9+
```
10+
11+
## Usage
12+
13+
To process outbox entries and emit them to the message queue, you need to create an instance of the `OutboxPeriodicJob` class:
14+
15+
```typescript
16+
import { OutboxPeriodicJob } from '@message-queue-toolkit/outbox-core';
17+
18+
const job = new OutboxPeriodicJob(
19+
//Implementation of OutboxStorage interface, TODO: Point to other packages in message-queue-toolkit
20+
outboxStorage,
21+
//Default available accumulator for gathering outbox entries as the process job is progressing.
22+
new InMemoryOutboxAccumulator(), //Default accumulator
23+
//DomainEventEmitter, it will be used to publish events, see @message-queue-toolkit/core
24+
eventEmitter,
25+
//See PeriodicJobDependencies from @lokalise/background-jobs-common
26+
dependencies,
27+
//Retry count, how many times outbox entries should be retried to be processed
28+
3,
29+
//emitBatchSize - how many outbox entries should be emitted at once
30+
10,
31+
//internalInMs - how often the job should be executed, e.g. below it runs every 1sec
32+
1000
33+
)
34+
```
35+
36+
Job will take care of processing outbox entries emitted by:
37+
```typescript
38+
const emitter = new OutboxEventEmitter(
39+
//Same instance of outbox storage that is used by OutboxPeriodicJob
40+
outboxStorage
41+
)
42+
```

packages/outbox-core/lib/accumulators.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
import type { CommonEventDefinition } from '@message-queue-toolkit/schemas'
22
import type { OutboxEntry } from './objects.ts'
33

4+
/**
5+
* Accumulator is responsible for storing outbox entries in two cases:
6+
* - successfully dispatched event
7+
* - failed events
8+
*
9+
* Thanks to this, we can use aggregated result and persist in the storage in batches.
10+
*/
411
export interface OutboxAccumulator<SupportedEvents extends CommonEventDefinition[]> {
12+
/**
13+
* Accumulates successfully dispatched event.
14+
* @param outboxEntry
15+
*/
516
add(outboxEntry: OutboxEntry<SupportedEvents[number]>): Promise<void>
617

18+
/**
19+
* Accumulates failed event.
20+
* @param outboxEntry
21+
*/
722
addFailure(outboxEntry: OutboxEntry<SupportedEvents[number]>): Promise<void>
823

24+
/**
25+
* It's meant to be used by OutboxStorage::flush() to get all entries that should be persisted as successful ones.
26+
*/
927
getEntries(): Promise<OutboxEntry<SupportedEvents[number]>[]>
1028

29+
/**
30+
* Also used by OutboxStorage::flush() to get all entries that should be persisted as failed ones. Such entries will be retried + their retryCount will be incremented.
31+
*/
1132
getFailedEntries(): Promise<OutboxEntry<SupportedEvents[number]>[]>
1233

34+
/**
35+
* After running clear(), no entries should be returned by getEntries() and getFailedEntries().
36+
*
37+
* clear() is always called after flush() in OutboxStorage.
38+
*/
1339
clear(): Promise<void>
1440
}
1541

0 commit comments

Comments
 (0)