File tree Expand file tree Collapse file tree 2 files changed +66
-1
lines changed Expand file tree Collapse file tree 2 files changed +66
-1
lines changed Original file line number Diff line number Diff line change 1
1
# outbox-core
2
2
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
+ ```
Original file line number Diff line number Diff line change 1
1
import type { CommonEventDefinition } from '@message-queue-toolkit/schemas'
2
2
import type { OutboxEntry } from './objects.ts'
3
3
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
+ */
4
11
export interface OutboxAccumulator < SupportedEvents extends CommonEventDefinition [ ] > {
12
+ /**
13
+ * Accumulates successfully dispatched event.
14
+ * @param outboxEntry
15
+ */
5
16
add ( outboxEntry : OutboxEntry < SupportedEvents [ number ] > ) : Promise < void >
6
17
18
+ /**
19
+ * Accumulates failed event.
20
+ * @param outboxEntry
21
+ */
7
22
addFailure ( outboxEntry : OutboxEntry < SupportedEvents [ number ] > ) : Promise < void >
8
23
24
+ /**
25
+ * It's meant to be used by OutboxStorage::flush() to get all entries that should be persisted as successful ones.
26
+ */
9
27
getEntries ( ) : Promise < OutboxEntry < SupportedEvents [ number ] > [ ] >
10
28
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
+ */
11
32
getFailedEntries ( ) : Promise < OutboxEntry < SupportedEvents [ number ] > [ ] >
12
33
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
+ */
13
39
clear ( ) : Promise < void >
14
40
}
15
41
You can’t perform that action at this time.
0 commit comments