7

Hoping someone can point me in the right direction here. My electron app needs to perform an API call every 10 minutes or so. I currently do this using a setInterval loop in the renderer process that fires every 10 minutes.

It generally works fine, for a few hours before it seems to just stop firing. I have several processes that clear and restart the setInterval to try and counteract the problem, but nothing seems to work.

The app opens new browser windows and displays content, which mean the main window may not be in focus all the time, which I suspect may have something to do with it.

I have tried adding

powerSaveBlocker.start("prevent-display-sleep");
powerSaveBlocker.start("prevent-app-suspension");

to my main electron js script, but it doesn't seem to have an effect. The problem is mostly being displayed on Windows machines. I'm not entirely sure if it happens on Mac or Linux.

So my question, is there any reason why this would be happening that intervals just die after a point? The powerSaveBlocker calls made sense to me, but they don't really seem to do anything.

Or is there a better way to have a background process running at intervals that can perform these API calls? I had looked at node-schedule but I'm not sure if it will fix this issue.

Cheyne
  • 1,964
  • 4
  • 27
  • 43
  • Could you post a minimal but complete example that allows to reproduce this behavior? Since testing takes at least an hour, this would go a long way. Also, are you on the newest Electron version? Have you tried to disable [`webPreferences.backgroundThrottling`](https://electronjs.org/docs/api/browser-window#new-browserwindowoptions)? – snwflk Jan 03 '20 at 00:07
  • Interesting, I didnt know about that setting. Thanks, let me give that a try. Maybe post that as an answer incase it works so I can mark it so. – Cheyne Jan 04 '20 at 01:15
  • 1
    Also check your Electron version, there seems to have been changes wrt `backgroundThrottling` in [7.1.4](https://electronjs.org/releases/stable#7.1.4) – snwflk Jan 04 '20 at 01:17
  • Interesting, im on 5.0.6 currently. Thanks @snwflk – Cheyne Jan 04 '20 at 01:21
  • Just following on from @snwflk's comment, it seems the bug was that until 7.1.4 you couldn't switch the background throttling off, meaning timers are always throttled: https://github.com/electron/electron/issues/20974 This almost certainly explains the problem, perhaps snwflk can post those comments as an answer? – Darren Cook Jan 04 '20 at 08:59
  • @Cheyne: Has your issue been resolved? If yes, maybe *you* can post this as an answer - seeing that you indeed have concrete data to show. – snwflk Jan 04 '20 at 16:54
  • I only tested Electron 5.0.4 vs 7.1.7 on Linux and found that both timers worked for over 60 minutes. Timers were set with a one minute interval on the renderer process. The BrowserWindow was minimized during my tests. – snwflk Jan 04 '20 at 16:54
  • I've bumped the electron version to 5.0.13 and im currently testing with an active customer now. So far, it's looking better (I think). I'll do a little more testing myself although the problem was always more present on my customers Windows based machines, where as i'm using a Macbook. It feels like the right solution though. I'll post the answer later today if @snwflk doesn't post it, it's really his answer and he deserves the credit. – Cheyne Jan 05 '20 at 00:29

2 Answers2

11

Answering my own question here with credit to @snwflk who pointed me in the right direction in a comment on the original post.

Whilst I have not been able to clarify with absolute certainty this solves the problem, I have also not seen the problem since. (It's not always 100% reproducible, and its difficult to test as it requires a machine, left alone for several hours, which may or may not display the problem).

I have however rolled the fix out to a few customers and their machines seem to still be online days later, which is a good sign.

So, the solution was to disable backgroundThrottling on the main BrowserWindow object (interval was running in the renderer processes)

Docs: https://electronjs.org/docs/api/browser-window#new-browserwindowoptions

An example

mainWindow = new BrowserWindow({
    webPreferences: {
        backgroundThrottling: false,
    },
});

FYI be warned there have been a few bugs that prevented this setting from working, ie https://github.com/electron/electron/issues/20974 so be sure to update your electron version.

Cheyne
  • 1,964
  • 4
  • 27
  • 43
1

As far as I know intervals should keep running forever (the MDN page also doesn't mention anything).

If I understood correctly, your Electron app only does that API call? So it would be hard to tell the difference between the interval not triggering, and some other problem causing a freeze?

My guess would be you have an uncaught exception being thrown, or some other similar error or event. https://stackoverflow.com/a/43911292/841830 gives some suggestions of things to catch. https://github.com/sindresorhus/electron-unhandled says it can be used in both main and renderer processes to catch and report all problems.

Or is there a better way to have a background process running at intervals that can perform these API calls?

If you were on Linux, using cron to run a node script (or even just a wget or curl command) would be better than using an Electron app just for this. Task scheduler seems to be the windows equivalent of cron.

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • Thanks for the response, the application does several things, mostly displaying content on browser windows, the interval API call is to do a license check and ensure the customer has a valid license. Its not possible to do this outside of the app. I suspect that maybe because a new browser window is in front of the main window, its maybe putting the thread to sleep. I have exception handling and monitoring already, there are none being thrown. – Cheyne Jan 02 '20 at 22:17
  • @Cheyne I just saw snwflk's comment, and `backgroundThrottling` (which defaults to `true`) does seem a possible cause, doesn't it. – Darren Cook Jan 03 '20 at 08:42
  • Yes interesting, didnt know about that one. I will give that a try and see how it goes. I'll post back when I know more. – Cheyne Jan 04 '20 at 01:15