-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathserver.js
136 lines (119 loc) · 3.46 KB
/
server.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
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
const cluster = require('cluster');
const path = require('path');
const fs = require('fs');
const workers = {};
function start(file) {
if (workers[file]) return;
const args = [path.join(__dirname, file), ...process.argv.slice(2)];
cluster.setupMaster({
exec: path.join(__dirname, file),
args: args.slice(1),
});
const p = cluster.fork();
p.on('message', async (data) => {
console.log(`[RECEIVED from ${file}]`, "Restart");
switch (data) {
case 'reset':
resetProcess(file);
break;
case 'uptime':
p.send(process.uptime());
break;
case 'shutdown':
shutdown()
break;
}
});
p.on('exit', (code, signal) => {
console.error(`Child process for ${file} exited with code: ${code}, signal: ${signal}`);
if (!workers[file]) {
console.error(`No process reference found for ${file}`);
return;
}
delete workers[file];
console.log("Restarting the process immediately");
start(file);
});
workers[file] = p;
}
function resetProcess(file) {
const worker = workers[file];
if (worker) {
worker.kill();
} else {
console.error(`No child process running for ${file}`);
}
}
function BootUp() {
console.log("Booting Up Sequence Initiated!");
start("index.js");
}
function shutdown() {
console.log("Shutting down the server...");
for (const file in workers) {
stopProcess(file);
}
}
function stopProcess(file) {
const worker = workers[file];
if (worker) {
worker.send('shutdown'); // Send shutdown message to the worker
delete workers[file];
console.log(`Stopping process for ${file}`);
} else {
console.error(`No child process running for ${file}`);
}
}
async function deleteSession(){
fs.readdir('session/', (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
files.forEach(file => {
if (file !== 'Aurora.txt') {
fs.unlink(path.join('session/', file), err => {
if (err) {
console.error('Error deleting file:', err);
return;
}
console.log(`${file} has been deleted.`);
});
}
});
});
}
console.log(`==================================================\n Server Starting...!\n==================================================`);
const express = require("express");
const app = express();
const port = process.env.PORT || 8000;
app.post('/restart', (req, res) => {
console.log("[Restarting]");
for (const file in workers) {
resetProcess(file);
}
res.sendStatus(200);
});
app.post('/update', (req, res) => {
console.log("[Discarding Session]");
deleteSession()
return res.sendStatus(200);
});
app.post('/shutdown', (req, res) => {
console.log("[ShutDown]");
shutdown()
return res.sendStatus(200);
});
app.post('/bootup', (req, res) => {
console.log("[BootUp]");
BootUp()
return res.sendStatus(200);
});
app.post('/feksession', (req, res) => {
console.log("[Discarding Session]");
//BootUp()
return res.sendStatus(200);
});
app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'lib/Messages/index.html')); });
app.listen(port, () => console.log(`cortana Server listening on port http://localhost:${port}`));
start("index.js");