mirror of
https://github.com/QuiteAFancyEmerald/Holy-Unblocker.git
synced 2025-05-12 11:30:01 -04:00
Flesh out error handling for run-commands.mjs; try simplifying GitHub workflow.
This commit is contained in:
parent
995183d239
commit
f47d6bc28c
4 changed files with 36 additions and 10 deletions
|
@ -28,7 +28,7 @@ const shutdown = fileURLToPath(new URL("./src/.shutdown", import.meta.url));
|
|||
|
||||
// Run each command line argument passed after node run-command.mjs.
|
||||
// Commands are defined in the switch case statement below.
|
||||
for (let i = 2; i < process.argv.length; i++)
|
||||
commands: for (let i = 2; i < process.argv.length; i++)
|
||||
switch (process.argv[i]) {
|
||||
// Commmand to boot up the server. Use PM2 to run if production is true in the
|
||||
// config file.
|
||||
|
@ -44,7 +44,10 @@ for (let i = 2; i < process.argv.length; i++)
|
|||
// This should run the server as a background process.
|
||||
else if (process.platform === "win32")
|
||||
exec('START /MIN "" node backend.js', (error, stdout) => {
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
console.log(stdout);
|
||||
});
|
||||
// The following approach (and similar approaches) will not work on Windows,
|
||||
|
@ -52,8 +55,16 @@ for (let i = 2; i < process.argv.length; i++)
|
|||
else {
|
||||
const server = fork(
|
||||
fileURLToPath(new URL("./backend.js", import.meta.url)),
|
||||
{detached: true}
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
stdio: ["inherit", "inherit", "pipe", "ipc"],
|
||||
detached: true
|
||||
}
|
||||
);
|
||||
server.stderr.on("data", stderr => {
|
||||
console.error(stderr.toString());
|
||||
process.exitCode = 1;
|
||||
});
|
||||
server.unref();
|
||||
server.disconnect();
|
||||
}
|
||||
|
@ -63,7 +74,7 @@ for (let i = 2; i < process.argv.length; i++)
|
|||
// to shut down. This is done by sending a GET request to the server.
|
||||
case "stop": {
|
||||
await writeFile(shutdown, "");
|
||||
let timeoutId = undefined;
|
||||
let timeoutId, hasErrored = false;
|
||||
try {
|
||||
// Give the server 5 seconds to respond, otherwise cancel this and throw an
|
||||
// error to the console. The fetch request will also throw an error immediately
|
||||
|
@ -79,18 +90,33 @@ for (let i = 2; i < process.argv.length; i++)
|
|||
clearTimeout(timeoutId);
|
||||
if (response === "Error") throw new Error("Server is unresponsive.");
|
||||
} catch (e) {
|
||||
// Remove the temporary shutdown file since the server didn't remove it.
|
||||
await unlink(shutdown);
|
||||
// Check if this is the error thrown by the fetch request for an unused port.
|
||||
// Don't print the unused port error, since nothing has actually broken.
|
||||
if (e instanceof TypeError) clearTimeout(timeoutId);
|
||||
else console.error(e);
|
||||
await unlink(shutdown);
|
||||
else {
|
||||
console.error(e);
|
||||
// Stop here unless Node will be killed later.
|
||||
if (!process.argv.slice(i + 1).includes("kill"))
|
||||
hasErrored = true;
|
||||
}
|
||||
}
|
||||
// Do not run this if Node will be killed later in this script. It will fail.
|
||||
if (config.production && !process.argv.slice(i + 1).includes("kill"))
|
||||
exec("npx pm2 stop ecosystem.config.js", (error, stdout) => {
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
console.error(error);
|
||||
hasErrored = true;
|
||||
}
|
||||
console.log(stdout);
|
||||
});
|
||||
// Do not continue executing commands since the server was unable to be stopped.
|
||||
// Mostly implemented to prevent duplicating Node instances with npm restart.
|
||||
if (hasErrored) {
|
||||
process.exitCode = 1;
|
||||
break commands;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -132,4 +158,4 @@ for (let i = 2; i < process.argv.length; i++)
|
|||
}
|
||||
|
||||
|
||||
process.exitCode = 0;
|
||||
process.exitCode = process.exitCode || 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue