-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
195 lines (164 loc) · 5.49 KB
/
index.js
File metadata and controls
195 lines (164 loc) · 5.49 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const config_loader = require('#root/Config.js');
const logger = require('#src/Logger.js');
/**
* @type {import('node:cluster').default}
*/
const cluster = require('node:cluster');
/**
* @type {import('axios').Axios}
*/
const axios = require('axios');
async function bootstrap() {
await config_loader.fetch();
if (cluster.isPrimary) {
await initParent();
}
if (cluster.isWorker) {
await initChild();
}
}
bootstrap();
/**
* Init parent process
*/
async function initParent() {
const config = config_loader.get();
async function useWebhook(message, color = 0x800000) {
let params = {
content: config.errors.role,
embeds: [
{
title: 'The bridge has sent an update',
fields: [
{
name: 'Information',
value: `${message}\nInstance: \`${config.identity.unique_id}\``
}
],
color: color
}
]
};
try {
await axios.post(config.errors.webhook, params, {
headers: {
'Content-type': 'application/json',
Authorization: config.approaches.discord.token
}
});
} catch (error) {
logger.error('Failed to send webhook notification', error);
}
}
async function handleEvent(event) {
if (event.id === 'exception') {
await useWebhook(`An exception was caught:\n\`\`\`${event.exception.toString()}\`\`\``, 0x800000);
}
if (event.id === 'init') {
await useWebhook(`The bridge has initialized successfully.`, 0x008000);
}
if (event.id === 'warning') {
await useWebhook(`A warning was issued.\n\`\`\`${event.info.toString()}\`\`\``, 0x808000);
}
}
async function handleState() {
if (state == states.TERMINATED) {
for (const id in cluster.workers) {
cluster.workers[id].kill();
}
return;
}
if (state == states.STOPPED) {
cluster.fork({
...process.env
});
state = states.STARTED;
for (const id in cluster.workers) {
cluster.workers[id].on('message', handleEvent);
}
}
}
async function handleEmergencyLongpoll() {
try {
if (!config.SCF) return;
let requests = await config.SCF.API.longpoll.getApplicable();
for (let action of requests) {
try {
let act_rid = action.rid ?? 'NONE';
let act_type = action.action ?? 'NONE';
let act_data = action.data ?? {};
let completed = false;
if (act_type == 'forceReboot') {
for (const id in cluster.workers) {
cluster.workers[id].kill();
}
state = states.STOPPED;
completed = true;
}
if (act_type == 'killYourself') {
setTimeout(() => {
process.exit();
}, 10000);
completed = true;
}
if (completed) {
await config.SCF.API.longpoll.remove(act_rid);
}
} catch (e) {
logger.warn(`Failed to handle a longpoll request!`, action);
console.log(e);
}
}
} catch (e) {
logger.warn(`Encountered an error while handling emergency longpoll.`, e);
}
}
let states = {
TERMINATED: -1,
STOPPED: 0,
STARTED: 1
};
let state = states.STOPPED;
handleState();
setInterval(handleState, 5000);
setInterval(handleEmergencyLongpoll, 30000);
cluster.on('exit', function (worker, code, signal) {
state = states.STOPPED;
logger.warn(`The bridge has stopped with a ${code} error code.`);
if (code == 123) {
state = states.TERMINATED;
useWebhook("Something bad has happened to the bot, maybe it's banned or muted. The app will shut down.");
logger.error(
'The bridge has stopped due to a fatal error requiring manual maintenance. The bridge will not reboot. The parent process is still running.'
);
}
if (code == 124) {
useWebhook('The bridge failed to start a critical approach.');
logger.error('The bridge failed to start a critical approach.');
}
if (code == 5) {
logger.warn('The bridge is deploying a new version...');
}
});
}
/**
* Init child process
*/
async function initChild() {
const Application = require('#root/src/Application.js');
process.on('uncaughtException', (error) => {
logger.error(`Caught an uncaught exception!`, error);
process.send({
id: 'exception',
exception: error
});
process.exit(1);
});
process.on('unhandledRejection', function (err, promise) {
logger.error('Caught an unhandled rejection!', err);
console.log(err, promise);
process.exit(1);
});
const app = new Application();
await app.init();
}