forked from CoderDojo/cp-zen-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
128 lines (110 loc) · 3.6 KB
/
service.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
var TITLE = 'cp-zen-platform';
process.env.component = TITLE;
process.setMaxListeners(0);
require('events').EventEmitter.prototype._maxListeners = 100;
var util = require('util');
var cluster = require('cluster');
var heapdump = require('heapdump');
var server;
// Show 'starting' message
var workerId = process.env.NODE_WORKER_ID || 0;
var starting = "Starting " + TITLE;
if (cluster.isWorker) starting += " Worker Id: " + cluster.worker.id + " pid: " + process.pid;
else starting = starting + ' Master: ' + process.pid;
console.log(starting);
// handle uncaught exceptions
process.on('uncaughtException', function (err) {
console.error(new Date().toString() + " FATAL: UncaughtException, please report: " + util.inspect(err));
if (err != undefined && err.stack != undefined) {
console.error(util.inspect(err.stack));
}
console.trace();
cleanShutdown(); // exit on uncaught exception
});
process.on('SIGUSR2', function() {
var snapshot = '/tmp/cp-zen-platform-' + Date.now() + '.heapsnapshot'
console.log('Got SIGUSR2, creating heap snapshot: ', snapshot);
heapdump.writeSnapshot(snapshot, function(err, filename) {
if (err) console.error('Error creating snapshot:', err);
console.log('dump written to', filename);
});
});
// Array of Worker processes
var workers = [];
// clean shut down - note cb is optional here (used in testsuite)
var cleanShutdown = function(cb) {
if (cluster.isMaster) {
// shutdown all our workers - exit when all workers have exited..
console.log("Master, got shutdown signal, shutting down workers..");
for (var i = 0; i < workers.length; i++) {
var worker = workers[i];
if (worker.destroy) worker.destroy();
else if (worker.kill) worker.kill();
else if (worker.process && worker.process.kill) worker.process.kill();
}
}else {
console.log("Worker: " + cluster.worker.id + ' exiting');
process.exit(0);
}
};
// handle process signals
process.on('SIGTERM', cleanShutdown);
process.on('SIGHUP', cleanShutdown);
process.on('INT', cleanShutdown);
// start worker
function startWorker() {
var index = require('./web/index.js');
index.start();
};
// start: note we use one master and one worker, so any uncaught exceptions in worker
// will result in the worker process being restarted by the master.
function start() {
if (cluster.isMaster) {
var numCPUs = require('os').cpus().length;
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
var worker = cluster.fork();
workers.push(worker);
}
// Handle workers exiting
cluster.on('exit', function (worker, code, signal) {
if (worker.suicide === true) {
console.log("Cleanly exiting..");
process.exit(0);
} else {
var msg = "Worker: " + worker.process.pid + " has died!! Respawning..";
console.error(msg);
var newWorker = cluster.fork();
for (var i = 0; i < workers.length; i++) {
if (workers[i] && workers[i].id === worker.id) workers.splice(i);
}
workers.push(newWorker);
}
});
} else {
startWorker();
}
};
start();
/*
var pm2 = require('pm2');
pm2.connect(function() {
pm2.start({
script : 'web/index.js', // Script to be run
exec_mode : 'cluster', // Allow your app to be clustered
instances : 0
}, function(err, apps) {
if (err) return console.error(err);
pm2.streamLogs('all', 0, false, 'HH:mm:ss', false);
});
});
process.on('SIGINT', function() {
console.log('Got SIGINT, exiting');
pm2.kill();
});
process.on('SIGTERM', function() {
console.log('Got SIGTERM, exiting');
pm2.kill();
});
*/