New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get a daily schedule to work with an Electron app? #319

Closed
komplexb opened this Issue Dec 28, 2016 · 4 comments

Comments

Projects
None yet
2 participants
@komplexb

komplexb commented Dec 28, 2016

I'm scheduling an action to occur daily at a set time, but it doesn't work (reliably). If scheduled for every 5 minutes, the action seems to occur reliably with the app minimized. If the computer sleeps, when woken the action is run repeatedly (this is fine).

I scheduled the action for Dec 27 @ 9 and it didn't occur. Logging (based on job events) reveals that the schedule was accurately changed, but somehow didn't occur, despite the computer being awake.

I then changed the schedule back to every 5 minutes, and it runs fine. Any advice on what the problem is and how I can go about fixing it? I suspect it has something to do with the computer sleeping for an extended period of time.

Please advise.

function scheduleRandomNote (scheduleVal = storeSettings.getItemSync('schedule')) {
  let rule

  if (scheduleVal === '*/5 * * * *') {
    rule = '*/5 * * * *' // every 5 minutes
  } else {
    rule = new schedule.RecurrenceRule()
    rule.dayOfWeek = [0, new schedule.Range(0, 7)]
    rule.hour = scheduleVal
    rule.minute = 0
  }

  let j = schedule.scheduleJob(rule, () => {
    mainWindow.webContents.send('trigger-random-note')
  })

  j.on('scheduled', (event) => {
    log.info(`scheduled: ${event}`)
    log.info(`scheduled-settings: ${scheduleVal}`)
  })

  j.on('run', (event) => {
    log.info(`run: ${event}`)
    log.info(`run-settings: ${scheduleVal}`)
  })

  j.on('canceled', (event) => {
    log.info(`canceled: ${event}`)
    log.info(`canceled-settings: ${scheduleVal}`)
  })

  // return job instance so we can cancel it when schedule is updated
  // via settings
  return j
}

ipc.on('reset-schedule', (event, arg) => {
  scheduleInstance.cancel()
  log.info(`reset-schedule: ${arg}`)
  scheduleInstance = scheduleRandomNote(arg)
})

Logs

2016-12-27 | 12:01:15:0594 canceled: Tue Dec 27 2016 13:00:00 GMT-0500 (EST)
2016-12-27 | 12:01:15:0597 canceled-settings: 13
2016-12-27 | 12:01:15:0597 reset-schedule: */5 * * * *
2016-12-27 | 12:05:10:0591 scheduled: Tue Dec 27 2016 12:10:00 GMT-0500 (EST)
2016-12-27 | 12:05:10:0593 scheduled-settings: */5 * * * *
2016-12-27 | 12:05:10:0599 run: undefined
2016-12-27 | 12:05:10:0600 run-settings: */5 * * * *
2016-12-27 | 12:05:34:0184 canceled: Tue Dec 27 2016 12:10:00 GMT-0500 (EST)
2016-12-27 | 12:05:34:0184 canceled-settings: */5 * * * *
2016-12-27 | 12:05:34:0184 reset-schedule: 13 // Computer sleeping, didn't run Dec 27 @ 1
2016-12-27 | 16:57:57:0132 canceled: Tue Dec 27 2016 13:00:00 GMT-0500 (EST)
2016-12-27 | 16:57:57:0134 canceled-settings: 13
2016-12-27 | 16:57:57:0135 reset-schedule: 18
2016-12-27 | 18:03:30:0454 scheduled: Wed Dec 28 2016 18:00:00 GMT-0500 (EST)
2016-12-27 | 18:03:30:0456 scheduled-settings: 18
2016-12-27 | 18:03:30:0456 run: undefined // computer awake, ran Dec 27 @ 6
2016-12-27 | 18:03:30:0457 run-settings: 18
2016-12-27 | 18:05:43:0057 canceled: Wed Dec 28 2016 18:00:00 GMT-0500 (EST)
2016-12-27 | 18:05:43:0059 canceled-settings: 18
2016-12-27 | 18:05:43:0059 reset-schedule: 9 // Computer awake, didn't run Dec 28 @ 9
2016-12-28 | 10:48:31:0685 canceled: Wed Dec 28 2016 09:00:00 GMT-0500 (EST)
2016-12-28 | 10:48:31:0687 canceled-settings: 9
2016-12-28 | 10:48:31:0687 reset-schedule: */5 * * * *
2016-12-28 | 10:50:10:0690 scheduled: Wed Dec 28 2016 10:55:00 GMT-0500 (EST)
2016-12-28 | 10:50:10:0692 scheduled-settings: */5 * * * *
2016-12-28 | 10:50:10:0693 run: undefined
2016-12-28 | 10:50:10:0693 run-settings: */5 * * * *
2016-12-28 | 10:55:00:0681 scheduled: Wed Dec 28 2016 11:00:00 GMT-0500 (EST)
2016-12-28 | 10:55:00:0684 scheduled-settings: */5 * * * *
2016-12-28 | 10:55:00:0684 run: undefined
2016-12-28 | 10:55:00:0685 run-settings: */5 * * * *
@santigimeno

This comment has been minimized.

Show comment
Hide comment
@santigimeno

santigimeno Jan 12, 2017

Member

@komplexb strange issue indeed. Are you still experiencing the issue? From what I can tell, in the code there's an issue when you define the rule:

rule.dayOfWeek = [0, new schedule.Range(0, 7)]

defining it like this should suffice:

rule.dayOfWeek = new schedule.Range(0, 6)

Apart from that I don't seen anything obviously wrong.

Another thing you could do is trying to reproduce the scenario you described in the issue by using sinon fake timers (if you're not familiar with them, see the tests for examples).

One more thing... if the computer goes to sleep, strange things happen

Member

santigimeno commented Jan 12, 2017

@komplexb strange issue indeed. Are you still experiencing the issue? From what I can tell, in the code there's an issue when you define the rule:

rule.dayOfWeek = [0, new schedule.Range(0, 7)]

defining it like this should suffice:

rule.dayOfWeek = new schedule.Range(0, 6)

Apart from that I don't seen anything obviously wrong.

Another thing you could do is trying to reproduce the scenario you described in the issue by using sinon fake timers (if you're not familiar with them, see the tests for examples).

One more thing... if the computer goes to sleep, strange things happen

@komplexb

This comment has been minimized.

Show comment
Hide comment
@komplexb

komplexb Jan 12, 2017

@santigimeno thanks for getting back in touch. I did some refactors that included reapplying the schedule each time the computer wakes up. That solved it.

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

function rescheduleRandomNote (scheduleVal) {
  scheduleInstance.cancel()
  log.info(`reschedule-notes-to: ${scheduleVal}`)
  scheduleInstance = scheduleRandomNote(scheduleVal)
}

komplexb commented Jan 12, 2017

@santigimeno thanks for getting back in touch. I did some refactors that included reapplying the schedule each time the computer wakes up. That solved it.

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

function rescheduleRandomNote (scheduleVal) {
  scheduleInstance.cancel()
  log.info(`reschedule-notes-to: ${scheduleVal}`)
  scheduleInstance = scheduleRandomNote(scheduleVal)
}
@santigimeno

This comment has been minimized.

Show comment
Hide comment
@santigimeno

santigimeno Jan 12, 2017

Member

@komplexb that's great news and thanks for sharing your workaround!. Can this be closed then?

Member

santigimeno commented Jan 12, 2017

@komplexb that's great news and thanks for sharing your workaround!. Can this be closed then?

@komplexb

This comment has been minimized.

Show comment
Hide comment
@komplexb

komplexb Jan 12, 2017

komplexb commented Jan 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment