diff --git a/app.js b/app.js index f5eda3e..b9e1055 100644 --- a/app.js +++ b/app.js @@ -5,16 +5,24 @@ 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 { exitHook } = require("./exitHook.js"); + +const onExit = () => { + setupHost(true) + console.log("按任意键退出") + cp.execSync("pause > nul", { stdio: "inherit" }) +}; // TODO: use kotlin rewrite it (async () => { try { - exitHook(() => { - setupHost(true) - console.log("按任意键退出") - cp.execSync("pause > nul", { stdio: "inherit" }) + process.on("uncaughtException", (err, origin) => { + appcenter.uploadError(err, true) + console.log(err) + console.log(`Origin: ${origin}`) + process.exit(1) }) + process.once("exit", onExit) + process.once("SIGINT", onExit) appcenter.init() let conf = await initConfig() try { diff --git a/exitHook.js b/exitHook.js deleted file mode 100644 index 7f75efe..0000000 --- a/exitHook.js +++ /dev/null @@ -1,50 +0,0 @@ -// 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 -}