mirror of
https://github.com/MasterAcnolo/Freedom-Loader.git
synced 2026-09-27 20:11:15 +02:00
refactor: extract shutdown logic in a function, rename server => serverInstance and now we stop the server smoothly
This commit is contained in:
38
main.js
38
main.js
@@ -76,6 +76,7 @@ const { registerIpcHandlers } = require("./app/ipcHandlers");
|
||||
const { createSplashWindow, closeSplashWindow, setSplashProgress } = require("./app/splashManager");
|
||||
const { userThemesPath, initUserThemes, isWindows, validateBinaries, defaultDownloadFolder } = require("./server/helpers/path.helpers");
|
||||
const {createSystemTray, destroyTray} = require("./app/tray");
|
||||
const { stopServer } = require("./server/server");
|
||||
|
||||
/**
|
||||
* Global flag indicating if the application is intentionally shutting down.
|
||||
@@ -91,6 +92,28 @@ app.isQuitting = false;
|
||||
*/
|
||||
let isCleanedUp = false;
|
||||
|
||||
/**
|
||||
* Performs a graceful shutdown of all application services asynchronously.
|
||||
* Once finished, it re-triggers the app.quit() process.
|
||||
*/
|
||||
async function cleanShutdown() {
|
||||
try {
|
||||
destroyTray();
|
||||
await stopRPC();
|
||||
stopServer();
|
||||
|
||||
} catch (err) {
|
||||
logger.error("Error during cleanup:", err);
|
||||
|
||||
} finally {
|
||||
logger.info("All services stopped. Have a nice day!");
|
||||
logSessionEnd();
|
||||
|
||||
isCleanedUp = true;
|
||||
app.quit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If another instance want to run
|
||||
*/
|
||||
@@ -166,19 +189,6 @@ app.on("before-quit", (event) => {
|
||||
if (!isCleanedUp) {
|
||||
event.preventDefault();
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
destroyTray();
|
||||
await stopRPC();
|
||||
} catch (err) {
|
||||
logger.error("Error during cleanup:", err);
|
||||
} finally {
|
||||
logger.info("All services stopped. Have a nice day!");
|
||||
logSessionEnd();
|
||||
|
||||
isCleanedUp = true;
|
||||
app.quit();
|
||||
}
|
||||
})();
|
||||
cleanShutdown();
|
||||
}
|
||||
});
|
||||
@@ -1,11 +1,17 @@
|
||||
const express = require("express");
|
||||
const path = require("path");
|
||||
const { logger, logSessionEnd } = require("./logger");
|
||||
const config = require("../config");
|
||||
|
||||
const { logger, logSessionEnd } = require("./logger");
|
||||
const { rateLimit } = require("./helpers/rateLimit.helpers");
|
||||
|
||||
const app = express();
|
||||
|
||||
/**
|
||||
* Store the state of the server
|
||||
*/
|
||||
let serverInstance = null;
|
||||
|
||||
// Middlewares
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
@@ -30,33 +36,29 @@ app.get("/", rateLimit, (req, res) => {
|
||||
*/
|
||||
async function startServer() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = app.listen(config.applicationPort, () => {
|
||||
const serverInstance = app.listen(config.applicationPort, () => {
|
||||
logger.info(`Express server ready at http://localhost:${config.applicationPort}`);
|
||||
resolve(server);
|
||||
resolve(serverInstance);
|
||||
});
|
||||
|
||||
server.on("error", (err) => {
|
||||
serverInstance.on("error", (err) => {
|
||||
logger.error("Express server error:", err);
|
||||
reject(err);
|
||||
});
|
||||
|
||||
/**
|
||||
* Clean exit function
|
||||
* - Stop Log
|
||||
* - Stop Express Server
|
||||
* - Stop Electron App
|
||||
*/
|
||||
const gracefulExit = () => {
|
||||
logSessionEnd();
|
||||
server.close(() => {
|
||||
logger.info("Express server closed cleanly.");
|
||||
process.exit();
|
||||
});
|
||||
};
|
||||
|
||||
process.on("SIGINT", gracefulExit);
|
||||
process.on("SIGTERM", gracefulExit);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { startServer };
|
||||
/**
|
||||
* Clean exit function called by Electron
|
||||
*/
|
||||
function stopServer() {
|
||||
if (serverInstance) {
|
||||
serverInstance.close();
|
||||
logger.info("Express server closed cleanly.");
|
||||
|
||||
} else {
|
||||
logger.error("Express server was not closed cleanly")
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { startServer, stopServer };
|
||||
Reference in New Issue
Block a user