-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTuxManager.js
89 lines (88 loc) · 2.33 KB
/
TuxManager.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
import {inspect} from 'util';
import {HelperTux} from './structures/core/tux.js';
import {load} from './utils/TuxProcessHelper.js';
import cluster from 'cluster';
import fastify from 'fastify';
import dotenv from 'dotenv';
dotenv.config();
if (cluster.isPrimary) {
let x = 0;
console.log(`Tux Process Manager Online, PID: ${process.pid}`);
let node = cluster.fork();
cluster.on('exit', (worker, code, signal) => {
x++;
console.log(`Termination count: ${x}`);
console.log(
`Tux Node Handler (PID: ${worker.process.pid}) exited with code: ${code} and signal: ${signal} `
);
node = cluster.fork();
});
let lastStatus = true;
node.on('message', message => {
if (message.msg === 'status') {
lastStatus = message.status;
}
});
node.send('status');
setInterval(() => node.send('status'), 15000);
const server = fastify();
server.get('/', (_request, reply) => {
reply
.type('application/json')
.code(lastStatus ? 200 : 500)
.send({
server: 'online',
bot: lastStatus ? 'online' : 'offline',
});
});
(async () => {
try {
await server.listen({
port: process.env.PORT || 3000,
});
console.log(`Server is listening on ${server.server.address().port}`);
} catch (err) {
console.log('Failed creating server');
console.log(err);
}
})();
} else {
const tux = new HelperTux();
load(tux);
tux
.login(process.env.TOKEN)
.then(tux.logger.log(`Login Requested`, 'LOGIN', 'Login Method Called'));
tux.logger.log(
`Tux Node Handler Online, PID: ${process.pid}`,
'INFO',
'Tux Node Handler Online'
);
process
.on('unhandledRejection', (reason, promise) =>
tux.logger.log(
`Unhandled Rejection at: ${inspect(promise)} reason: ${inspect(
reason
)}`,
'unhandledRejection',
'Error'
)
)
.on('uncaughtException', (err, origin) =>
tux.logger.log(
`Error: ${inspect(err)} at ${inspect(origin)}`,
'uncaughtException',
'Error'
)
)
.on('SIGINT', () => process.exit(0))
.on('SIGTERM', () => process.exit(0))
.on('beforeExit', () => process.exit(0))
.on('message', msg => {
if (msg === 'status') {
process.send({
msg,
status: tux.ws.ping,
});
}
});
}