Read number of workers and port from environment vars

This commit is contained in:
Peter Stockings
2025-07-26 12:40:08 +10:00
parent af1d9b0fb4
commit 30c73c5e01

View File

@@ -8,7 +8,9 @@ const States = Object.freeze({
// Create a pool of workers.
const workerPool: { worker: Worker; inUse: boolean }[] = [];
const numWorkers = navigator.hardwareConcurrency;
// Allow the number of workers to be configured via an environment variable.
const numWorkers =
parseInt(Deno.env.get("NUM_WORKERS") || "0") || navigator.hardwareConcurrency;
for (let i = 0; i < numWorkers; i++) {
const worker = new Worker(new URL("./worker.ts", import.meta.url).href, {
@@ -92,6 +94,8 @@ async function handler(req: Request): Promise<Response> {
}
}
console.log(`⚡ Deno server ready on :8000 with ${numWorkers} workers.`);
const port = parseInt(Deno.env.get("PORT") || "8000");
await serve(handler, { port: 8000 });
console.log(`⚡ Deno server ready on :${port} with ${numWorkers} workers.`);
await serve(handler, { port });