-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
120 lines (98 loc) · 3.05 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
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
'use strict';
process.title = 'pending-dns';
// cache before wild-config
const argv = process.argv.slice(2);
const config = require('wild-config');
const logger = require('./lib/logger').child({ component: 'server' });
const pathlib = require('path');
const packageData = require('./package.json');
const { Worker, SHARE_ENV } = require('worker_threads');
const { isemail } = require('./lib/tools');
if (!config.acme || !isemail(config.acme.email)) {
console.error('"acme.email" configuration value is not set or is not a valid email address');
process.exit(51);
}
const Bugsnag = require('@bugsnag/js');
if (process.env.BUGSNAG_API_KEY) {
Bugsnag.start({
apiKey: process.env.BUGSNAG_API_KEY,
appVersion: packageData.version,
logger: {
debug(...args) {
logger.debug({ msg: args.shift(), worker: 'main', source: 'bugsnag', args: args.length ? args : undefined });
},
info(...args) {
logger.debug({ msg: args.shift(), worker: 'main', source: 'bugsnag', args: args.length ? args : undefined });
},
warn(...args) {
logger.warn({ msg: args.shift(), worker: 'main', source: 'bugsnag', args: args.length ? args : undefined });
},
error(...args) {
logger.error({ msg: args.shift(), worker: 'main', source: 'bugsnag', args: args.length ? args : undefined });
}
}
});
logger.notifyError = Bugsnag.notify.bind(Bugsnag);
}
let closing = false;
let workers = new Map();
let spawnWorker = type => {
if (closing) {
return;
}
if (!workers.has(type)) {
workers.set(type, new Set());
}
let worker = new Worker(pathlib.join(__dirname, 'workers', `${type}.js`), {
argv,
env: SHARE_ENV
});
workers.get(type).add(worker);
worker.on('exit', exitCode => {
workers.get(type).delete(worker);
if (closing) {
return;
}
// spawning a new worker trigger reassign
logger.error({ msg: 'Worker exited', exitCode });
setTimeout(() => spawnWorker(type), 1000);
});
};
if (config.api.enabled) {
spawnWorker('api');
}
// DNS server
if (config.dns.enabled) {
spawnWorker('dns');
}
// Public HTTP/HTTPS server
if (config.public.enabled) {
spawnWorker('public');
}
// Healthchecks
if (config.health.enabled) {
spawnWorker('health');
}
const closeProcess = (code, errType, err) => {
if (closing) {
return;
}
closing = true;
if (!code) {
return setTimeout(() => {
process.exit(code);
}, 10);
}
logger.fatal({
msg: errType,
_msg: errType,
err
});
if (!logger.notifyError) {
setTimeout(() => process.exit(code), 10);
}
};
process.on('uncaughtException', err => closeProcess(1, 'uncaughtException', err));
process.on('unhandledRejection', err => closeProcess(2, 'unhandledRejection', err));
process.on('SIGTERM', () => closeProcess(0));
process.on('SIGINT', () => closeProcess(0));