perf: Avoid loading unused services (#12537)

* fix: Run single process when only the worker service is enabled

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* perf: Improve memory consumption through lazy service loading

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Tom Moor
2026-05-30 16:48:31 -04:00
committed by GitHub
parent cecc9ef576
commit b189c308e5
2 changed files with 21 additions and 17 deletions
+6 -3
View File
@@ -184,8 +184,8 @@ async function start(_id: number, disconnect: () => void) {
} }
Logger.info("lifecycle", `Starting ${name} service`); Logger.info("lifecycle", `Starting ${name} service`);
const init = services[name as keyof typeof services]; const { default: init } = await services[name as keyof typeof services]();
await init(app, server as https.Server, env.SERVICES); await Promise.resolve(init(app, server as https.Server, env.SERVICES));
} }
server.on("error", (err) => { server.on("error", (err) => {
@@ -258,8 +258,11 @@ const isWebProcess =
env.SERVICES.includes("api") || env.SERVICES.includes("api") ||
env.SERVICES.includes("collaboration"); env.SERVICES.includes("collaboration");
const isWorkerProcess =
env.SERVICES.length === 1 && env.SERVICES.includes("worker");
void throng({ void throng({
master, master,
worker: start, worker: start,
count: isWebProcess ? webProcessCount : undefined, count: isWorkerProcess ? 1 : isWebProcess ? webProcessCount : undefined,
}); });
+15 -14
View File
@@ -1,15 +1,16 @@
import admin from "./admin"; /**
import collaboration from "./collaboration"; * Services are lazily imported so that only the modules for the services
import cron from "./cron"; * actually being run are loaded into memory. For example, a worker-only
import web from "./web"; * process does not need to import the web or collaboration services and their
import websockets from "./websockets"; * (often heavy) dependency trees.
import worker from "./worker"; */
const services = {
export default { websockets: () => import("./websockets"),
websockets, collaboration: () => import("./collaboration"),
collaboration, admin: () => import("./admin"),
admin, web: () => import("./web"),
web, worker: () => import("./worker"),
worker, cron: () => import("./cron"),
cron,
} as const; } as const;
export default services;