node/test/fixtures/clustered-server/app.js
Michael Dawson 15164cebce lib,src: update cluster to use Parent
Doc deprecate isMaster and setupMaster in favor
of isPrimary and setupPrimary.

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: https://github.com/nodejs/node/pull/36478
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Beth Griggs <bgriggs@redhat.com>
2021-01-05 15:41:45 -05:00

37 lines
822 B
JavaScript

'use strict';
const http = require('http');
const cluster = require('cluster');
function handleRequest(request, response) {
response.end('hello world\n');
}
const NUMBER_OF_WORKERS = 2;
var workersOnline = 0;
if (cluster.isPrimary) {
cluster.on('online', function() {
if (++workersOnline === NUMBER_OF_WORKERS) {
console.error('all workers are running');
}
});
process.on('message', function(msg) {
if (msg.type === 'getpids') {
const pids = [];
pids.push(process.pid);
for (var key in cluster.workers)
pids.push(cluster.workers[key].process.pid);
process.send({ type: 'pids', pids: pids });
}
});
for (var i = 0; i < NUMBER_OF_WORKERS; i++) {
cluster.fork();
}
} else {
const server = http.createServer(handleRequest);
server.listen(0);
}