-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (45 loc) · 1.34 KB
/
index.ts
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
import { config } from './Constants';
import { BServer } from './classes/BServer';
import { Server } from './Server';
import { addListeners } from './classes/Listener';
import { createInterface } from 'readline';
import Database from './classes/DatabaseImpl';
import PluginSystem from './classes/Plugins';
export const rl = createInterface({
input: process.stdin,
output: process.stdout
});
PluginSystem.initalizePluginSystem();
Server.start();
addListeners();
Database.initializeDatabaseData(config).then(() => Server.listen());
function pKill() {
console.log("Servers stopping");
const proms = [];
try {
BServer.servers.forEach(s => proms.push(s.stop(true)));
} catch {
console.error("Error stopping servers. Ending now.");
process.exit();
}
Promise.all(proms).then(() => {
console.log("Exiting");
Server.io.emit("logout");
process.exit();
}).catch(() => {
process.exit();
});
}
rl.on('SIGINT', pKill);
process.on('SIGTERM', pKill);
process.on('SIGINT', pKill);
//catches uncaught exceptions
process.on('uncaughtException', (err, origin) => {
console.error(err);
console.error(`Error in ${origin}`);
pKill();
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
pKill();
});