summaryrefslogtreecommitdiff
path: root/test/fixtures/clustered-server/app.js
blob: 16ac62fca847dc0ed04ecb68ade20088f29529c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'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.isMaster) {
  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);
}