feat: Improve startup time by parallelizing module and link loading (#12731)

This commit is contained in:
Stevche Radevski authored and GitHub committed 2025-06-13 13:11:19 +02:00
1 parent 3b1dc0ced0
commit bff5c00777
3 files changed
+23 -12

No files matched your search

@@ -116,7 +116,7 @@ export const declineOrderTransferRequestWorkflow = createWorkflow(
): WorkflowData<void> { ): WorkflowData<void> {
const orderQuery = useQueryGraphStep({ const orderQuery = useQueryGraphStep({
entity: "order", entity: "order",
fields: ["id", "version", "declineed_at"], fields: ["id", "version", "declined_at"],
filters: { id: input.order_id }, filters: { id: input.order_id },
options: { throwIfKeyNotFound: true }, options: { throwIfKeyNotFound: true },
}).config({ name: "order-query" }) }).config({ name: "order-query" })
+10 -5
View File
@@ -395,7 +395,8 @@ class MedusaModule {
const services: { [Key: string]: any }[] = [] const services: { [Key: string]: any }[] = []
for (const moduleOptions of modulesOptions) { await promiseAll(
modulesOptions.map(async (moduleOptions) => {
const { const {
moduleKey, moduleKey,
defaultPath, defaultPath,
@@ -420,12 +421,12 @@ class MedusaModule {
if (!loaderOnly && MedusaModule.instances_.has(hashKey)) { if (!loaderOnly && MedusaModule.instances_.has(hashKey)) {
services.push(MedusaModule.instances_.get(hashKey)!) services.push(MedusaModule.instances_.get(hashKey)!)
continue return
} }
if (!loaderOnly && MedusaModule.loading_.has(hashKey)) { if (!loaderOnly && MedusaModule.loading_.has(hashKey)) {
services.push(await MedusaModule.loading_.get(hashKey)) services.push(await MedusaModule.loading_.get(hashKey))
continue return
} }
if (!loaderOnly) { if (!loaderOnly) {
@@ -455,7 +456,10 @@ class MedusaModule {
for (const service in injectedDependencies) { for (const service in injectedDependencies) {
container.register(service, asValue(injectedDependencies[service])) container.register(service, asValue(injectedDependencies[service]))
if (!container.hasRegistration(service)) { if (!container.hasRegistration(service)) {
container.register(service, asValue(injectedDependencies[service])) container.register(
service,
asValue(injectedDependencies[service])
)
} }
} }
} }
@@ -492,7 +496,8 @@ class MedusaModule {
container, container,
finishLoading, finishLoading,
}) })
} })
)
if (loaderOnly) { if (loaderOnly) {
loadedModules.forEach(({ finishLoading }) => finishLoading({})) loadedModules.forEach(({ finishLoading }) => finishLoading({}))
@@ -15,6 +15,7 @@ import {
composeTableName, composeTableName,
ContainerRegistrationKeys, ContainerRegistrationKeys,
Modules, Modules,
promiseAll,
simpleHash, simpleHash,
toPascalCase, toPascalCase,
} from "@medusajs/framework/utils" } from "@medusajs/framework/utils"
@@ -42,14 +43,18 @@ export const initialize = async (
pluginLinksDefinitions ?? [] pluginLinksDefinitions ?? []
) )
for (const linkDefinition of allLinksToLoad) { await promiseAll(
allLinksToLoad.map(async (linkDefinition) => {
const definition: ModuleJoinerConfig = JSON.parse( const definition: ModuleJoinerConfig = JSON.parse(
JSON.stringify(linkDefinition) JSON.stringify(linkDefinition)
) )
const [primary, foreign] = definition.relationships ?? [] const [primary, foreign] = definition.relationships ?? []
if (definition.relationships?.length !== 2 && !definition.isReadOnlyLink) { if (
definition.relationships?.length !== 2 &&
!definition.isReadOnlyLink
) {
throw new Error( throw new Error(
`Link module ${definition.serviceName} can only link 2 modules.` `Link module ${definition.serviceName} can only link 2 modules.`
) )
@@ -89,7 +94,7 @@ export const initialize = async (
: simpleHash(JSON.stringify(definition.extends)) : simpleHash(JSON.stringify(definition.extends))
if (modulesLoadedKeys.includes(serviceKey)) { if (modulesLoadedKeys.includes(serviceKey)) {
continue return
} else if (serviceKey in allLinks) { } else if (serviceKey in allLinks) {
throw new Error(`Link module ${serviceKey} already defined.`) throw new Error(`Link module ${serviceKey} already defined.`)
} }
@@ -107,13 +112,13 @@ export const initialize = async (
definition.extends = extended definition.extends = extended
if (extended.length === 0) { if (extended.length === 0) {
continue return
} }
} else if ( } else if (
!modulesLoadedKeys.includes(primary.serviceName) || !modulesLoadedKeys.includes(primary.serviceName) ||
!modulesLoadedKeys.includes(foreign.serviceName) !modulesLoadedKeys.includes(foreign.serviceName)
) { ) {
continue return
} }
const logger = const logger =
@@ -165,7 +170,8 @@ export const initialize = async (
}) })
allLinks[serviceKey as string] = Object.values(loaded)[0] allLinks[serviceKey as string] = Object.values(loaded)[0]
} })
)
return allLinks return allLinks
} }