Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ _This release is scheduled to be released on 2023-04-01._
- Update dates in Calendar widgets every minute
- Cleanup jest coverage for patches
- Update `stylelint` dependencies, switch to `stylelint-config-standard` and handle `stylelint` issues
- Convert lots of callbacks to async/await
- Fixed Open-Meteo wind speed units

### Fixed
Expand Down
177 changes: 79 additions & 98 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ function App() {

/**
* 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() {
Log.log("Loading config ...");
Expand Down Expand Up @@ -118,7 +115,8 @@ function App() {
fs.accessSync(configFilename, fs.F_OK);
const c = require(configFilename);
checkDeprecatedOptions(c);
return Object.assign(defaults, c);
const config = Object.assign(defaults, c);
return config;
} catch (e) {
if (e.code === "ENOENT") {
Log.error(Utils.colors.error("WARNING! Could not find config file. Please create one. Starting with default configuration."));
Expand All @@ -127,9 +125,8 @@ function App() {
} else {
Log.error(Utils.colors.error(`WARNING! Could not load config file. Starting with default configuration. Error found: ${e}`));
}
return defaults;
}

return defaults;
}

/**
Expand All @@ -152,8 +149,9 @@ function App() {
* Loads a specific module.
*
* @param {string} module The name of the module (including subpath).
* @param {Function} callback Function to be called after loading
*/
function loadModule(module) {
function loadModule(module, callback) {
const elements = module.split("/");
const moduleName = elements[elements.length - 1];
let moduleFolder = `${__dirname}/../modules/${module}`;
Expand Down Expand Up @@ -198,37 +196,39 @@ function App() {
m.setPath(path.resolve(moduleFolder));
nodeHelpers.push(m);

m.loaded();
m.loaded(callback);
} else {
callback();
}
}

/**
* Loads all modules.
*
* @param {string[]} modules All modules to be loaded
* @param {Module[]} modules All modules to be loaded
* @param {Function} callback Function to be called after loading
*/
async function loadModules(modules) {
return new Promise((resolve) => {
Log.log("Loading module helpers ...");

/**
*
*/
function loadNextModule() {
if (modules.length > 0) {
const nextModule = modules[0];
loadModule(nextModule);
function loadModules(modules, callback) {
Log.log("Loading module helpers ...");

/**
*
*/
function loadNextModule() {
if (modules.length > 0) {
const nextModule = modules[0];
loadModule(nextModule, function () {
modules = modules.slice(1);
loadNextModule();
} else {
// All modules are loaded
Log.log("All module helpers loaded.");
resolve();
}
});
} else {
// All modules are loaded
Log.log("All module helpers loaded.");
callback();
}
}

loadNextModule();
});
loadNextModule();
}

/**
Expand Down Expand Up @@ -258,53 +258,59 @@ function App() {
/**
* Start the core app.
*
* It loads the config, then it loads all modules.
* It loads the config, then it loads all modules. When it's done it
* executes the callback with the config as argument.
*
* @async
* @returns {Promise<object>} the config used
* @param {Function} callback Function to be called after start
*/
this.start = async function () {
config = await loadConfig();
this.start = function (callback) {
loadConfig().then((c) => {
config = c;

Log.setLogLevel(config.logLevel);
Log.setLogLevel(config.logLevel);

let modules = [];
for (const module of config.modules) {
if (!modules.includes(module.module) && !module.disabled) {
modules.push(module.module);
let modules = [];

for (const module of config.modules) {
if (!modules.includes(module.module) && !module.disabled) {
modules.push(module.module);
}
}
}
await loadModules(modules);

httpServer = new Server(config);
const { app, io } = await httpServer.open();
Log.log("Server started ...");
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);
}
}

const nodePromises = [];
for (let nodeHelper of nodeHelpers) {
nodeHelper.setExpressApp(app);
nodeHelper.setSocketIO(io);
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);
}
});

try {
nodePromises.push(nodeHelper.start());
} catch (error) {
Log.error(`Error when starting node_helper for module ${nodeHelper.name}:`);
Log.error(error);
}
}
Log.log("Sockets connected & modules started ...");
});
});

const results = await Promise.allSettled(nodePromises);

// Log errors that happened during async node_helper startup
results.forEach((result) => {
if (result.status === "rejected") {
Log.error(result.reason);
if (typeof callback === "function") {
callback(config);
}
});

Log.log("Sockets connected & modules started ...");

return config;
};

/**
Expand All @@ -313,40 +319,15 @@ function App() {
*
* Added to fix #1056
*
* @returns {Promise} A promise that is resolved when all node_helpers and
* the http server has been closed
* @param {Function} callback Function to be called after the app has stopped
*/
this.stop = async function () {
const nodePromises = [];
for (let nodeHelper of nodeHelpers) {
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);
this.stop = function (callback) {
for (const nodeHelper of nodeHelpers) {
if (typeof nodeHelper.stop === "function") {
nodeHelper.stop();
}
}

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();
httpServer.close().then(callback);
};

/**
Expand All @@ -356,25 +337,25 @@ function App() {
* Note: this is only used if running `server-only`. Otherwise
* this.stop() is called by app.on("before-quit"... in `electron.js`
*/
process.on("SIGINT", async () => {
process.on("SIGINT", () => {
Log.log("[SIGINT] Received. Shutting down server...");
setTimeout(() => {
process.exit(0);
}, 3000); // Force quit after 3 seconds
await this.stop();
this.stop();
process.exit(0);
});

/**
* Listen to SIGTERM signals so we can stop everything when we
* are asked to stop by the OS.
*/
process.on("SIGTERM", async () => {
process.on("SIGTERM", () => {
Log.log("[SIGTERM] Received. Shutting down server...");
setTimeout(() => {
process.exit(0);
}, 3000); // Force quit after 3 seconds
await this.stop();
this.stop();
process.exit(0);
});
}
Expand Down
13 changes: 7 additions & 6 deletions js/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,18 @@ app.on("activate", function () {
* Note: this is only used if running Electron. Otherwise
* core.stop() is called by process.on("SIGINT"... in `app.js`
*/
app.on("before-quit", async (event) => {
app.on("before-quit", (event) => {
Log.log("Shutting down server...");
event.preventDefault();
setTimeout(() => {
process.exit(0);
}, 3000); // Force-quit after 3 seconds.
await core.stop();
core.stop();
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) => {
event.preventDefault();
callback(true);
Expand All @@ -178,5 +177,7 @@ app.on("certificate-error", (event, webContents, url, error, certificate, callba
// Start the core application if server is run on localhost
// 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)) {
core.start().then((c) => (config = c));
core.start(function (c) {
config = c;
});
}
Loading