-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.js
More file actions
69 lines (57 loc) · 1.64 KB
/
Copy pathcluster.js
File metadata and controls
69 lines (57 loc) · 1.64 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const http = require('http');
const cluster = require('cluster');
const { cpus } = require('os');
const heavyCommand = () => {
const list = new Array(1e6);
for (let k = 0; k < list.length; k++) {
list[k] = k * Math.floor(Math.random() * 1000);
}
if(process.send) {
process.send(`List is created on worker process id ${process.pid}`);
} else {
console.log('List created without workers');
}
return process.pid;
}
const workers = [];
const setupWorkerProcesses = () => {
const numCores = cpus().length;
console.log(`Master cluster setting up ${numCores} workers`);
for (let i = 0; i < numCores; i++) {
workers.push(cluster.fork());
workers[i].on('message', msg => {
console.log(msg);
});
}
cluster.on('online', worker => {
console.log(`Worker ${worker.process.pid} is listening`);
});
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');
workers.push(cluster.fork());
workers[workers.length - 1].on('message', msg => {
console.log(msg);
});
});
};
const setupRouter = () => {
const server = http.createServer((req, res) => {
if (req.url === '/' && req.method === 'GET') {
const pid = heavyCommand();
res.writeHead(200, { 'Content-Type': 'text/html'});
res.end(`Worker Process Id ${pid}`);
}
});
server.listen(8000, () => {
console.log('Server started');
});
};
const setupServer = isClustered => {
if (isClustered && cluster.isMaster) {
setupWorkerProcesses();
} else {
setupRouter();
}
}
setupServer(false);