2024-02-22 17:08:09 +07:00
|
|
|
"use strict";
|
2024-05-10 13:18:05 +07:00
|
|
|
|
2024-05-10 19:08:00 +07:00
|
|
|
process.title = "Bostr (keeper)";
|
2024-05-10 13:18:05 +07:00
|
|
|
|
2024-02-19 18:37:50 +07:00
|
|
|
const config = require(process.env.BOSTR_CONFIG_PATH || "./config");
|
2024-06-02 11:50:39 +07:00
|
|
|
|
|
|
|
if (typeof(Bun) === "object") {
|
|
|
|
console.log("You are running Bostr with Bun runtime.");
|
|
|
|
console.log("Clustering will not work, But worker thread will continue to work.");
|
|
|
|
return require("./http.js");
|
|
|
|
}
|
|
|
|
|
2024-06-05 19:57:17 +07:00
|
|
|
const cluster = require("node:cluster");
|
|
|
|
const fs = require("node:fs");
|
|
|
|
const os = require("node:os");
|
2023-10-30 03:14:50 +07:00
|
|
|
|
|
|
|
if (!process.env.NO_CLUSTERS && cluster.isPrimary) {
|
2023-10-31 14:16:43 +07:00
|
|
|
const numClusters = process.env.CLUSTERS || config.clusters || (os.availableParallelism ? os.availableParallelism() : (os.cpus().length || 2))
|
2023-10-30 03:14:50 +07:00
|
|
|
|
|
|
|
console.log(`Primary ${process.pid} is running. Will fork ${numClusters} clusters.`);
|
|
|
|
|
|
|
|
// Fork workers.
|
|
|
|
for (let i = 0; i < numClusters; i++) {
|
|
|
|
cluster.fork();
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster.on('exit', (worker, code, signal) => {
|
2024-06-02 11:50:39 +07:00
|
|
|
console.log(`Cluster process ${worker.process.pid} died. Forking another one....`);
|
2023-10-30 03:14:50 +07:00
|
|
|
cluster.fork();
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
require("./http.js");
|