mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
Convert app-start/-stop callbacks to async/await (#3035)
supersedes https://github.com/MichMich/MagicMirror/pull/3027 and just touches the start/stop calls. --------- Co-authored-by: veeck <michael@veeck.de>
This commit is contained in:
parent
2b792cdbb8
commit
fe0b915a5d
@ -38,6 +38,7 @@ _This release is scheduled to be released on 2023-04-01._
|
|||||||
- Cleanup jest coverage for patches
|
- Cleanup jest coverage for patches
|
||||||
- Update `stylelint` dependencies, switch to `stylelint-config-standard` and handle `stylelint` issues
|
- Update `stylelint` dependencies, switch to `stylelint-config-standard` and handle `stylelint` issues
|
||||||
- Convert translator callbacks to async/await
|
- Convert translator callbacks to async/await
|
||||||
|
- Convert app-start/-stop callbacks to async/awaits
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
130
js/app.js
130
js/app.js
@ -53,6 +53,9 @@ function App() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the config file. Combines it with the defaults and returns the config
|
* Loads the config file. Combines it with the defaults and returns the config
|
||||||
|
*
|
||||||
|
* @async
|
||||||
|
* @returns {Promise<object>} the loaded config or the defaults if something goes wrong
|
||||||
*/
|
*/
|
||||||
async function loadConfig() {
|
async function loadConfig() {
|
||||||
Log.log("Loading config ...");
|
Log.log("Loading config ...");
|
||||||
@ -115,8 +118,7 @@ function App() {
|
|||||||
fs.accessSync(configFilename, fs.F_OK);
|
fs.accessSync(configFilename, fs.F_OK);
|
||||||
const c = require(configFilename);
|
const c = require(configFilename);
|
||||||
checkDeprecatedOptions(c);
|
checkDeprecatedOptions(c);
|
||||||
const config = Object.assign(defaults, c);
|
return Object.assign(defaults, c);
|
||||||
return config;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.code === "ENOENT") {
|
if (e.code === "ENOENT") {
|
||||||
Log.error(Utils.colors.error("WARNING! Could not find config file. Please create one. Starting with default configuration."));
|
Log.error(Utils.colors.error("WARNING! Could not find config file. Please create one. Starting with default configuration."));
|
||||||
@ -125,8 +127,9 @@ function App() {
|
|||||||
} else {
|
} else {
|
||||||
Log.error(Utils.colors.error(`WARNING! Could not load config file. Starting with default configuration. Error found: ${e}`));
|
Log.error(Utils.colors.error(`WARNING! Could not load config file. Starting with default configuration. Error found: ${e}`));
|
||||||
}
|
}
|
||||||
return defaults;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return defaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -258,59 +261,55 @@ function App() {
|
|||||||
/**
|
/**
|
||||||
* Start the core app.
|
* Start the core app.
|
||||||
*
|
*
|
||||||
* It loads the config, then it loads all modules. When it's done it
|
* It loads the config, then it loads all modules.
|
||||||
* executes the callback with the config as argument.
|
|
||||||
*
|
*
|
||||||
* @param {Function} callback Function to be called after start
|
* @async
|
||||||
|
* @returns {Promise<object>} the config used
|
||||||
*/
|
*/
|
||||||
this.start = function (callback) {
|
this.start = async function () {
|
||||||
loadConfig().then((c) => {
|
config = await loadConfig();
|
||||||
config = c;
|
|
||||||
|
|
||||||
Log.setLogLevel(config.logLevel);
|
Log.setLogLevel(config.logLevel);
|
||||||
|
|
||||||
let modules = [];
|
let modules = [];
|
||||||
|
|
||||||
for (const module of config.modules) {
|
for (const module of config.modules) {
|
||||||
if (!modules.includes(module.module) && !module.disabled) {
|
if (!modules.includes(module.module) && !module.disabled) {
|
||||||
modules.push(module.module);
|
modules.push(module.module);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadModules(modules, async function () {
|
||||||
|
httpServer = new Server(config);
|
||||||
|
const { app, io } = await httpServer.open();
|
||||||
|
Log.log("Server started ...");
|
||||||
|
|
||||||
|
const nodePromises = [];
|
||||||
|
for (let nodeHelper of nodeHelpers) {
|
||||||
|
nodeHelper.setExpressApp(app);
|
||||||
|
nodeHelper.setSocketIO(io);
|
||||||
|
|
||||||
|
try {
|
||||||
|
nodePromises.push(nodeHelper.start());
|
||||||
|
} catch (error) {
|
||||||
|
Log.error(`Error when starting node_helper for module ${nodeHelper.name}:`);
|
||||||
|
Log.error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadModules(modules, async function () {
|
const results = await Promise.allSettled(nodePromises);
|
||||||
httpServer = new Server(config);
|
|
||||||
const { app, io } = await httpServer.open();
|
|
||||||
Log.log("Server started ...");
|
|
||||||
|
|
||||||
const nodePromises = [];
|
// Log errors that happened during async node_helper startup
|
||||||
for (let nodeHelper of nodeHelpers) {
|
results.forEach((result) => {
|
||||||
nodeHelper.setExpressApp(app);
|
if (result.status === "rejected") {
|
||||||
nodeHelper.setSocketIO(io);
|
Log.error(result.reason);
|
||||||
|
|
||||||
try {
|
|
||||||
nodePromises.push(nodeHelper.start());
|
|
||||||
} catch (error) {
|
|
||||||
Log.error(`Error when starting node_helper for module ${nodeHelper.name}:`);
|
|
||||||
Log.error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Promise.allSettled(nodePromises).then((results) => {
|
|
||||||
// Log errors that happened during async node_helper startup
|
|
||||||
results.forEach((result) => {
|
|
||||||
if (result.status === "rejected") {
|
|
||||||
Log.error(result.reason);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Log.log("Sockets connected & modules started ...");
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (typeof callback === "function") {
|
Log.log("Sockets connected & modules started ...");
|
||||||
callback(config);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return config;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -319,15 +318,40 @@ function App() {
|
|||||||
*
|
*
|
||||||
* Added to fix #1056
|
* Added to fix #1056
|
||||||
*
|
*
|
||||||
* @param {Function} callback Function to be called after the app has stopped
|
* @returns {Promise} A promise that is resolved when all node_helpers and
|
||||||
|
* the http server has been closed
|
||||||
*/
|
*/
|
||||||
this.stop = function (callback) {
|
this.stop = async function () {
|
||||||
for (const nodeHelper of nodeHelpers) {
|
const nodePromises = [];
|
||||||
if (typeof nodeHelper.stop === "function") {
|
for (let nodeHelper of nodeHelpers) {
|
||||||
nodeHelper.stop();
|
try {
|
||||||
|
if (typeof nodeHelper.stop === "function") {
|
||||||
|
nodePromises.push(nodeHelper.stop());
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
Log.error(`Error when stopping node_helper for module ${nodeHelper.name}:`);
|
||||||
|
console.error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
httpServer.close().then(callback);
|
|
||||||
|
const results = await Promise.allSettled(nodePromises);
|
||||||
|
|
||||||
|
// Log errors that happened during async node_helper stopping
|
||||||
|
results.forEach((result) => {
|
||||||
|
if (result.status === "rejected") {
|
||||||
|
Log.error(result.reason);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Log.log("Node_helpers stopped ...");
|
||||||
|
|
||||||
|
// To be able to stop the app even if it hasn't been started (when
|
||||||
|
// running with Electron against another server)
|
||||||
|
if (!httpServer) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpServer.close();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -337,12 +361,12 @@ function App() {
|
|||||||
* Note: this is only used if running `server-only`. Otherwise
|
* Note: this is only used if running `server-only`. Otherwise
|
||||||
* this.stop() is called by app.on("before-quit"... in `electron.js`
|
* this.stop() is called by app.on("before-quit"... in `electron.js`
|
||||||
*/
|
*/
|
||||||
process.on("SIGINT", () => {
|
process.on("SIGINT", async () => {
|
||||||
Log.log("[SIGINT] Received. Shutting down server...");
|
Log.log("[SIGINT] Received. Shutting down server...");
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}, 3000); // Force quit after 3 seconds
|
}, 3000); // Force quit after 3 seconds
|
||||||
this.stop();
|
await this.stop();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -350,12 +374,12 @@ function App() {
|
|||||||
* Listen to SIGTERM signals so we can stop everything when we
|
* Listen to SIGTERM signals so we can stop everything when we
|
||||||
* are asked to stop by the OS.
|
* are asked to stop by the OS.
|
||||||
*/
|
*/
|
||||||
process.on("SIGTERM", () => {
|
process.on("SIGTERM", async () => {
|
||||||
Log.log("[SIGTERM] Received. Shutting down server...");
|
Log.log("[SIGTERM] Received. Shutting down server...");
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}, 3000); // Force quit after 3 seconds
|
}, 3000); // Force quit after 3 seconds
|
||||||
this.stop();
|
await this.stop();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -157,18 +157,19 @@ app.on("activate", function () {
|
|||||||
* Note: this is only used if running Electron. Otherwise
|
* Note: this is only used if running Electron. Otherwise
|
||||||
* core.stop() is called by process.on("SIGINT"... in `app.js`
|
* core.stop() is called by process.on("SIGINT"... in `app.js`
|
||||||
*/
|
*/
|
||||||
app.on("before-quit", (event) => {
|
app.on("before-quit", async (event) => {
|
||||||
Log.log("Shutting down server...");
|
Log.log("Shutting down server...");
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}, 3000); // Force-quit after 3 seconds.
|
}, 3000); // Force-quit after 3 seconds.
|
||||||
core.stop();
|
await core.stop();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
/* handle errors from self signed certificates */
|
/**
|
||||||
|
* Handle errors from self-signed certificates
|
||||||
|
*/
|
||||||
app.on("certificate-error", (event, webContents, url, error, certificate, callback) => {
|
app.on("certificate-error", (event, webContents, url, error, certificate, callback) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
callback(true);
|
callback(true);
|
||||||
@ -177,7 +178,5 @@ app.on("certificate-error", (event, webContents, url, error, certificate, callba
|
|||||||
// Start the core application if server is run on localhost
|
// Start the core application if server is run on localhost
|
||||||
// This starts all node helpers and starts the webserver.
|
// This starts all node helpers and starts the webserver.
|
||||||
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].includes(config.address)) {
|
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].includes(config.address)) {
|
||||||
core.start(function (c) {
|
core.start().then((c) => (config = c));
|
||||||
config = c;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
const app = require("../js/app.js");
|
const app = require("../js/app.js");
|
||||||
const Log = require("logger");
|
const Log = require("logger");
|
||||||
|
|
||||||
app.start((config) => {
|
app.start().then((config) => {
|
||||||
const bindAddress = config.address ? config.address : "localhost";
|
const bindAddress = config.address ? config.address : "localhost";
|
||||||
const httpType = config.useHttps ? "https" : "http";
|
const httpType = config.useHttps ? "https" : "http";
|
||||||
Log.log("\nReady to go! Please point your browser to: " + httpType + "://" + bindAddress + ":" + config.port);
|
Log.log("\nReady to go! Please point your browser to: " + httpType + "://" + bindAddress + ":" + config.port);
|
||||||
|
@ -15,19 +15,15 @@ exports.startApplication = async (configFilename, exec) => {
|
|||||||
if (exec) exec;
|
if (exec) exec;
|
||||||
global.app = require("app.js");
|
global.app = require("app.js");
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return global.app.start();
|
||||||
global.app.start(resolve);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.stopApplication = async () => {
|
exports.stopApplication = async () => {
|
||||||
if (global.app) {
|
if (!global.app) {
|
||||||
return new Promise((resolve) => {
|
return Promise.resolve();
|
||||||
global.app.stop(resolve);
|
|
||||||
delete global.app;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return Promise.resolve();
|
await global.app.stop();
|
||||||
|
delete global.app;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getDocument = () => {
|
exports.getDocument = () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user