"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const path_1=__importDefault(require("path")),fs_1=__importDefault(require("fs")),envPaths=[path_1.default.resolve(process.cwd(),".env"),path_1.default.resolve(__dirname,"../.env"),path_1.default.resolve(__dirname,".env"),path_1.default.resolve(process.cwd(),"../.env")];let envLoaded=!1;for(const e of envPaths)if(fs_1.default.existsSync(e)){require("dotenv").config({path:e});console.log(`[32mEnvironment loaded from: ${e}[0m`);envLoaded=!0;break}if(!envLoaded){console.warn(`[33mWarning: No .env file found. Tried paths: ${envPaths.join(", ")}[0m`);require("dotenv").config()}const worker_threads_1=require("worker_threads"),src_1=require("./src"),port=Number(process.env.NEXT_PUBLIC_BACKEND_PORT)||4e3,threads=Number(process.env.NEXT_PUBLIC_BACKEND_THREADS)||2;if(worker_threads_1.isMainThread){const e=new src_1.MashServer;e.listen(port,()=>{console.log(`Main Thread: listening on port ${port} (thread ${worker_threads_1.threadId})`)});const r=require("os").cpus().length;threads>r&&console.warn(`WARNING: Number of threads (${threads}) is greater than the number of CPUs (${r})`);const o=Math.min(threads,r);for(let r=0;r<o;r++){const o=new worker_threads_1.Worker(path_1.default.resolve(__dirname,"backend","worker.ts"),{execArgv:["-r","ts-node/register","-r","module-alias/register"],workerData:{port:4001+r}});o.on("message",r=>{e.addChildAppDescriptor(r)});o.on("error",e=>{console.error(`Error in worker ${r}:`,e)});o.on("exit",e=>{0!==e&&console.error(`Worker ${r} stopped with exit code ${e}`)})}}