-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.js
46 lines (35 loc) · 1.16 KB
/
line.js
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
/*
* file: line.js multithread simulation
* Copyright (c) 2014, Ranchao Zhang & Zhihao Ni. All rights reserved.
*/
"use strict";
var i, _i;
var cluster = require('cluster');
var colors = require('colors');
var dot = require('./dot');
var instances = {};
var numCPUs = require('os').cpus().length;
var spawn = function() {
var instance = cluster.fork();
instances[instance.pid] = instance;
return instance;
};
if (cluster.isMaster) {
var nodeEnv = process.env.NODE_ENV;
var isProd = (nodeEnv === 'production') || (nodeEnv === 'nginx');
if (isProd) {
console.log('Please make sure you have configured and started database server.'.yellow)
}
console.log('Cluster ' + process.pid + ' spawning instances' +
(isProd ? ' in production mode' : ' in development mode'));
for (i = _i = 0; 0 <= numCPUs ? _i <= numCPUs : _i >= numCPUs; i = 0 <= numCPUs ? ++_i : --_i) {
spawn();
}
cluster.on('death', function(instance) {
console.log('Instance ' + instance.pid + ' died. Spawning a new process.');
delete instances[instance.pid];
return spawn();
});
} else {
instance.startServer();
}