How to schedule a daily task in an Electron app

Solution to a problem I encountered while building Notifyer

diligent_notification.gif

One of the main features of Notifyer is the ability to schedule quotes (or really any message) to be triggered once daily. The thing is I couldn’t get the schedule to work overnight or even a few hours later. I’d suspend my computer, wake it later, stare at the clock as the scheduled time passed and nothing would happen.

After adding some logging, re-reading node-schedule documentation, and filing an issue, I finally stumbled upon a solution.

electron.powerMonitor.on('resume', () => {
    log.info(`computer awake: refresh schedule for ${scheduledFor}`)
    rescheduleRandomNote(scheduledFor)
})

Schedule

It occurred to me, probably after reading through old node-schedule issues, that perhaps the problem is with suspending the computer. If node-schedule somehow forgot my instructions when it sleeps, then maybe I need to help it remember when it wakes. So I re-applied the schedule on the resume event; and it worked!

 
0
Kudos
 
0
Kudos

Now read this

What I learned when gh-pages wouldn’t work

gulp deploy hmm, that was fast, did it update anything? *checks Sourcetree*, nope nothing. Maybe it’s a fluke, try gulp deploy again, checks GitHub still nothing, aargh! whats happening!?!? Checks gh-pages docs, hmm it has a command line... Continue →