This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
app.js
126 lines (103 loc) · 2.98 KB
/
app.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
import { fork } from "child_process";
import figlet from "figlet";
import path from "path";
import semver from "semver";
import url from "url";
import "#utils/config";
("use strict");
const m_ME = path.basename(url.fileURLToPath(import.meta.url || ""));
let mPostRunning = false;
const mBot = { name: "机器人", path: path.resolve(global.rootdir, "bot.js"), process: undefined };
const mServer = { name: "文件服务器", path: path.resolve(global.rootdir, "server.js"), process: undefined };
const m_LOG_TYPE = Object.freeze({
DEBUG: "1",
INFO: "2",
ERROR: "3",
});
function log(type, ...rest) {
let logger;
switch (type) {
case m_LOG_TYPE.ERROR:
logger = console.error;
break;
default:
logger = console.log;
break;
}
return logger(`${m_ME}:`, ...rest);
}
function hello() {
const asciiArt = figlet.textSync(global.package.name, {
font: "DOS Rebel",
horizontalLayout: "full",
verticalLayout: "full",
width: 120,
whitespaceBreak: true,
});
log(m_LOG_TYPE.INFO);
asciiArt.split("\n").forEach((line) => log(m_LOG_TYPE.INFO, line));
log(m_LOG_TYPE.INFO, `\t\t\t项目主页:${global.package.homepage}`);
log(m_LOG_TYPE.INFO);
}
function quit() {
process.kill(process.pid, "SIGTERM");
}
function runBot() {
log(m_LOG_TYPE.INFO, `正在从 ${mBot.path} 拉起${mBot.name}。`);
mBot.process = fork(mBot.path);
mBot.process.on("exit", () => {
if (false === mPostRunning) {
log(m_LOG_TYPE.ERROR, `${mBot.name}异常退出!`);
quit();
}
});
}
function runServer(port = 9934) {
log(m_LOG_TYPE.INFO, `正在从 ${mServer.path} 拉起${mServer.name}。`);
mServer.process = fork(mServer.path, ["-p", port.toString()]);
mServer.process.on("exit", () => {
if (false === mPostRunning) {
log(m_LOG_TYPE.ERROR, `${mServer.name}异常退出!`);
runServer(port);
}
});
}
function killBot() {
if (undefined !== mBot.process) {
log(m_LOG_TYPE.INFO, `正在终止${mBot.name}……`);
mBot.process.kill();
}
}
function killServer() {
if (undefined !== mServer.process) {
log(m_LOG_TYPE.INFO, `正在终止${mServer.name}……`);
mServer.process.kill();
}
}
async function doPost() {
if (false === mPostRunning) {
mPostRunning = true; // {
log(m_LOG_TYPE.INFO, "正在退出……");
killServer();
killBot();
await new Promise((resolve) => setTimeout(resolve, 3000));
// }
mPostRunning = false;
} else {
while (true === mPostRunning) {
await new Promise((resolve) => setTimeout(() => resolve(), 100));
}
}
}
(async function main() {
for (const signal of ["SIGHUP", "SIGINT", "SIGTERM"]) {
process.on(signal, () => doPost().then((n) => process.exit(n)));
}
hello();
if (!semver.satisfies(process.versions.node, global.package.engines.node)) {
log(m_LOG_TYPE.ERROR, `当前 Node.JS 版本(${process.versions.node})不满足 ${global.package.engines.node}!`);
quit();
}
runServer();
runBot();
})();