forked from Schibsted-Tech-Polska/stp.phantom-renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (86 loc) · 2.73 KB
/
index.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
var cluster = require('cluster'),
Server = require('./server'),
logger = require('./logger').logger,
config = require('./config'),
argv = require('minimist')(process.argv.slice(2)),
httpProxy = require('http-proxy'),
getPort = require('get-port'),
http = require('http'),
_ = require('lodash'),
i, server, proxy, port, url, workerServers = [];
if(argv.port) {
port = argv.port;
}
else {
logger.error('Please provide port with "--port" option');
process.exit();
}
if(argv.port) {
url = argv.url;
}
else {
logger.error('Please provide url with "--url" option');
process.exit();
}
function spawnChild() {
getPort(function (err, port) {
var worker = cluster.fork();
workerServers.push({id: worker.id, port: port});
worker.send({port: port});
});
}
if(cluster.isMaster) {
for (i = 0; i < (config.workers || 2); i += 1) {
logger.info('Starting worker thread #' + (i + 1));
spawnChild();
}
cluster.on('exit', function (worker) {
logger.info('Worker ' + worker.id + ' died.');
_.remove(workerServers, function(element){
return element.id === worker.id;
});
// spin up another to replace it
logger.info('Restarting worker thread...');
spawnChild();
});
proxy = httpProxy.createProxyServer({});
server = http.createServer(function(req, res) {
var workerServer = workerServers.shift();
workerServers.push(workerServer);
proxy.web(req, res, {target: 'http://127.0.0.1:' + workerServer.port});
});
proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong');
});
logger.info('Master listening on port ' + argv.port);
server.listen(argv.port);
} else {
logger.info('Worker ' + cluster.worker.id + ' started');
logger.setProcessInfo('Worker id #' + cluster.worker.id);
process.on('message', function(msg) {
var port = msg.port;
server = new Server({
logger: logger,
url: url,
port: port,
workerId: cluster.worker.id,
blacklistedDomains: config.blacklistedDomains,
pageRequestsBeforeRespawn: config.pageRequestsBeforeRespawn,
page404meta: config.page404meta
});
server.start();
});
process.on('uncaughtException', function( err ) {
console.error('Process uncaughtException');
console.error(err.stack);
});
process.on("exit", function() {
if(server && server.phantom) {
logger.info('Process exit - dispose phantom');
server.phantom.dispose();
}
});
}