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`);
const init = services[name as keyof typeof services];
await init(app, server as https.Server, env.SERVICES);
const { default: init } = await services[name as keyof typeof services]();
await Promise.resolve(init(app, server as https.Server, env.SERVICES));
}
server.on("error", (err) => {
@@ -258,8 +258,11 @@ const isWebProcess =
env.SERVICES.includes("api") ||
env.SERVICES.includes("collaboration");
const isWorkerProcess =
env.SERVICES.length === 1 && env.SERVICES.includes("worker");
void throng({
master,
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";
import cron from "./cron";
import web from "./web";
import websockets from "./websockets";
import worker from "./worker";
export default {
websockets,
collaboration,
admin,
web,
worker,
cron,
/**
* Services are lazily imported so that only the modules for the services
* actually being run are loaded into memory. For example, a worker-only
* process does not need to import the web or collaboration services and their
* (often heavy) dependency trees.
*/
const services = {
websockets: () => import("./websockets"),
collaboration: () => import("./collaboration"),
admin: () => import("./admin"),
web: () => import("./web"),
worker: () => import("./worker"),
cron: () => import("./cron"),
} as const;
export default services;