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