feat: Improve startup time by parallelizing module and link loading (#12731)
This commit is contained in:
@@ -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,104 +395,109 @@ class MedusaModule {
|
||||
|
||||
const services: { [Key: string]: any }[] = []
|
||||
|
||||
for (const moduleOptions of modulesOptions) {
|
||||
const {
|
||||
moduleKey,
|
||||
defaultPath,
|
||||
declaration,
|
||||
moduleExports,
|
||||
sharedContainer,
|
||||
moduleDefinition,
|
||||
injectedDependencies,
|
||||
} = moduleOptions
|
||||
await promiseAll(
|
||||
modulesOptions.map(async (moduleOptions) => {
|
||||
const {
|
||||
moduleKey,
|
||||
defaultPath,
|
||||
declaration,
|
||||
moduleExports,
|
||||
sharedContainer,
|
||||
moduleDefinition,
|
||||
injectedDependencies,
|
||||
} = moduleOptions
|
||||
|
||||
const hashKey = simpleHash(
|
||||
stringifyCircular({ moduleKey, defaultPath, declaration })
|
||||
)
|
||||
const hashKey = simpleHash(
|
||||
stringifyCircular({ moduleKey, defaultPath, declaration })
|
||||
)
|
||||
|
||||
let finishLoading: any
|
||||
let errorLoading: any
|
||||
let finishLoading: any
|
||||
let errorLoading: any
|
||||
|
||||
const loadingPromise = new Promise((resolve, reject) => {
|
||||
finishLoading = resolve
|
||||
errorLoading = reject
|
||||
})
|
||||
const loadingPromise = new Promise((resolve, reject) => {
|
||||
finishLoading = resolve
|
||||
errorLoading = reject
|
||||
})
|
||||
|
||||
if (!loaderOnly && MedusaModule.instances_.has(hashKey)) {
|
||||
services.push(MedusaModule.instances_.get(hashKey)!)
|
||||
continue
|
||||
}
|
||||
if (!loaderOnly && MedusaModule.instances_.has(hashKey)) {
|
||||
services.push(MedusaModule.instances_.get(hashKey)!)
|
||||
return
|
||||
}
|
||||
|
||||
if (!loaderOnly && MedusaModule.loading_.has(hashKey)) {
|
||||
services.push(await MedusaModule.loading_.get(hashKey))
|
||||
continue
|
||||
}
|
||||
if (!loaderOnly && MedusaModule.loading_.has(hashKey)) {
|
||||
services.push(await MedusaModule.loading_.get(hashKey))
|
||||
return
|
||||
}
|
||||
|
||||
if (!loaderOnly) {
|
||||
MedusaModule.loading_.set(hashKey, loadingPromise)
|
||||
}
|
||||
if (!loaderOnly) {
|
||||
MedusaModule.loading_.set(hashKey, loadingPromise)
|
||||
}
|
||||
|
||||
let modDeclaration =
|
||||
declaration ??
|
||||
({} as InternalModuleDeclaration | ExternalModuleDeclaration)
|
||||
let modDeclaration =
|
||||
declaration ??
|
||||
({} as InternalModuleDeclaration | ExternalModuleDeclaration)
|
||||
|
||||
if (declaration?.scope !== MODULE_SCOPE.EXTERNAL) {
|
||||
modDeclaration = {
|
||||
scope: declaration?.scope || MODULE_SCOPE.INTERNAL,
|
||||
resolve: defaultPath,
|
||||
options: declaration?.options ?? declaration,
|
||||
dependencies:
|
||||
(declaration as InternalModuleDeclaration)?.dependencies ?? [],
|
||||
alias: declaration?.alias,
|
||||
main: declaration?.main,
|
||||
worker_mode: workerMode,
|
||||
} as InternalModuleDeclaration
|
||||
}
|
||||
if (declaration?.scope !== MODULE_SCOPE.EXTERNAL) {
|
||||
modDeclaration = {
|
||||
scope: declaration?.scope || MODULE_SCOPE.INTERNAL,
|
||||
resolve: defaultPath,
|
||||
options: declaration?.options ?? declaration,
|
||||
dependencies:
|
||||
(declaration as InternalModuleDeclaration)?.dependencies ?? [],
|
||||
alias: declaration?.alias,
|
||||
main: declaration?.main,
|
||||
worker_mode: workerMode,
|
||||
} as InternalModuleDeclaration
|
||||
}
|
||||
|
||||
const container = sharedContainer ?? createMedusaContainer()
|
||||
const container = sharedContainer ?? createMedusaContainer()
|
||||
|
||||
if (injectedDependencies) {
|
||||
for (const service in injectedDependencies) {
|
||||
container.register(service, asValue(injectedDependencies[service]))
|
||||
if (!container.hasRegistration(service)) {
|
||||
if (injectedDependencies) {
|
||||
for (const service in injectedDependencies) {
|
||||
container.register(service, asValue(injectedDependencies[service]))
|
||||
if (!container.hasRegistration(service)) {
|
||||
container.register(
|
||||
service,
|
||||
asValue(injectedDependencies[service])
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const moduleResolutions = registerMedusaModule(
|
||||
moduleKey,
|
||||
modDeclaration!,
|
||||
moduleExports,
|
||||
moduleDefinition
|
||||
)
|
||||
const moduleResolutions = registerMedusaModule(
|
||||
moduleKey,
|
||||
modDeclaration!,
|
||||
moduleExports,
|
||||
moduleDefinition
|
||||
)
|
||||
|
||||
const logger_ =
|
||||
container.resolve(ContainerRegistrationKeys.LOGGER, {
|
||||
allowUnregistered: true,
|
||||
}) ?? logger
|
||||
const logger_ =
|
||||
container.resolve(ContainerRegistrationKeys.LOGGER, {
|
||||
allowUnregistered: true,
|
||||
}) ?? logger
|
||||
|
||||
try {
|
||||
await moduleLoader({
|
||||
container,
|
||||
try {
|
||||
await moduleLoader({
|
||||
container,
|
||||
moduleResolutions,
|
||||
logger: logger_,
|
||||
migrationOnly,
|
||||
loaderOnly,
|
||||
})
|
||||
} catch (err) {
|
||||
errorLoading(err)
|
||||
throw err
|
||||
}
|
||||
|
||||
loadedModules.push({
|
||||
hashKey,
|
||||
modDeclaration,
|
||||
moduleResolutions,
|
||||
logger: logger_,
|
||||
migrationOnly,
|
||||
loaderOnly,
|
||||
container,
|
||||
finishLoading,
|
||||
})
|
||||
} catch (err) {
|
||||
errorLoading(err)
|
||||
throw err
|
||||
}
|
||||
|
||||
loadedModules.push({
|
||||
hashKey,
|
||||
modDeclaration,
|
||||
moduleResolutions,
|
||||
container,
|
||||
finishLoading,
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
if (loaderOnly) {
|
||||
loadedModules.forEach(({ finishLoading }) => finishLoading({}))
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
composeTableName,
|
||||
ContainerRegistrationKeys,
|
||||
Modules,
|
||||
promiseAll,
|
||||
simpleHash,
|
||||
toPascalCase,
|
||||
} from "@medusajs/framework/utils"
|
||||
@@ -42,130 +43,135 @@ export const initialize = async (
|
||||
pluginLinksDefinitions ?? []
|
||||
)
|
||||
|
||||
for (const linkDefinition of allLinksToLoad) {
|
||||
const definition: ModuleJoinerConfig = JSON.parse(
|
||||
JSON.stringify(linkDefinition)
|
||||
)
|
||||
|
||||
const [primary, foreign] = definition.relationships ?? []
|
||||
|
||||
if (definition.relationships?.length !== 2 && !definition.isReadOnlyLink) {
|
||||
throw new Error(
|
||||
`Link module ${definition.serviceName} can only link 2 modules.`
|
||||
await promiseAll(
|
||||
allLinksToLoad.map(async (linkDefinition) => {
|
||||
const definition: ModuleJoinerConfig = JSON.parse(
|
||||
JSON.stringify(linkDefinition)
|
||||
)
|
||||
} else if (
|
||||
foreign?.foreignKey?.split(",").length > 1 &&
|
||||
!definition.isReadOnlyLink
|
||||
) {
|
||||
throw new Error(`Foreign key cannot be a composed key.`)
|
||||
}
|
||||
|
||||
if (Array.isArray(definition.extraDataFields)) {
|
||||
const extraDataFields = definition.extraDataFields
|
||||
const definedDbFields = Object.keys(
|
||||
definition.databaseConfig?.extraFields || {}
|
||||
)
|
||||
const difference = arrayDifference(extraDataFields, definedDbFields)
|
||||
const [primary, foreign] = definition.relationships ?? []
|
||||
|
||||
if (difference.length) {
|
||||
if (
|
||||
definition.relationships?.length !== 2 &&
|
||||
!definition.isReadOnlyLink
|
||||
) {
|
||||
throw new Error(
|
||||
`extraDataFields (fieldNames: ${difference.join(
|
||||
","
|
||||
)}) need to be configured under databaseConfig (serviceName: ${
|
||||
definition.serviceName
|
||||
}).`
|
||||
`Link module ${definition.serviceName} can only link 2 modules.`
|
||||
)
|
||||
} else if (
|
||||
foreign?.foreignKey?.split(",").length > 1 &&
|
||||
!definition.isReadOnlyLink
|
||||
) {
|
||||
throw new Error(`Foreign key cannot be a composed key.`)
|
||||
}
|
||||
}
|
||||
|
||||
const serviceKey = !definition.isReadOnlyLink
|
||||
? definition.serviceName ??
|
||||
composeLinkName(
|
||||
primary.serviceName,
|
||||
primary.foreignKey,
|
||||
foreign.serviceName,
|
||||
foreign.foreignKey
|
||||
if (Array.isArray(definition.extraDataFields)) {
|
||||
const extraDataFields = definition.extraDataFields
|
||||
const definedDbFields = Object.keys(
|
||||
definition.databaseConfig?.extraFields || {}
|
||||
)
|
||||
: simpleHash(JSON.stringify(definition.extends))
|
||||
const difference = arrayDifference(extraDataFields, definedDbFields)
|
||||
|
||||
if (modulesLoadedKeys.includes(serviceKey)) {
|
||||
continue
|
||||
} else if (serviceKey in allLinks) {
|
||||
throw new Error(`Link module ${serviceKey} already defined.`)
|
||||
}
|
||||
|
||||
if (definition.isReadOnlyLink) {
|
||||
const extended: any[] = []
|
||||
for (const extension of definition.extends ?? []) {
|
||||
if (
|
||||
modulesLoadedKeys.includes(extension.serviceName) &&
|
||||
modulesLoadedKeys.includes(extension.relationship.serviceName)
|
||||
) {
|
||||
extended.push(extension)
|
||||
if (difference.length) {
|
||||
throw new Error(
|
||||
`extraDataFields (fieldNames: ${difference.join(
|
||||
","
|
||||
)}) need to be configured under databaseConfig (serviceName: ${
|
||||
definition.serviceName
|
||||
}).`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
definition.extends = extended
|
||||
if (extended.length === 0) {
|
||||
continue
|
||||
const serviceKey = !definition.isReadOnlyLink
|
||||
? definition.serviceName ??
|
||||
composeLinkName(
|
||||
primary.serviceName,
|
||||
primary.foreignKey,
|
||||
foreign.serviceName,
|
||||
foreign.foreignKey
|
||||
)
|
||||
: simpleHash(JSON.stringify(definition.extends))
|
||||
|
||||
if (modulesLoadedKeys.includes(serviceKey)) {
|
||||
return
|
||||
} else if (serviceKey in allLinks) {
|
||||
throw new Error(`Link module ${serviceKey} already defined.`)
|
||||
}
|
||||
} else if (
|
||||
!modulesLoadedKeys.includes(primary.serviceName) ||
|
||||
!modulesLoadedKeys.includes(foreign.serviceName)
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
const logger =
|
||||
injectedDependencies?.[ContainerRegistrationKeys.LOGGER] ?? console
|
||||
if (definition.isReadOnlyLink) {
|
||||
const extended: any[] = []
|
||||
for (const extension of definition.extends ?? []) {
|
||||
if (
|
||||
modulesLoadedKeys.includes(extension.serviceName) &&
|
||||
modulesLoadedKeys.includes(extension.relationship.serviceName)
|
||||
) {
|
||||
extended.push(extension)
|
||||
}
|
||||
}
|
||||
|
||||
definition.schema = generateGraphQLSchema(definition, primary, foreign, {
|
||||
logger,
|
||||
definition.extends = extended
|
||||
if (extended.length === 0) {
|
||||
return
|
||||
}
|
||||
} else if (
|
||||
!modulesLoadedKeys.includes(primary.serviceName) ||
|
||||
!modulesLoadedKeys.includes(foreign.serviceName)
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
const logger =
|
||||
injectedDependencies?.[ContainerRegistrationKeys.LOGGER] ?? console
|
||||
|
||||
definition.schema = generateGraphQLSchema(definition, primary, foreign, {
|
||||
logger,
|
||||
})
|
||||
|
||||
if (!Array.isArray(definition.alias)) {
|
||||
definition.alias = definition.alias ? [definition.alias] : []
|
||||
}
|
||||
|
||||
for (const alias of definition.alias) {
|
||||
alias.args ??= {}
|
||||
|
||||
alias.entity = toPascalCase(
|
||||
"Link_" +
|
||||
(definition.databaseConfig?.tableName ??
|
||||
composeTableName(
|
||||
primary.serviceName,
|
||||
primary.foreignKey,
|
||||
foreign.serviceName,
|
||||
foreign.foreignKey
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
const moduleDefinition = getLinkModuleDefinition(
|
||||
definition,
|
||||
primary,
|
||||
foreign
|
||||
) as ModuleExports
|
||||
|
||||
const linkModuleDefinition: LinkModuleDefinition = {
|
||||
key: serviceKey,
|
||||
label: serviceKey,
|
||||
dependencies: [Modules.EVENT_BUS],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
},
|
||||
}
|
||||
|
||||
const loaded = await MedusaModule.bootstrapLink({
|
||||
definition: linkModuleDefinition,
|
||||
declaration: options as InternalModuleDeclaration,
|
||||
moduleExports: moduleDefinition,
|
||||
injectedDependencies,
|
||||
})
|
||||
|
||||
allLinks[serviceKey as string] = Object.values(loaded)[0]
|
||||
})
|
||||
|
||||
if (!Array.isArray(definition.alias)) {
|
||||
definition.alias = definition.alias ? [definition.alias] : []
|
||||
}
|
||||
|
||||
for (const alias of definition.alias) {
|
||||
alias.args ??= {}
|
||||
|
||||
alias.entity = toPascalCase(
|
||||
"Link_" +
|
||||
(definition.databaseConfig?.tableName ??
|
||||
composeTableName(
|
||||
primary.serviceName,
|
||||
primary.foreignKey,
|
||||
foreign.serviceName,
|
||||
foreign.foreignKey
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
const moduleDefinition = getLinkModuleDefinition(
|
||||
definition,
|
||||
primary,
|
||||
foreign
|
||||
) as ModuleExports
|
||||
|
||||
const linkModuleDefinition: LinkModuleDefinition = {
|
||||
key: serviceKey,
|
||||
label: serviceKey,
|
||||
dependencies: [Modules.EVENT_BUS],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
},
|
||||
}
|
||||
|
||||
const loaded = await MedusaModule.bootstrapLink({
|
||||
definition: linkModuleDefinition,
|
||||
declaration: options as InternalModuleDeclaration,
|
||||
moduleExports: moduleDefinition,
|
||||
injectedDependencies,
|
||||
})
|
||||
|
||||
allLinks[serviceKey as string] = Object.values(loaded)[0]
|
||||
}
|
||||
)
|
||||
|
||||
return allLinks
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user