-
Notifications
You must be signed in to change notification settings - Fork 2
/
dispatch.js
55 lines (44 loc) · 1.37 KB
/
dispatch.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
47
48
49
50
51
52
53
54
55
/*!
* resonate - dispatch.js
* Copyright(c) 2013
* Author: karen <karenpenglabs@gmail.com>
*/
'use strict';
/**
* Module dependencies.
*/
var path = require('path');
var util = require('util');
var fs = require('fs');
var cluster = require('cluster');
var config = require('./config');
if (config.enableCluster) {
var workerPath = path.join(__dirname, 'worker.js');
var restartTime = 5000;
cluster.setupMaster({
exec: workerPath
});
cluster.on('fork', function (worker) {
console.log('[%s] [worker:%d] new worker start', new Date(), worker.process.pid);
});
cluster.on('disconnect', function (worker) {
var w = cluster.fork();
console.error('[%s] [master:%s] wroker:%s disconnect! new worker:%s fork',
new Date(), process.pid, worker.process.pid, w.process.pid);
});
cluster.on('exit', function (worker, code, signal) {
var exitCode = worker.process.exitCode;
var err = new Error(util.format('worker %s died (code: %s, signal: %s)', worker.process.pid, exitCode, signal));
err.name = 'WorkerDiedError';
console.error(err);
});
var numCPUs = require('os').cpus().length;
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
require('./worker');
}
console.log('[%s] [master:%d] Server started, listen at %d, cluster: %s',
new Date(), process.pid, config.webPort, config.enableCluster);