feat: Improve startup time by parallelizing module and link loading (#12731)
This commit is contained in:
@@ -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({}))
|
||||
|
||||
Reference in New Issue
Block a user