@@ -53,6 +53,41 @@ export interface OutboxStorage<SupportedEvents extends CommonEventDefinition[]>
53
53
getEntries ( maxRetryCount : number ) : Promise < OutboxEntry < SupportedEvents [ number ] > [ ] >
54
54
}
55
55
56
+ export class OutboxProcessor < SupportedEvents extends CommonEventDefinition [ ] > {
57
+ constructor (
58
+ private readonly outboxStorage : OutboxStorage < SupportedEvents > ,
59
+ private readonly eventEmitter : DomainEventEmitter < SupportedEvents > ,
60
+ private readonly maxRetryCount : number ,
61
+ ) { }
62
+
63
+ public async processOutboxEntries ( context : JobExecutionContext ) {
64
+ const entries = await this . outboxStorage . getEntries ( this . maxRetryCount )
65
+
66
+ for ( const entry of entries ) {
67
+ try {
68
+ const updatedEntry = await this . outboxStorage . update ( {
69
+ ...entry ,
70
+ updated : new Date ( ) ,
71
+ status : 'ACKED' ,
72
+ } )
73
+
74
+ await this . eventEmitter . emit ( entry . event , entry . data , entry . precedingMessageMetadata )
75
+
76
+ await this . outboxStorage . update ( { ...updatedEntry , updated : new Date ( ) , status : 'SUCCESS' } )
77
+ } catch ( e ) {
78
+ context . logger . error ( { error : e } , 'Failed to process outbox entry.' )
79
+
80
+ await this . outboxStorage . update ( {
81
+ ...entry ,
82
+ updated : new Date ( ) ,
83
+ status : 'FAILED' ,
84
+ retryCount : entry . retryCount + 1 ,
85
+ } )
86
+ }
87
+ }
88
+ }
89
+ }
90
+
56
91
/**
57
92
* Periodic job that processes outbox entries every second. If processing takes longer than 1 second, another subsequent job WILL NOT be started.
58
93
*
@@ -63,10 +98,12 @@ export interface OutboxStorage<SupportedEvents extends CommonEventDefinition[]>
63
98
export class OutboxPeriodicJob <
64
99
SupportedEvents extends CommonEventDefinition [ ] ,
65
100
> extends AbstractPeriodicJob {
101
+ private readonly outboxProcessor : OutboxProcessor < SupportedEvents >
102
+
66
103
constructor (
67
- private readonly outboxStorage : OutboxStorage < SupportedEvents > ,
68
- private readonly eventEmitter : DomainEventEmitter < SupportedEvents > ,
69
- private readonly maxRetryCount : number ,
104
+ outboxStorage : OutboxStorage < SupportedEvents > ,
105
+ eventEmitter : DomainEventEmitter < SupportedEvents > ,
106
+ maxRetryCount : number ,
70
107
dependencies : PeriodicJobDependencies ,
71
108
) {
72
109
super (
@@ -87,33 +124,16 @@ export class OutboxPeriodicJob<
87
124
scheduler : dependencies . scheduler ,
88
125
} ,
89
126
)
127
+
128
+ this . outboxProcessor = new OutboxProcessor < SupportedEvents > (
129
+ outboxStorage ,
130
+ eventEmitter ,
131
+ maxRetryCount ,
132
+ )
90
133
}
91
134
92
135
protected async processInternal ( context : JobExecutionContext ) : Promise < void > {
93
- const entries = await this . outboxStorage . getEntries ( this . maxRetryCount )
94
-
95
- for ( const entry of entries ) {
96
- try {
97
- const updatedEntry = await this . outboxStorage . update ( {
98
- ...entry ,
99
- updated : new Date ( ) ,
100
- status : 'ACKED' ,
101
- } )
102
-
103
- await this . eventEmitter . emit ( entry . event , entry . data , entry . precedingMessageMetadata )
104
-
105
- await this . outboxStorage . update ( { ...updatedEntry , updated : new Date ( ) , status : 'SUCCESS' } )
106
- } catch ( e ) {
107
- context . logger . error ( { error : e } , 'Failed to process outbox entry.' )
108
-
109
- await this . outboxStorage . update ( {
110
- ...entry ,
111
- updated : new Date ( ) ,
112
- status : 'FAILED' ,
113
- retryCount : entry . retryCount + 1 ,
114
- } )
115
- }
116
- }
136
+ await this . outboxProcessor . processOutboxEntries ( context )
117
137
}
118
138
}
119
139
0 commit comments