Add error handling to node_helper startup sequence (#2945)

Fixes https://github.com/MichMich/MagicMirror/issues/2944

Also splits the Server js into a constrcutor and an open call to remove
one callback parameter :-)

Co-authored-by: veeck <michael@veeck.de>
This commit is contained in:
Veeck 2022-10-19 21:40:43 +02:00 committed by GitHub
parent 7bd944391e
commit 64ed5a54cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 130 additions and 117 deletions

View File

@ -34,6 +34,7 @@ Special thanks to: @rejas, @sdetweil
- Correctly show apparent temperature in SMHI weather provider
- Ensure updatenotification module isn't shown when local is _ahead_ of remote
- Handle node_helper errors during startup (#2944)
- Possibility to change FontAwesome class in calendar, so icons like `fab fa-facebook-square` works.
- Fix cors problems with newsfeed articles (as far as possible), allow disabling cors per feed with option `useCorsProxy: false` (#2840)

View File

@ -223,27 +223,38 @@ function App() {
}
loadModules(modules, function () {
httpServer = new Server(config, function (app, io) {
httpServer = new Server(config);
const { app, io } = 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);
}
}
Promise.allSettled(nodePromises).then(() => {
Log.log("Sockets connected & modules started ...");
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") {
callback(config);
}
});
});
});
});
};
/**

View File

@ -5,7 +5,6 @@
* MIT Licensed.
*/
const express = require("express");
const app = require("express")();
const path = require("path");
const ipfilter = require("express-ipfilter").IpFilter;
const fs = require("fs");
@ -19,14 +18,15 @@ const Utils = require("./utils.js");
* Server
*
* @param {object} config The MM config
* @param {Function} callback Function called when done.
* @class
*/
function Server(config, callback) {
function Server(config) {
const app = express();
const port = process.env.MM_PORT || config.port;
const serverSockets = new Set();
let server = null;
this.open = function () {
if (config.useHttps) {
const options = {
key: fs.readFileSync(config.httpsPrivateKey),
@ -52,7 +52,6 @@ function Server(config, callback) {
});
Log.log(`Starting server on port ${port} ... `);
server.listen(port, config.address || "localhost");
if (config.ipWhitelist instanceof Array && config.ipWhitelist.length === 0) {
@ -69,8 +68,8 @@ function Server(config, callback) {
res.status(403).send("This device is not allowed to access your mirror. <br> Please check your config.js or config.js.sample to change this.");
});
});
app.use(helmet(config.httpHeaders));
app.use(helmet(config.httpHeaders));
app.use("/js", express.static(__dirname));
// TODO add tests directory only when running tests?
@ -127,9 +126,11 @@ function Server(config, callback) {
res.send(html);
});
if (typeof callback === "function") {
callback(app, io);
}
return {
app,
io
};
};
this.close = function () {
for (const socket of serverSockets.values()) {