I'm implementing cluster module of nodeJS on serverless. cluster couldn't listen serverless port so after running serverless I'm getting following issue
offline: Failure: offline: handler 'handler' in /home/Documents/cluster is not a function
Error: offline: handler 'handler' in /home/Documents/cluster is not a function
at InProcessRunner.run (/home/Documents/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:160:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async LambdaFunction.runHandler (/home/Documents/node_modules/serverless-offline/dist/lambda/LambdaFunction.js:368:20)
at async hapiHandler (/home/Documents/node_modules/serverless-offline/dist/events/http/HttpServer.js:702:18)
at async exports.Manager.execute (/home/Documents/node_modules/@hapi/hapi/lib/toolkit.js:60:28)
at async Object.internals.handler (/home/Documents/node_modules/@hapi/hapi/lib/handler.js:46:20)
at async exports.execute (/home/Documents/node_modules/@hapi/hapi/lib/handler.js:31:20)
at async Request._lifecycle (/home/Documents/node_modules/@hapi/hapi/lib/request.js:371:32)
at async Request._execute (/home/Documents/node_modules/@hapi/hapi/lib/request.js:281:9)
following is my cluster
cluster.js
const cluster = require("cluster");
const totalCPUs = require("os").cpus().length;
const serverless = require("serverless-http");
const app = require("./api");
console.log("cluster.isMaster", cluster.isMaster);
if (cluster.isMaster) {
console.log(`Number of CPUs is ${totalCPUs}`);
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < totalCPUs; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
// console.log(`process killed:`, worker.kill());
console.log(`${worker.process.pid} is killed`);
// console.log("code, signal ", code, signal);
cluster.fork();
// console.log(`new proccess pid: ${process.pid}`);
});
} else {
module.exports.handler = serverless(app);
}
app.js
const express = require("express");
const app = express();
// require("dotenv").config()
const cluster = require("cluster");
const totalCPUs = require("os").cpus().length;
app.get("/", (req, res) => {
console.log(`Hello World! - on process ${process.pid}`);
res.send("Hello World!");
// process.exit(0);
});
module.export = app
after all this it automatically kill the all the process.
- 15947 is killed
- 15961 is killed
- 15968 is killed
- 15954 is killed
How I can integrate cluster in serverless.