Skip to content

Commit

Permalink
feat: supports passing workers count in config
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Nov 26, 2023
1 parent 146f80d commit e03328b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
4 changes: 1 addition & 3 deletions CoCreate.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ module.exports = {
"organization_id": "",
"key": "",
"host": "",
server: {
workers: 4, // Number of worker processes
},
workers: true, // Number of worker processes

"lazyload": {
openaii: {
Expand Down
60 changes: 35 additions & 25 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,46 @@
const cluster = require('cluster');
const http = require('http');
const os = require('os');
const cpu = 2
const config = require("@cocreate/config");

if (cluster.isMaster) {
const numCPUs = cpu || os.cpus().length;
console.log(`Master process is running with PID: ${process.pid}`);
console.log(`Forking ${numCPUs} workers...`);
const modules = require("./modules");

for (let i = 0; i < numCPUs; i++) {
// Forking the worker and passing the worker ID as an environment variable
const worker = cluster.fork({ WORKER_ID: i + 1 });
async function init() {
let workers = await config('workers')
workers = workers.workers
if (!workers || workers === 'false') {
workers = 1
} else
workers = workers = parseInt(workers) || os.cpus().length;

// Logging the path that could be used for routing
console.log(`Worker ${worker.process.pid} will handle path /worker${i + 1}`);
}
if (cluster.isMaster) {

console.log(`Master process is running with PID: ${process.pid}`);
console.log(`Forking ${workers} workers...`);

for (let i = 0; i < workers; i++) {
const worker = cluster.fork({ WORKER_ID: i + 1 });
console.log(`Worker ${worker.process.pid} will handle path /worker${i + 1}`);
}

cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died with code: ${code}, and signal: ${signal}`);
console.log('Starting a new worker');
cluster.fork();
});
} else {
// Each worker can use the WORKER_ID environment variable to determine its unique path
const workerId = process.env.WORKER_ID;
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died with code: ${code}, and signal: ${signal}`);
console.log('Starting a new worker');
cluster.fork();
});
} else {
// Each worker can use the WORKER_ID environment variable to determine its unique path
const workerId = process.env.WORKER_ID;

const server = http.createServer();
const server = http.createServer();

const modules = require("./modules");
modules.init(cluster, server);
cluster.totalWorkers = workers
modules.init(cluster, server);

server.listen(process.env.PORT || 3000, () => {
console.log(`Worker ${process.pid} (ID: ${workerId}) started, listening on PORT ${process.env.PORT || 3000}`);
});
server.listen(process.env.PORT || 3000, () => {
console.log(`Worker ${process.pid} (ID: ${workerId}) started, listening on PORT ${process.env.PORT || 3000}`);
});
}
}

init()

0 comments on commit e03328b

Please sign in to comment.