-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
85 lines (68 loc) · 2.21 KB
/
app.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
/**
* GRTVPlaysTurtle -> Server -> App
*/
"use strict";
var config = require("./config.json"),
cluster = require("cluster"),
numCPUs = require("os").cpus().length,
chalk = require("chalk"),
winston = require("winston"),
l = {},
colors = ["green", "yellow", "blue", "magenta", "cyan", "gray", "white", "dim"];
winston.add(winston.transports.File, { filename: "logs/all.log"})
l.master = winston.loggers.add("master", {
console: {
colorize: "true",
label: chalk.red("Master")
},
file: {
filename: "logs/master.log"
}
});
if(cluster.isMaster) {
var timeouts = [];
var workerCount = config.workerCount === "CPU count" ? numCPUs : config.workerCount;
cluster.setupMaster({
silent: true,
exec: "lib/worker.js"
});
cluster.on("fork", function(worker) {
l.master.info("forked #" + worker.id);
timeouts[worker.id] = setTimeout(l.master.warn.bind(l.master, "worker #" + worker.id + " is not listening..."), 2000);
});
cluster.on("listening", function(worker) {
l.master.info("worker #" + worker.id + " is listening");
clearTimeout(timeouts[worker.id]);
});
cluster.on("exit", function(worker) {
l.master.info("worker #" + worker.id + " died");
});
l.master.info(chalk.italic("Starting", chalk.bold(workerCount), "workers"));
for (var i = 0; i < workerCount; i++) {
cluster.fork();
}
if(cluster.settings.silent) {
for(var j in cluster.workers) {
l["worker" + j] = winston.loggers.add("worker" + j, {
console: {
colorize: true,
label: chalk[colors[j % colors.length]]("Wrk #" + j)
},
file: {
filename: "logs/worker-" + j + ".log"
}
});
cluster.workers[j].process.stdout.on("data", function(id, chunk) {
var str = chunk.toString();
l["worker" + id].info(str.substring(0, str.length - 1));
}.bind(cluster.workers[j].process.stderr, j));
cluster.workers[j].process.stderr.on("data", function(id, chunk) {
var str = chunk.toString();
l["worker" + id].warn(str.substring(0, str.length - 1));
}.bind(cluster.workers[j].process.stderr, j));
}
}
}
else {
console.log(chalk.error("I used to be the master."));
}