-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAbstractSnsSqsConsumer.ts
More file actions
217 lines (196 loc) · 7.21 KB
/
AbstractSnsSqsConsumer.ts
File metadata and controls
217 lines (196 loc) · 7.21 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import type { SNSClient } from '@aws-sdk/client-sns'
import type { STSClient } from '@aws-sdk/client-sts'
import type {
SQSConsumerDependencies,
SQSConsumerOptions,
SQSCreationConfig,
SQSMessage,
SQSQueueLocatorType,
} from '@message-queue-toolkit/sqs'
import { AbstractSqsConsumer, deleteSqs } from '@message-queue-toolkit/sqs'
import { deleteSnsSqs, initSnsSqs } from '../utils/snsInitter.ts'
import { readSnsMessage } from '../utils/snsMessageReader.ts'
import type { SNSSubscriptionOptions } from '../utils/snsSubscriber.ts'
import type { SNSCreationConfig, SNSOptions, SNSTopicLocatorType } from './AbstractSnsService.ts'
export type SNSSQSConsumerDependencies = SQSConsumerDependencies & {
snsClient: SNSClient
stsClient: STSClient
}
export type SNSSQSCreationConfig = Omit<SQSCreationConfig, 'policyConfig'> & SNSCreationConfig
export type SNSSQSQueueLocatorType = Partial<SQSQueueLocatorType> &
SNSTopicLocatorType & {
subscriptionArn?: string
}
export type SNSSQSConsumerOptions<
MessagePayloadType extends object,
ExecutionContext,
PrehandlerOutput,
> = SQSConsumerOptions<
MessagePayloadType,
ExecutionContext,
PrehandlerOutput,
SNSSQSCreationConfig,
SNSSQSQueueLocatorType
> &
SNSOptions & {
subscriptionConfig?: SNSSubscriptionOptions
}
export abstract class AbstractSnsSqsConsumer<
MessagePayloadSchemas extends object,
ExecutionContext,
PrehandlerOutput = undefined,
> extends AbstractSqsConsumer<
MessagePayloadSchemas,
ExecutionContext,
PrehandlerOutput,
SNSSQSCreationConfig,
SNSSQSQueueLocatorType,
SNSSQSConsumerOptions<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>
> {
private readonly subscriptionConfig?: SNSSubscriptionOptions
private readonly snsClient: SNSClient
private readonly stsClient: STSClient
/**
* Tracks whether resources (SNS topic, SQS queue, subscription) are ready.
* In non-blocking polling mode, this may be false initially and become true
* when the onResourcesReady callback fires.
*/
private resourcesReady: boolean = false
/**
* Tracks whether start() has been called but consumers couldn't be started
* because resources weren't ready yet. When resources become ready and this
* is true, consumers will be started automatically.
*/
private startRequested: boolean = false
// @ts-expect-error
protected topicArn: string
// @ts-expect-error
protected subscriptionArn: string
protected constructor(
dependencies: SNSSQSConsumerDependencies,
options: SNSSQSConsumerOptions<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>,
executionContext: ExecutionContext,
) {
super(
dependencies,
{
...options,
},
executionContext,
)
this.subscriptionConfig = options.subscriptionConfig
this.snsClient = dependencies.snsClient
this.stsClient = dependencies.stsClient
}
override async init(): Promise<void> {
if (this.deletionConfig && this.creationConfig && this.subscriptionConfig) {
await deleteSnsSqs(
this.sqsClient,
this.snsClient,
this.stsClient,
this.deletionConfig,
this.creationConfig.queue,
this.creationConfig.topic,
this.subscriptionConfig,
undefined,
this.locatorConfig,
)
} else if (this.deletionConfig && this.creationConfig) {
await deleteSqs(this.sqsClient, this.deletionConfig, this.creationConfig)
}
const initSnsSqsResult = await initSnsSqs(
this.sqsClient,
this.snsClient,
this.stsClient,
this.locatorConfig,
this.creationConfig,
this.subscriptionConfig,
{
logger: this.logger,
// This callback is only invoked in non-blocking mode when resources were NOT
// immediately available. It will NOT be called if resourcesReady is true.
onResourcesReady: (result) => {
// Update values that were empty when resourcesReady was false
this.topicArn = result.topicArn
this.queueUrl = result.queueUrl
this.subscriptionArn = result.subscriptionArn
this.queueName = result.queueName
this.resourcesReady = true
// Initialize DLQ now that resources are ready (this is mutually exclusive
// with the synchronous initDeadLetterQueue call below)
this.initDeadLetterQueue()
.catch((err) => {
this.logger.error({
message: 'Failed to initialize dead letter queue after resources became ready',
error: err,
})
})
.then(() => {
// If start() was called while resources weren't ready, start consumers now
if (this.startRequested) {
this.logger.info({
message: 'Resources now ready, starting consumers',
queueName: this.queueName,
topicArn: this.topicArn,
})
return this.startConsumers()
}
})
.catch((err) => {
this.logger.error({
message: 'Failed to start consumers after resources became ready',
error: err,
})
})
},
},
)
// Always assign topicArn and queueName (always valid in both blocking and non-blocking modes)
this.topicArn = initSnsSqsResult.topicArn
this.queueName = initSnsSqsResult.queueName
this.resourcesReady = initSnsSqsResult.resourcesReady
// Only assign queueUrl and subscriptionArn if resources are ready,
// or if they have valid values (non-blocking mode with locatorConfig provides valid values)
if (initSnsSqsResult.resourcesReady || initSnsSqsResult.queueUrl) {
this.queueUrl = initSnsSqsResult.queueUrl
this.subscriptionArn = initSnsSqsResult.subscriptionArn
}
// Only initialize DLQ if resources are ready and queueUrl is available
if (initSnsSqsResult.resourcesReady && initSnsSqsResult.queueUrl) {
await this.initDeadLetterQueue()
}
}
/**
* Starts the consumer. In non-blocking polling mode, if resources aren't ready yet,
* this method will return immediately and consumers will start automatically once
* resources become available.
*/
public override async start() {
await this.init()
if (!this.resourcesReady) {
// Resources not ready yet (non-blocking polling mode), mark that start was requested.
// Consumers will be started automatically when onResourcesReady callback fires.
this.startRequested = true
this.logger.info({
message:
'Start requested but resources not ready yet, will start when resources become available',
queueName: this.queueName,
topicArn: this.topicArn,
})
return
}
// Resources are ready, start consumers immediately
await this.startConsumers()
}
protected override resolveMessage(message: SQSMessage) {
const result = readSnsMessage(message, this.errorResolver)
if (result.result) {
return result
}
// if it not an SNS message, then it is a SQS message
return super.resolveMessage(message)
}
protected override resolveSchema(messagePayload: MessagePayloadSchemas) {
return this._messageSchemaContainer.resolveSchema(messagePayload)
}
}