@@ -8,98 +8,8 @@ import type {
8
8
} from '@message-queue-toolkit/core'
9
9
import { PromisePool } from '@supercharge/promise-pool'
10
10
import { uuidv7 } from 'uuidv7'
11
-
12
- /**
13
- * Status of the outbox entry.
14
- * - CREATED - entry was created and is waiting to be processed to publish actual event
15
- * - ACKED - entry was picked up by outbox job and is being processed
16
- * - SUCCESS - entry was successfully processed, event was published
17
- * - FAILED - entry processing failed, it will be retried
18
- */
19
- export type OutboxEntryStatus = 'CREATED' | 'ACKED' | 'SUCCESS' | 'FAILED'
20
-
21
- export type OutboxEntry < SupportedEvent extends CommonEventDefinition > = {
22
- id : string
23
- event : SupportedEvent
24
- data : Omit < CommonEventDefinitionPublisherSchemaType < SupportedEvent > , 'type' >
25
- precedingMessageMetadata ?: Partial < ConsumerMessageMetadataType >
26
- status : OutboxEntryStatus
27
- created : Date
28
- updated ?: Date
29
- retryCount : number
30
- }
31
-
32
- export interface OutboxAccumulator < SupportedEvents extends CommonEventDefinition [ ] > {
33
- add ( outboxEntry : OutboxEntry < SupportedEvents [ number ] > ) : Promise < void >
34
-
35
- addFailure ( outboxEntry : OutboxEntry < SupportedEvents [ number ] > ) : Promise < void >
36
-
37
- getEntries ( ) : Promise < OutboxEntry < SupportedEvents [ number ] > [ ] >
38
-
39
- getFailedEntries ( ) : Promise < OutboxEntry < SupportedEvents [ number ] > [ ] >
40
-
41
- clear ( ) : Promise < void >
42
- }
43
-
44
- export class InMemoryOutboxAccumulator < SupportedEvents extends CommonEventDefinition [ ] >
45
- implements OutboxAccumulator < SupportedEvents >
46
- {
47
- private entries : OutboxEntry < SupportedEvents [ number ] > [ ] = [ ]
48
- private failedEntries : OutboxEntry < SupportedEvents [ number ] > [ ] = [ ]
49
-
50
- public add ( outboxEntry : OutboxEntry < SupportedEvents [ number ] > ) {
51
- this . entries = [ ...this . entries , outboxEntry ]
52
-
53
- return Promise . resolve ( )
54
- }
55
-
56
- public addFailure ( outboxEntry : OutboxEntry < SupportedEvents [ number ] > ) {
57
- this . failedEntries = [ ...this . failedEntries , outboxEntry ]
58
-
59
- return Promise . resolve ( )
60
- }
61
-
62
- getEntries ( ) : Promise < OutboxEntry < SupportedEvents [ number ] > [ ] > {
63
- return Promise . resolve ( this . entries )
64
- }
65
-
66
- getFailedEntries ( ) : Promise < OutboxEntry < SupportedEvents [ number ] > [ ] > {
67
- return Promise . resolve ( this . failedEntries )
68
- }
69
-
70
- public clear ( ) : Promise < void > {
71
- this . entries = [ ]
72
- this . failedEntries = [ ]
73
- return Promise . resolve ( )
74
- }
75
- }
76
-
77
- /**
78
- * Takes care of persisting and retrieving outbox entries.
79
- *
80
- * Implementation is required:
81
- * - in order to fulfill at least once delivery guarantee, persisting entries should be performed inside isolated transaction
82
- * - to return entries in the order they were created (UUID7 is used to create entries in OutboxEventEmitter)
83
- * - returned entries should not include the ones with 'SUCCESS' status
84
- */
85
- export interface OutboxStorage < SupportedEvents extends CommonEventDefinition [ ] > {
86
- create (
87
- outboxEntry : OutboxEntry < SupportedEvents [ number ] > ,
88
- ) : Promise < OutboxEntry < SupportedEvents [ number ] > >
89
-
90
- flush ( outboxAccumulator : OutboxAccumulator < SupportedEvents > ) : Promise < void >
91
-
92
- update (
93
- outboxEntry : OutboxEntry < SupportedEvents [ number ] > ,
94
- ) : Promise < OutboxEntry < SupportedEvents [ number ] > >
95
-
96
- /**
97
- * Returns entries in the order they were created. It doesn't return entries with 'SUCCESS' status. It doesn't return entries that have been retried more than maxRetryCount times.
98
- *
99
- * For example if entry retryCount is 1 and maxRetryCount is 1, entry MUST be returned. If it fails again then retry count is 2, in that case entry MUST NOT be returned.
100
- */
101
- getEntries ( maxRetryCount : number ) : Promise < OutboxEntry < SupportedEvents [ number ] > [ ] >
102
- }
11
+ import type { OutboxAccumulator } from './accumulators'
12
+ import type { OutboxStorage } from './storage'
103
13
104
14
/**
105
15
* Main logic for handling outbox entries.
@@ -132,6 +42,7 @@ export class OutboxProcessor<SupportedEvents extends CommonEventDefinition[]> {
132
42
} )
133
43
134
44
await this . outboxStorage . flush ( this . outboxAccumulator )
45
+ await this . outboxAccumulator . clear ( )
135
46
}
136
47
}
137
48
0 commit comments