chore: cleanup/improve bootstrap (#14023)
* chore: cleanup/improve bootstrap * chore: cleanup/improve bootstrap * Create few-guests-visit.md
This commit is contained in:
committed by
GitHub
parent
213c344804
commit
7e3eb6e413
7
.changeset/few-guests-visit.md
Normal file
7
.changeset/few-guests-visit.md
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
"@medusajs/framework": patch
|
||||||
|
"@medusajs/modules-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore: cleanup/improve bootstrap
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ContainerRegistrationKeys, parseCorsOrigins } from "@medusajs/utils"
|
import { ContainerRegistrationKeys, parseCorsOrigins, promiseAll } from "@medusajs/utils"
|
||||||
import cors, { CorsOptions } from "cors"
|
import cors, { CorsOptions } from "cors"
|
||||||
import type { ErrorRequestHandler, Express, RequestHandler } from "express"
|
import type { ErrorRequestHandler, Express, RequestHandler } from "express"
|
||||||
import type {
|
import type {
|
||||||
@@ -85,10 +85,12 @@ export class ApiLoader {
|
|||||||
const routesLoader = new RoutesLoader()
|
const routesLoader = new RoutesLoader()
|
||||||
const middlewareLoader = new MiddlewareFileLoader()
|
const middlewareLoader = new MiddlewareFileLoader()
|
||||||
|
|
||||||
for (let dir of this.#sourceDirs) {
|
await promiseAll(
|
||||||
await routesLoader.scanDir(dir)
|
this.#sourceDirs.flatMap(dir => [
|
||||||
await middlewareLoader.scanDir(dir)
|
routesLoader.scanDir(dir),
|
||||||
}
|
middlewareLoader.scanDir(dir)
|
||||||
|
])
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
routes: routesLoader.getRoutes(),
|
routes: routesLoader.getRoutes(),
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Logger, MedusaContainer, ModuleResolution } from "@medusajs/types"
|
import { Logger, MedusaContainer, ModuleResolution } from "@medusajs/types"
|
||||||
|
import { promiseAll } from "@medusajs/utils"
|
||||||
import { asValue } from "@medusajs/deps/awilix"
|
import { asValue } from "@medusajs/deps/awilix"
|
||||||
import { EOL } from "os"
|
import { EOL } from "os"
|
||||||
import { MODULE_SCOPE } from "../types"
|
import { MODULE_SCOPE } from "../types"
|
||||||
@@ -17,23 +18,22 @@ export const moduleLoader = async ({
|
|||||||
migrationOnly?: boolean
|
migrationOnly?: boolean
|
||||||
loaderOnly?: boolean
|
loaderOnly?: boolean
|
||||||
}): Promise<void> => {
|
}): Promise<void> => {
|
||||||
for (const resolution of Object.values(moduleResolutions ?? {})) {
|
const resolutions = Object.values(moduleResolutions ?? {})
|
||||||
const registrationResult = await loadModule(
|
const results = await promiseAll(
|
||||||
container,
|
resolutions.map((resolution) =>
|
||||||
resolution,
|
loadModule(container, resolution, logger!, migrationOnly, loaderOnly)
|
||||||
logger!,
|
|
||||||
migrationOnly,
|
|
||||||
loaderOnly
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
results.forEach((registrationResult, idx) => {
|
||||||
if (registrationResult?.error) {
|
if (registrationResult?.error) {
|
||||||
const { error } = registrationResult
|
const { error } = registrationResult
|
||||||
logger?.error(
|
logger?.error(
|
||||||
`Could not resolve module: ${resolution.definition.label}. Error: ${error.message}${EOL}`
|
`Could not resolve module: ${resolutions[idx].definition.label}. Error: ${error.message}${EOL}`
|
||||||
)
|
)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadModule(
|
async function loadModule(
|
||||||
|
|||||||
@@ -515,25 +515,29 @@ class MedusaModule {
|
|||||||
return [{}]
|
return [{}]
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const {
|
const resolvedServices = await promiseAll(
|
||||||
hashKey,
|
loadedModules.map(async ({
|
||||||
modDeclaration,
|
|
||||||
moduleResolutions,
|
|
||||||
container,
|
|
||||||
finishLoading,
|
|
||||||
} of loadedModules) {
|
|
||||||
const service = await MedusaModule.resolveLoadedModule({
|
|
||||||
hashKey,
|
hashKey,
|
||||||
modDeclaration,
|
modDeclaration,
|
||||||
moduleResolutions,
|
moduleResolutions,
|
||||||
container,
|
container,
|
||||||
})
|
finishLoading,
|
||||||
|
}) => {
|
||||||
|
const service = await MedusaModule.resolveLoadedModule({
|
||||||
|
hashKey,
|
||||||
|
modDeclaration,
|
||||||
|
moduleResolutions,
|
||||||
|
container,
|
||||||
|
})
|
||||||
|
|
||||||
MedusaModule.instances_.set(hashKey, service)
|
MedusaModule.instances_.set(hashKey, service)
|
||||||
finishLoading(service)
|
finishLoading(service)
|
||||||
MedusaModule.loading_.delete(hashKey)
|
MedusaModule.loading_.delete(hashKey)
|
||||||
services.push(service)
|
return service
|
||||||
}
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
services.push(...resolvedServices)
|
||||||
|
|
||||||
return services
|
return services
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -200,6 +200,8 @@ async function start(args: {
|
|||||||
skipDbConnection: true,
|
skipDbConnection: true,
|
||||||
})
|
})
|
||||||
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
|
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
|
||||||
|
const serverActivity = logger.activity(`Creating server`)
|
||||||
|
|
||||||
await registerInstrumentation(directory)
|
await registerInstrumentation(directory)
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
@@ -259,8 +261,6 @@ async function start(args: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const serverActivity = logger.activity(`Creating server`)
|
|
||||||
|
|
||||||
// Register a health check endpoint. Ideally this also checks the readiness of the service, rather than just returning a static response.
|
// Register a health check endpoint. Ideally this also checks the readiness of the service, rather than just returning a static response.
|
||||||
app.get("/health", (_, res) => {
|
app.get("/health", (_, res) => {
|
||||||
res.status(200).send("OK")
|
res.status(200).send("OK")
|
||||||
|
|||||||
@@ -92,8 +92,10 @@ async function loadEntrypoints(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (shouldLoadBackgroundProcessors(configModule)) {
|
if (shouldLoadBackgroundProcessors(configModule)) {
|
||||||
await subscribersLoader(plugins, container)
|
await promiseAll([
|
||||||
await jobsLoader(plugins, container)
|
subscribersLoader(plugins, container),
|
||||||
|
jobsLoader(plugins, container),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isWorkerMode(configModule)) {
|
if (isWorkerMode(configModule)) {
|
||||||
@@ -119,17 +121,18 @@ async function loadEntrypoints(
|
|||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
const { shutdown } = await expressLoader({
|
const [{ shutdown }] = await promiseAll([
|
||||||
app: expressApp,
|
expressLoader({
|
||||||
container,
|
app: expressApp,
|
||||||
})
|
container,
|
||||||
|
}),
|
||||||
await adminLoader({ app: expressApp, configModule, rootDirectory, plugins })
|
adminLoader({ app: expressApp, configModule, rootDirectory, plugins }),
|
||||||
await apiLoader({
|
apiLoader({
|
||||||
container,
|
container,
|
||||||
plugins,
|
plugins,
|
||||||
app: expressApp,
|
app: expressApp,
|
||||||
})
|
}),
|
||||||
|
])
|
||||||
|
|
||||||
return shutdown
|
return shutdown
|
||||||
}
|
}
|
||||||
@@ -140,10 +143,8 @@ export async function initializeContainer(
|
|||||||
skipDbConnection?: boolean
|
skipDbConnection?: boolean
|
||||||
}
|
}
|
||||||
): Promise<MedusaContainer> {
|
): Promise<MedusaContainer> {
|
||||||
// custom flags from medusa project
|
|
||||||
await featureFlagsLoader(rootDirectory)
|
await featureFlagsLoader(rootDirectory)
|
||||||
const configDir = await configLoader(rootDirectory, "medusa-config")
|
const configDir = await configLoader(rootDirectory, "medusa-config")
|
||||||
// core flags
|
|
||||||
await featureFlagsLoader(join(__dirname, ".."))
|
await featureFlagsLoader(join(__dirname, ".."))
|
||||||
|
|
||||||
const customLogger = configDir.logger ?? defaultLogger
|
const customLogger = configDir.logger ?? defaultLogger
|
||||||
|
|||||||
Reference in New Issue
Block a user