Minor changes; resolve waiting period for restart when the server is already off.

This commit is contained in:
00Fjongl 2024-08-07 22:51:59 -05:00
parent a140c73537
commit 1411d697b2

View file

@ -61,13 +61,13 @@ for (let i = 2; i < process.argv.length; i++)
// Stop the server. Make a temporary file that the server will check for if told // Stop the server. Make a temporary file that the server will check for if told
// to shut down. This is done by sending a GET request to the server. // to shut down. This is done by sending a GET request to the server.
case "stop": case "stop": {
await writeFile(shutdown, ""); await writeFile(shutdown, "");
let timeoutId = undefined;
try { try {
// Give the server 5 seconds to respond, otherwise cancel this and throw an // 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 // error to the console. The fetch request will also throw an error immediately
// if checking the server on localhost and the port is unused. // if checking the server on localhost and the port is unused.
let timeoutId = undefined;
const response = await Promise.race([ const response = await Promise.race([
fetch(new URL("/test-shutdown", serverUrl)), fetch(new URL("/test-shutdown", serverUrl)),
new Promise(resolve => { new Promise(resolve => {
@ -79,15 +79,19 @@ for (let i = 2; i < process.argv.length; i++)
clearTimeout(timeoutId); clearTimeout(timeoutId);
if (response === "Error") throw new Error("Server is unresponsive."); if (response === "Error") throw new Error("Server is unresponsive.");
} catch (e) { } catch (e) {
if (!(e instanceof TypeError)) console.error(e); // Check if this is the error thrown by the fetch request for an unused port.
if (e instanceof TypeError) clearTimeout(timeoutId);
else console.error(e);
await unlink(shutdown); await unlink(shutdown);
} }
// 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")) if (config.production && !process.argv.slice(i + 1).includes("kill"))
exec("npx pm2 stop ecosystem.config.js", (error, stdout) => { exec("npx pm2 stop ecosystem.config.js", (error, stdout) => {
if (error) throw error; if (error) throw error;
console.log(stdout); console.log(stdout);
}); });
break; break;
}
case "build": { case "build": {
const dist = fileURLToPath(new URL("./views/dist", import.meta.url)); const dist = fileURLToPath(new URL("./views/dist", import.meta.url));