mirror of
https://github.com/HolographicHat/Yae.git
synced 2026-03-26 14:29:44 +08:00
fix exit hook
This commit is contained in:
11
app.js
11
app.js
@@ -4,12 +4,17 @@ const cp = require("child_process")
|
||||
const rs = require("./regionServer")
|
||||
const appcenter = require("./appcenter")
|
||||
const { initConfig, splitPacket, upload, decodeProto, log, setupHost, KPacket, debug, checkCDN, checkUpdate } = require("./utils")
|
||||
const { exportData } = require("./export");
|
||||
const { exportData } = require("./export")
|
||||
const { exitHook } = require("./exitHook.js");
|
||||
|
||||
// TODO: i18n
|
||||
// TODO: send ack to avoid resend
|
||||
(async () => {
|
||||
try {
|
||||
exitHook(() => {
|
||||
console.log("按任意键退出")
|
||||
cp.execSync("pause > nul", { stdio: "inherit" })
|
||||
})
|
||||
appcenter.init()
|
||||
let conf = await initConfig()
|
||||
try {
|
||||
@@ -95,8 +100,6 @@ const { exportData } = require("./export");
|
||||
const data = zlib.brotliDecompressSync(response.data)
|
||||
const proto = await decodeProto(data,"AllAchievement")
|
||||
await exportData(proto)
|
||||
console.log("按任意键退出")
|
||||
cp.execSync("pause > nul", { stdio: "inherit" })
|
||||
}
|
||||
process.exit(0)
|
||||
}
|
||||
@@ -140,8 +143,6 @@ const { exportData } = require("./export");
|
||||
} else {
|
||||
appcenter.uploadError(Error(e), true)
|
||||
}
|
||||
console.log("按任意键退出")
|
||||
cp.execSync("pause > nul", { stdio: "inherit" })
|
||||
process.exit(0)
|
||||
}
|
||||
})()
|
||||
|
||||
50
exitHook.js
Normal file
50
exitHook.js
Normal file
@@ -0,0 +1,50 @@
|
||||
// https://github.com/sindresorhus/exit-hook
|
||||
|
||||
const callbacks = new Set();
|
||||
let isCalled = false;
|
||||
let isRegistered = false;
|
||||
|
||||
function exit(shouldManuallyExit, signal) {
|
||||
if (isCalled) {
|
||||
return;
|
||||
}
|
||||
|
||||
isCalled = true;
|
||||
|
||||
for (const callback of callbacks) {
|
||||
callback();
|
||||
}
|
||||
|
||||
if (shouldManuallyExit === true) {
|
||||
process.exit(128 + signal);
|
||||
}
|
||||
}
|
||||
|
||||
function exitHook(onExit) {
|
||||
callbacks.add(onExit);
|
||||
|
||||
if (!isRegistered) {
|
||||
isRegistered = true;
|
||||
|
||||
process.once('exit', exit);
|
||||
process.once('SIGINT', exit.bind(undefined, true, 2));
|
||||
process.once('SIGTERM', exit.bind(undefined, true, 15));
|
||||
|
||||
// PM2 Cluster shutdown message. Caught to support async handlers with pm2, needed because
|
||||
// explicitly calling process.exit() doesn't trigger the beforeExit event, and the exit
|
||||
// event cannot support async handlers, since the event loop is never called after it.
|
||||
process.on('message', message => {
|
||||
if (message === 'shutdown') {
|
||||
exit(true, -128);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return () => {
|
||||
callbacks.delete(onExit);
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
exitHook
|
||||
}
|
||||
Reference in New Issue
Block a user