-
Notifications
You must be signed in to change notification settings - Fork 49
/
server.js
55 lines (45 loc) · 1.33 KB
/
server.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
var logger = require('logfmt');
var cpus = require('os').cpus().length;
var http = require('http');
var throng = require('throng');
var config = require('./config');
var app = require('./app');
var web = require('./web');
http.globalAgent.maxSockets = Infinity;
throng(start, { workers: config.concurrency });
function start() {
logger.log({
type: 'info',
msg: 'starting server',
concurrency: config.concurrency,
thrifty: config.thrifty,
timeout: config.timeout,
busy_ms: config.busy_ms
});
var instance = app(config);
instance.on('ready', createServer);
instance.on('lost', abort);
function createServer() {
if (config.thrifty) instance.startScraping();
var server = http.createServer(web(instance, config));
process.on('SIGTERM', shutdown);
instance
.removeListener('lost', abort)
.on('lost', shutdown);
server.listen(config.port, onListen);
function onListen() {
logger.log({ type: 'info', msg: 'listening', port: server.address().port });
}
function shutdown() {
logger.log({ type: 'info', msg: 'shutting down' });
server.close(function() {
logger.log({ type: 'info', msg: 'exiting' });
process.exit();
});
}
}
function abort() {
logger.log({ type: 'info', msg: 'shutting down', abort: true });
process.exit();
}
}