-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
42 lines (35 loc) · 1.25 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
const { spawn } = require('child_process');
const path = require('path');
const http = require('http');
// Function to start the server with a specified port
function start(port) {
let args = [path.join(__dirname, 'plugins.js'), port, ...process.argv.slice(2)];
console.log(`Spawning process: ${process.argv[0]} ${args.join(' ')}`);
let p = spawn(process.argv[0], args, {
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
});
p.on('message', data => {
if (data === 'reset') {
console.log('Restarting Bot...');
p.kill();
start(port); // Restart with the same port
}
});
p.on('exit', code => {
console.error('Exited with code:', code);
// Retry if the process exits with an error code or is terminated
if (code === 0 || code === 1) start(port); // Restart on exit code 0 or 1
});
}
// Explicitly set port to 3000
const port = 3000;
start(port);
// Create an HTTP server to keep the HTTP port open
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, world!\n');
});
server.listen(port, () => {
console.log(`HTTP server running at http://localhost:${port}/`);
});