forked from AstroX11/Xstro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (53 loc) · 1.37 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
import http from 'http';
import { config } from 'dotenv';
import { DATABASE } from '#database';
import { client, eventlogger, initSession, loadPlugins } from '#lib';
import cluster from 'cluster';
import { sessionData } from '#config';
config();
if (cluster.isMaster) {
let isRestarting = false;
const createWorker = () => {
const worker = cluster.fork();
worker.on('message', message => {
if (message === 'app.kill') {
console.log('Shutting down Xstro...');
worker.kill();
process.exit(0);
} else if (message === 'restart') {
console.log('Restarting...');
isRestarting = true;
worker.kill();
}
});
worker.on('exit', () => {
if (!isRestarting) console.log('Restarting...');
isRestarting = false;
createWorker();
});
};
createWorker();
['SIGINT', 'SIGTERM'].forEach(sig => {
process.on(sig, () => {
for (const id in cluster.workers) {
cluster.workers[id].kill();
}
process.exit(0);
});
});
} else {
const startServer = async () => {
console.log('Starting...');
await DATABASE.sync();
eventlogger();
initSession(sessionData);
await loadPlugins();
await client();
http
.createServer((req, res) => res.end(JSON.stringify({ alive: req.url === '/' })))
.listen(process.env.PORT || 8000);
};
startServer();
process.on('unhandledRejection', () => {});
process.on('exit', () => process.send('restart'));
}