-
Notifications
You must be signed in to change notification settings - Fork 741
/
Copy pathindex.ts
258 lines (238 loc) · 9.12 KB
/
index.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { BatchProcessor } from '@crowd/common'
import { DbConnection, DbStore } from '@crowd/data-access-layer/src/database'
import { Logger } from '@crowd/logging'
import { RedisClient } from '@crowd/redis'
import {
SEARCH_SYNC_WORKER_QUEUE_SETTINGS,
SqsClient,
SqsPrioritizedQueueReciever,
} from '@crowd/sqs'
import { Span, SpanStatusCode, Tracer } from '@crowd/tracing'
import { IQueueMessage, QueuePriorityLevel, SearchSyncWorkerQueueMessageType } from '@crowd/types'
import {
OpenSearchService,
ActivitySyncService,
MemberSyncService,
OrganizationSyncService,
} from '@crowd/opensearch'
import { SERVICE_CONFIG } from '../conf'
/* eslint-disable @typescript-eslint/no-explicit-any */
export class WorkerQueueReceiver extends SqsPrioritizedQueueReciever {
// private readonly memberBatchProcessor: BatchProcessor<string>
private readonly activityBatchProcessor: BatchProcessor<string>
// private readonly organizationBatchProcessor: BatchProcessor<string>
constructor(
level: QueuePriorityLevel,
private readonly redisClient: RedisClient,
client: SqsClient,
private readonly dbConn: DbConnection,
private readonly openSearchService: OpenSearchService,
tracer: Tracer,
parentLog: Logger,
maxConcurrentProcessing: number,
) {
super(
level,
client,
SEARCH_SYNC_WORKER_QUEUE_SETTINGS,
maxConcurrentProcessing,
tracer,
parentLog,
true,
5 * 60,
10,
)
// this.memberBatchProcessor = new BatchProcessor(
// 100,
// 30,
// async (memberIds) => {
// const distinct = Array.from(new Set(memberIds))
// if (distinct.length > 0) {
// this.log.info({ batchSize: distinct.length }, 'Processing batch of members!')
// await this.initMemberService().syncMembers(distinct)
// }
// },
// async (memberIds, err) => {
// this.log.error(err, { memberIds }, 'Error while processing batch of members!')
// },
// )
this.activityBatchProcessor = new BatchProcessor(
200,
30,
async (activityIds) => {
const distinct = Array.from(new Set(activityIds))
if (distinct.length > 0) {
this.log.info({ batchSize: distinct.length }, 'Processing batch of activities!')
await this.initActivityService().syncActivities(distinct)
}
},
async (activityIds, err) => {
this.log.error(err, { activityIds }, 'Error while processing batch of activities!')
},
)
// this.organizationBatchProcessor = new BatchProcessor(
// 20,
// 30,
// async (organizationIds) => {
// const distinct = Array.from(new Set(organizationIds))
// if (distinct.length > 0) {
// this.log.info({ batchSize: distinct.length }, 'Processing batch of organizations!')
// await this.initOrganizationService().syncOrganizations(distinct)
// }
// },
// async (organizationIds, err) => {
// this.log.error(err, { organizationIds }, 'Error while processing batch of organizations!')
// },
// )
}
private initMemberService(): MemberSyncService {
return new MemberSyncService(
this.redisClient,
new DbStore(this.log, this.dbConn),
this.openSearchService,
this.log,
SERVICE_CONFIG(),
)
}
private initActivityService(): ActivitySyncService {
return new ActivitySyncService(
new DbStore(this.log, this.dbConn),
this.openSearchService,
this.log,
)
}
private initOrganizationService(): OrganizationSyncService {
return new OrganizationSyncService(
new DbStore(this.log, this.dbConn),
this.openSearchService,
this.log,
SERVICE_CONFIG(),
)
}
public override async processMessage<T extends IQueueMessage>(message: T): Promise<void> {
await this.tracer.startActiveSpan('ProcessMessage', async (span: Span) => {
try {
this.log.trace({ messageType: message.type }, 'Processing message!')
const type = message.type as SearchSyncWorkerQueueMessageType
const data = message as any
switch (type) {
// members
case SearchSyncWorkerQueueMessageType.SYNC_MEMBER:
if (data.memberId) {
// await this.memberBatchProcessor.addToBatch(data.memberId)
await this.initMemberService().syncMembers(
[data.memberId],
data.segmentId ? [data.segmentId] : undefined,
)
}
break
// this one taks a while so we can't relly on it to be finished in time and the queue message might pop up again so we immediatelly return
case SearchSyncWorkerQueueMessageType.SYNC_TENANT_MEMBERS:
if (data.tenantId) {
this.initMemberService()
.syncTenantMembers(data.tenantId)
.catch((err) => this.log.error(err, 'Error while syncing tenant members!'))
}
break
case SearchSyncWorkerQueueMessageType.SYNC_ORGANIZATION_MEMBERS:
if (data.organizationId) {
this.initMemberService()
.syncOrganizationMembers(data.organizationId)
.catch((err) => this.log.error(err, 'Error while syncing organization members!'))
}
break
// this one taks a while so we can't relly on it to be finished in time and the queue message might pop up again so we immediatelly return
case SearchSyncWorkerQueueMessageType.CLEANUP_TENANT_MEMBERS:
if (data.tenantId) {
this.initMemberService()
.cleanupMemberIndex(data.tenantId)
.catch((err) => this.log.error(err, 'Error while cleaning up tenant members!'))
}
break
case SearchSyncWorkerQueueMessageType.REMOVE_MEMBER:
if (data.memberId) {
await this.initMemberService().removeMember(data.memberId)
}
break
// activities
case SearchSyncWorkerQueueMessageType.SYNC_ACTIVITY:
if (data.activityId) {
await this.activityBatchProcessor.addToBatch(data.activityId)
}
break
case SearchSyncWorkerQueueMessageType.SYNC_TENANT_ACTIVITIES:
if (data.tenantId) {
this.initActivityService()
.syncTenantActivities(data.tenantId)
.catch((err) => this.log.error(err, 'Error while syncing tenant activities!'))
}
break
case SearchSyncWorkerQueueMessageType.SYNC_ORGANIZATION_ACTIVITIES:
if (data.organizationId) {
this.initActivityService()
.syncOrganizationActivities(data.organizationId)
.catch((err) => this.log.error(err, 'Error while syncing organization activities!'))
}
break
case SearchSyncWorkerQueueMessageType.CLEANUP_TENANT_ACTIVITIES:
if (data.tenantId) {
this.initActivityService()
.cleanupActivityIndex(data.tenantId)
.catch((err) => this.log.error(err, 'Error while cleaning up tenant activities!'))
}
break
case SearchSyncWorkerQueueMessageType.REMOVE_ACTIVITY:
if (data.activityId) {
await this.initActivityService().removeActivity(data.activityId)
}
break
// organizations
case SearchSyncWorkerQueueMessageType.SYNC_ORGANIZATION:
if (data.organizationId) {
// await this.organizationBatchProcessor.addToBatch(data.organizationId)
await this.initOrganizationService().syncOrganizations(
[data.organizationId],
data.segmentId ? [data.segmentId] : undefined,
)
}
break
case SearchSyncWorkerQueueMessageType.SYNC_TENANT_ORGANIZATIONS:
if (data.tenantId) {
this.initOrganizationService()
.syncTenantOrganizations(data.tenantId)
.catch((err) => this.log.error(err, 'Error while syncing tenant organizations!'))
}
break
case SearchSyncWorkerQueueMessageType.CLEANUP_TENANT_ORGANIZATIONS:
if (data.tenantId) {
this.initOrganizationService()
.cleanupOrganizationIndex(data.tenantId)
.catch((err) => {
this.log.error(err, 'Error while cleaning up tenant organizations!')
})
}
break
case SearchSyncWorkerQueueMessageType.REMOVE_ORGANIZATION:
if (data.organizationId) {
await this.initOrganizationService().removeOrganization(data.organizationId)
}
break
default:
throw new Error(`Unknown message type: ${message.type}`)
}
span.setStatus({
code: SpanStatusCode.OK,
})
} catch (err) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: err,
})
this.log.error(err, 'Error while processing message!')
throw err
} finally {
span.end()
}
})
}
}