From ebc2bb28bfa9ea6be1eeb60bf2dcc4af8feae257 Mon Sep 17 00:00:00 2001 From: Avior Date: Tue, 10 Dec 2024 13:34:57 +0100 Subject: [PATCH] feat: Allow to customize number of workers (#616) --- server/src/index.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/server/src/index.ts b/server/src/index.ts index 15b3fb206..a28261d74 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -7,11 +7,19 @@ import jsonEndpoints from './V2/endpoints/jsonEndpoints' import graphql from './V2/graphql' if (cluster.isPrimary) { - console.log(`Primary ${process.pid} is running`); + console.log(`Primary ${process.pid} is running`) - const parallelism = availableParallelism() - console.log(`creating ${parallelism} workers...`) - for (let i = 0; i < parallelism; i++) { + // get maximum number of workers available for the software + let maxWorkers = availableParallelism() + + // allow to override max worker count + if (process.env.MAX_WORKERS) { + maxWorkers = Math.min(parallelism, parseInt(process.env.MAX_WORKERS)) + } + + // create the workers + console.log(`creating ${maxWorkers} workers...`) + for (let i = 0; i < maxWorkers; i++) { cluster.fork(); }