MagicMirror icon indicating copy to clipboard operation
MagicMirror copied to clipboard

Async node_helper start()

Open MikeBishop opened this issue 4 years ago • 3 comments

If a node_helper uses an async method for start(), it appears that MagicMirror declares the system ready as soon as the function returns the initial Promise. It would be nice if MM would collect the promises from the various modules, then await Promise.all(...) of them.

Steps to Reproduce: Use the async/await pattern within a node_helper's start() method

Expected Results: Initialization completes when all node_helpers are done initializing

Actual Results: MagicMirror reports ready while node_helper is still printing loading status to console

MikeBishop avatar Mar 17 '21 19:03 MikeBishop

Is there a module you can point to that uses async start?

rejas avatar Mar 17 '21 21:03 rejas

I've been trying to use it in my own module, MMM-Powerwall. I borrowed logic from MMM-RemoteControl that reads config files off the disk, and I'd already used the async file access methods elsewhere.

MikeBishop avatar Mar 23 '21 18:03 MikeBishop

but u only need to use async functions if u intend to use await. which is similar to using sync calls. there are a few different approaches to using async and await without doing it with start. you can start a timer, or use the notification call from module to node helper.

sdetweil avatar Mar 23 '21 19:03 sdetweil

Hi @MikeBishop finally came around to look at some old issues, and whipped up a PR for your suggestion. Seems to work on my local setup but I want to get a little feedback first before I proceed.

rejas avatar Oct 03 '22 17:10 rejas

Fixed with #2928

rejas avatar Oct 29 '22 15:10 rejas

So there's apparently an issue with waiting for modules that take a long time. It looks like if initialization of the modules gets pushed out past a certain point in electron's loading process, electron will display a blank screen locally. Accessing with a browser works fine, because everything initialized by then.

I think I have a clean repro of the issue in https://github.com/MikeBishop/MMM-asyncdeath – the synchronous start function produces this in the logs and loads normally:

[21.01.2023 14:55.02.692] [LOG]   Synchronous start-up -- all is well
[21.01.2023 14:55.02.693] [LOG]   Sockets connected & modules started ...
[21.01.2023 14:55.03.235] [LOG]   Launching application.

The async start function produces this in the logs and fails:

[21.01.2023 14:52.54.143] [LOG]   About to break by using await
[21.01.2023 14:52.54.884] [LOG]   Launching application.
[21.01.2023 14:52.56.512] [LOG]   Sockets connected & modules started ...

Note the reversed order of “Launching application” and “modules started”. Seems feasible this could also happen with a synchronous module startup that takes a long time, but I haven’t tested that.

Seems like there's an additional step needed here to delay Electron's startup until the modules are done loading.

MikeBishop avatar Jan 21 '23 20:01 MikeBishop

A quick look before bedtime makes me think the problem only occurs when starting MM with electron and not when in servermode?

rejas avatar Jan 21 '23 22:01 rejas

Seems like electron.js calls createWindow (when the electron app is "ready") but config isnt yet set (in the start callback at the end) due to the async nature....

rejas avatar Jan 21 '23 22:01 rejas

A quick look before bedtime makes me think the problem only occurs when starting MM with electron and not when in servermode?

Correct -- web browsers connecting once everything finishes startup are fine. I think it's probably that electron launch fails (or fails to connect) and then never retries. If so, perhaps rather than delaying electron start-up, we could kick electron to retry once the backend is ready.

MikeBishop avatar Jan 22 '23 03:01 MikeBishop

I dont like delaying stuff arbitrarily since we dont know how long some modules might take. Maybe my PR solution is enough and a little more future-proof

rejas avatar Jan 22 '23 08:01 rejas

@MikeBishop did you have time to check out the fixed develop branch?

rejas avatar Feb 03 '23 19:02 rejas

Belated, but I reverted the work-around in my module and cherry-picked in this change, and MM is able to start up properly.

MikeBishop avatar Mar 16 '23 17:03 MikeBishop