-
Notifications
You must be signed in to change notification settings - Fork 5
/
SqsPermissionConsumer.ts
158 lines (148 loc) · 4.8 KB
/
SqsPermissionConsumer.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
import type { Either } from '@lokalise/node-core'
import { MessageHandlerConfigBuilder } from '@message-queue-toolkit/core'
import type { BarrierResult, PreHandlingOutputs, Prehandler } from '@message-queue-toolkit/core'
import type { SQSConsumerDependencies, SQSConsumerOptions } from '../../lib/sqs/AbstractSqsConsumer'
import { AbstractSqsConsumer } from '../../lib/sqs/AbstractSqsConsumer'
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 SqsPermissionConsumerOptions = Pick<
SQSConsumerOptions<SupportedMessages, ExecutionContext, PrehandlerOutput>,
| 'creationConfig'
| 'locatorConfig'
| 'logMessages'
| 'deletionConfig'
| 'deadLetterQueue'
| 'consumerOverrides'
| 'maxRetryDuration'
| 'payloadStoreConfig'
> & {
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>[]
concurrentConsumersAmount?: number
}
type ExecutionContext = {
incrementAmount: number
}
type PrehandlerOutput = {
messageId: string
}
export class SqsPermissionConsumer extends AbstractSqsConsumer<
SupportedMessages,
ExecutionContext,
PrehandlerOutput
> {
public addCounter = 0
public removeCounter = 0
public processedMessagesIds: Set<string> = new Set()
public static readonly QUEUE_NAME = 'user_permissions_multi'
constructor(
dependencies: SQSConsumerDependencies,
options: SqsPermissionConsumerOptions = {
creationConfig: {
queue: {
QueueName: SqsPermissionConsumer.QUEUE_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,
{
...(options.locatorConfig
? { locatorConfig: options.locatorConfig }
: {
creationConfig: options.creationConfig ?? {
queue: { QueueName: SqsPermissionConsumer.QUEUE_NAME },
},
}),
logMessages: options.logMessages,
deletionConfig: options.deletionConfig ?? {
deleteIfExists: true,
},
deadLetterQueue: options.deadLetterQueue,
messageTypeField: 'messageType',
handlerSpy: true,
consumerOverrides: options.consumerOverrides ?? {
terminateVisibilityTimeout: true, // this allows to retry failed messages immediately
},
concurrentConsumersAmount: options.concurrentConsumersAmount,
maxRetryDuration: options.maxRetryDuration,
payloadStoreConfig: options.payloadStoreConfig,
handlers: new MessageHandlerConfigBuilder<
SupportedMessages,
ExecutionContext,
PrehandlerOutput
>()
.addConfig(
PERMISSIONS_ADD_MESSAGE_SCHEMA,
(_message, context, barrierOutput) => {
if (options.addPreHandlerBarrier && !barrierOutput) {
return Promise.resolve({ error: 'retryLater' })
}
this.addCounter += context.incrementAmount
this.processedMessagesIds.add(_message.id)
return Promise.resolve({ result: 'success' })
},
{
preHandlerBarrier: options.addPreHandlerBarrier,
preHandlers: [
(message, _context, preHandlerOutput, next) => {
preHandlerOutput.messageId = message.id
next({
result: 'success',
})
},
],
},
)
.addConfig(
PERMISSIONS_REMOVE_MESSAGE_SCHEMA,
options.removeHandlerOverride ?? defaultRemoveHandler,
{
preHandlers: options.removePreHandlers,
},
)
.build(),
},
{
incrementAmount: 1,
},
)
}
public get queueProps() {
return {
name: this.queueName,
url: this.queueUrl,
arn: this.queueArn,
}
}
public get dlqUrl() {
return this.deadLetterQueueUrl ?? ''
}
}