chore(): Emit events in batch and index process event ids in batch (#12097)

**What**
First iteration to prevent events from overwhelming the systems.
- Group emitted event ids when possible instead of creating a message per id which leads to reduced amount of events to process massively in cases of import for example
- Update the index engine to process event data in batches of 100
- Update event handling by the index engine to be able to upsert by batch as well
- Fix index engine build config for intermediate listeners inferrence
This commit is contained in:
Adrien de Peretti
2025-04-08 18:57:08 +02:00
committed by GitHub
parent b05807bfc1
commit 74381addc3
21 changed files with 548 additions and 463 deletions

View File

@@ -16,6 +16,8 @@ import {
MedusaContext,
MedusaError,
MedusaService,
moduleEventBuilderFactory,
Modules,
UserEvents,
} from "@medusajs/framework/utils"
import jwt, { JwtPayload } from "jsonwebtoken"
@@ -110,16 +112,15 @@ export default class UserModuleService
): Promise<UserTypes.InviteDTO[]> {
const invites = await this.refreshInviteTokens_(inviteIds, sharedContext)
sharedContext.messageAggregator?.saveRawMessageData(
invites.map((invite) => ({
eventName: UserEvents.INVITE_TOKEN_GENERATED,
source: this.constructor.name,
action: "token_generated",
object: "invite",
context: sharedContext,
data: { id: invite.id },
}))
)
moduleEventBuilderFactory({
eventName: UserEvents.INVITE_TOKEN_GENERATED,
source: Modules.USER,
action: "token_generated",
object: "invite",
})({
data: invites,
sharedContext,
})
return await this.baseRepository_.serialize<UserTypes.InviteDTO[]>(
invites,
@@ -193,16 +194,15 @@ export default class UserModuleService
populate: true,
})
sharedContext.messageAggregator?.saveRawMessageData(
users.map((user) => ({
eventName: UserEvents.USER_CREATED,
source: this.constructor.name,
action: CommonEvents.CREATED,
object: "user",
context: sharedContext,
data: { id: user.id },
}))
)
moduleEventBuilderFactory({
eventName: UserEvents.USER_CREATED,
source: Modules.USER,
action: CommonEvents.CREATED,
object: "user",
})({
data: serializedUsers,
sharedContext,
})
return Array.isArray(data) ? serializedUsers : serializedUsers[0]
}
@@ -235,16 +235,15 @@ export default class UserModuleService
populate: true,
})
sharedContext.messageAggregator?.saveRawMessageData(
updatedUsers.map((user) => ({
eventName: UserEvents.USER_UPDATED,
source: this.constructor.name,
action: CommonEvents.UPDATED,
object: "user",
context: sharedContext,
data: { id: user.id },
}))
)
moduleEventBuilderFactory({
eventName: UserEvents.USER_UPDATED,
source: Modules.USER,
action: CommonEvents.UPDATED,
object: "user",
})({
data: serializedUsers,
sharedContext,
})
return Array.isArray(data) ? serializedUsers : serializedUsers[0]
}
@@ -277,27 +276,25 @@ export default class UserModuleService
populate: true,
})
sharedContext.messageAggregator?.saveRawMessageData(
invites.map((invite) => ({
eventName: UserEvents.INVITE_CREATED,
source: this.constructor.name,
action: CommonEvents.CREATED,
object: "invite",
context: sharedContext,
data: { id: invite.id },
}))
)
moduleEventBuilderFactory({
eventName: UserEvents.INVITE_CREATED,
source: Modules.USER,
action: CommonEvents.CREATED,
object: "invite",
})({
data: serializedInvites,
sharedContext,
})
sharedContext.messageAggregator?.saveRawMessageData(
invites.map((invite) => ({
eventName: UserEvents.INVITE_TOKEN_GENERATED,
source: this.constructor.name,
action: "token_generated",
object: "invite",
context: sharedContext,
data: { id: invite.id },
}))
)
moduleEventBuilderFactory({
eventName: UserEvents.INVITE_TOKEN_GENERATED,
source: Modules.USER,
action: "token_generated",
object: "invite",
})({
data: serializedInvites,
sharedContext,
})
return Array.isArray(data) ? serializedInvites : serializedInvites[0]
}
@@ -364,16 +361,15 @@ export default class UserModuleService
populate: true,
})
sharedContext.messageAggregator?.saveRawMessageData(
serializedInvites.map((invite) => ({
eventName: UserEvents.INVITE_UPDATED,
source: this.constructor.name,
action: CommonEvents.UPDATED,
object: "invite",
context: sharedContext,
data: { id: invite.id },
}))
)
moduleEventBuilderFactory({
eventName: UserEvents.INVITE_UPDATED,
source: Modules.USER,
action: CommonEvents.UPDATED,
object: "invite",
})({
data: serializedInvites,
sharedContext,
})
return Array.isArray(data) ? serializedInvites : serializedInvites[0]
}