-
Notifications
You must be signed in to change notification settings - Fork 5
/
SnsSqsPermissionConsumer.ts
187 lines (177 loc) · 5.88 KB
/
SnsSqsPermissionConsumer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type { Either } from '@lokalise/node-core'
import type {
BarrierResult,
PreHandlingOutputs,
Prehandler,
PrehandlerResult,
} from '@message-queue-toolkit/core'
import { MessageHandlerConfigBuilder } from '@message-queue-toolkit/core'
import type {
SNSSQSConsumerDependencies,
SNSSQSConsumerOptions,
} from '../../lib/sns/AbstractSnsSqsConsumer'
import { AbstractSnsSqsConsumer } from '../../lib/sns/AbstractSnsSqsConsumer'
import type {
PERMISSIONS_ADD_MESSAGE_TYPE,
PERMISSIONS_REMOVE_MESSAGE_TYPE,
} from './userConsumerSchemas'
import {
PERMISSIONS_ADD_MESSAGE_SCHEMA,
PERMISSIONS_REMOVE_MESSAGE_SCHEMA,
} from './userConsumerSchemas'
type SupportedMessages = PERMISSIONS_ADD_MESSAGE_TYPE | PERMISSIONS_REMOVE_MESSAGE_TYPE
type ExecutionContext = {
incrementAmount: number
}
type PreHandlerOutput = {
preHandlerCount: number
}
type SnsSqsPermissionConsumerOptions = Pick<
SNSSQSConsumerOptions<SupportedMessages, ExecutionContext, PreHandlerOutput>,
| 'creationConfig'
| 'locatorConfig'
| 'deletionConfig'
| 'deadLetterQueue'
| 'consumerOverrides'
| 'maxRetryDuration'
| 'payloadStoreConfig'
| 'concurrentConsumersAmount'
> & {
addPreHandlerBarrier?: (
message: SupportedMessages,
_executionContext: ExecutionContext,
preHandlerOutput: PreHandlerOutput,
) => Promise<BarrierResult<number>>
removeHandlerOverride?: (
_message: SupportedMessages,
context: ExecutionContext,
preHandlingOutputs: PreHandlingOutputs<PreHandlerOutput, number>,
) => Promise<Either<'retryLater', 'success'>>
removePreHandlers?: Prehandler<SupportedMessages, ExecutionContext, PreHandlerOutput>[]
}
export class SnsSqsPermissionConsumer extends AbstractSnsSqsConsumer<
SupportedMessages,
ExecutionContext,
PreHandlerOutput
> {
public static readonly CONSUMED_QUEUE_NAME = 'user_permissions_multi'
public static readonly SUBSCRIBED_TOPIC_NAME = 'user_permissions_multi'
public addCounter = 0
public addBarrierCounter = 0
public removeCounter = 0
public preHandlerCounter = 0
public processedMessagesIds: Set<string> = new Set()
constructor(
dependencies: SNSSQSConsumerDependencies,
options: SnsSqsPermissionConsumerOptions = {
creationConfig: {
queue: {
QueueName: SnsSqsPermissionConsumer.CONSUMED_QUEUE_NAME,
},
topic: {
Name: SnsSqsPermissionConsumer.SUBSCRIBED_TOPIC_NAME,
},
},
},
) {
const defaultRemoveHandler = (
_message: SupportedMessages,
context: ExecutionContext,
_preHandlingOutputs: PreHandlingOutputs<PreHandlerOutput, number>,
): Promise<Either<'retryLater', 'success'>> => {
this.removeCounter += context.incrementAmount
return Promise.resolve({ result: 'success' })
}
super(
dependencies,
{
handlerSpy: true,
handlers: new MessageHandlerConfigBuilder<
SupportedMessages,
ExecutionContext,
PreHandlerOutput
>()
.addConfig(
PERMISSIONS_ADD_MESSAGE_SCHEMA,
(_message, context, _preHandlingOutputs) => {
this.addCounter += context.incrementAmount
this.processedMessagesIds.add(_message.id)
return Promise.resolve({ result: 'success' })
},
{
preHandlers: [
(
message: SupportedMessages,
_context: ExecutionContext,
_preHandlerOutput: Partial<PreHandlerOutput>,
next: (result: PrehandlerResult) => void,
) => {
if (message.preHandlerIncrement) {
this.preHandlerCounter += message.preHandlerIncrement
}
next({
result: 'success',
})
},
],
preHandlerBarrier: (_message, context) => {
this.addBarrierCounter += context.incrementAmount
if (this.addBarrierCounter < 3) {
return Promise.resolve({ isPassing: false })
}
return Promise.resolve({
isPassing: true,
output: this.addBarrierCounter,
})
},
},
)
.addConfig(
PERMISSIONS_REMOVE_MESSAGE_SCHEMA,
options.removeHandlerOverride ?? defaultRemoveHandler,
{
preHandlers: options.removePreHandlers,
},
)
.build(),
deletionConfig: options.deletionConfig ?? {
deleteIfExists: false,
},
payloadStoreConfig: options.payloadStoreConfig,
consumerOverrides: options.consumerOverrides ?? {
terminateVisibilityTimeout: true, // this allows to retry failed messages immediately
},
deadLetterQueue: options.deadLetterQueue,
...(options.locatorConfig
? // biome-ignore lint/suspicious/noExplicitAny: This is expected
{ locatorConfig: options.locatorConfig, creationConfig: options.creationConfig as any }
: {
creationConfig: options.creationConfig ?? {
queue: {
QueueName: SnsSqsPermissionConsumer.CONSUMED_QUEUE_NAME,
},
topic: { Name: SnsSqsPermissionConsumer.SUBSCRIBED_TOPIC_NAME },
},
}),
messageTypeField: 'messageType',
subscriptionConfig: {
updateAttributesIfExists: false,
},
maxRetryDuration: options.maxRetryDuration,
concurrentConsumersAmount: options.concurrentConsumersAmount,
},
{
incrementAmount: 1,
},
)
}
get subscriptionProps() {
return {
topicArn: this.topicArn,
queueUrl: this.queueUrl,
queueName: this.queueName,
subscriptionArn: this.subscriptionArn,
deadLetterQueueUrl: this.deadLetterQueueUrl,
}
}
}