Scheduled events after database restore, why do none of my timed triggers work after restoring a backup? When do sunrise sunset events get scheduled because after restoring my backup none of my evening scheduled events fired. THIS is why i didn't want to have to restore a backup.

Haven't seen that problem. Restore db's all the time, everything that is scheduled keeps on as before...

When does it schedule sunrise and sunset events?

When does what schedule them? Subscriptions are in the database, as are schedules. I'd imagine that the restored database has the exact state it had at the time it was backed up.

Yes, but the backup was made at 2am and I restored it at some time in the afternoon. So, the scheduler has to run sometime to check when sunset is and schedule events that are offset from sunset. When does that function occur? It can't happen constantly. Every second it is checking every rule to see if there is a schedule event that might need to take place? Of course not. They are schedule events. For example, when i look at my rule that has a scheduled event at 60 minutes past sunrise, this is what is see in Scheduled Jobs.

But if i look at my sunset routine, this is already scheduled for tomorrow:

So, when do those events get scheduled? And if they have already past when the db is restored, then how would events not get missed. I had to go 2 days back to get an unaffected database. So, whatever was scheduled in that database passed yesterday.

Sunrise and sunset events are not scheduled, they are events generated by the hub.

Wouldn't that be because it is already after sunset today?

No....because the sunset event for tomorrow is already scheduled but the sunrise event for tomorrow is not.

Okay....they are events generated by the hub....but, if i have a trigger for 20 minutes before sunrise, it can't trigger off of sunrise occurring. Something, at some point has to look at when sunrise is and apply an offset. I don't understand why you're being so difficult.

There is a scheduled job for my rule that looks at sunset tomorrow - 20 minutes. When did that scheduled job get created?

Okay, here's another way of putting it. The beforeSunsetHandler seems to schedule the job for things happening before sunset. When does this handler do that? Because, if you restore an old database, and this handler doesn't run to reschedule the jobs, then things are going to get missed, right?

Ha ha, you guys. I always enjoy watching the love hate relationship between you two :smile:


Yes, it is an imperfect thing, restoring one's database. Beats the alternative, wouldn't you say? You could also have gotten around the bug by removing Rule Machine and all of your rules, and starting over.

Bruce, I am not saying that restoring the database wasn't the best solution available. We can debate if another option should have been available but I'm not doing that. I'm trying to understand what's going on so that if I ever have to restore a backup again, which is a strong possibility, I can correct these things ahead of time instead of hours after they were missed. I'm simply looking for information.

So, when does beforeSunsetHandler handle the sunset time? Is it at sunset the day before? For example, will the job scheduled for 20 minutes before sunset tomorrow be scheduled at (or shortly after) sunset today? I don't know when the system updates the "sunset time" to the next day's sunset so I don't know when tomorrow's job gets scheduled. Knowing that would be the first step in knowing how to recover properly from a database restore.

Since it appears that the afterSunsetHandler and afterSunriseHandler schedule job at the time of sunset or sunrise, respectively, the odds of missing one of those events after a restore are relatively low. If you're executing an event 20 minutes after sunrise, to miss that event you would have to do the restore between sunrise and 20 minutes later. Pretty slim chance of that. But, if the "before scheduler" is scheduling those jobs almost 24 hours before they are supposed to happen, the odds of missing one are quite high. So, if i had to restore the database, i would have to address rules that looked at before sunset and sunrise but most likely not those that looked at after. THAT is why I am trying to understand when these jobs get scheduled.

This is actually a fairly complex topic.

You are correct that After Sunrise and After Sunset are scheduled by the sunrise and sunset events.

The hub event for tomorrow's sunriseTime happens along with the sunrise event, immediately after it. Same is true for sunsetTime, tomorrow's sunset. You can see these in Location Events.

Where it gets complicated is when you hit Done in an app that uses sunrise/sunset related times with offsets. What happens depends on when you hit Done and what kind of offset is used.

If you hit Done for After Sunset during the window of time between sunset and the offset time, it calculates how far in the future that is and schedules it. The same is true for After Sunrise when Done is hit during the window between Sunrise and the offset after. If you hit Done outside those windows, it simply uses the sunset or sunrise event to schedule the event on subsequent days.

Before Sunrise and Sunset are trickier. There are location variables called sunrise and sunset that provide today's times. If Done is hit after sunrise for a Before Sunrise offset, meaning that the sunriseTime event giving tomorrow's sunrise has already happened, an event is scheduled for just after midnight. What happens then is that the time from then to sunrise is calculated and scheduled (first day of before sunrise). Subsequent days use the sunriseTime event to schedule for the next day. Before Sunset works the same way when Done is hit after sunset. There is a known flaw in this for Before Sunrise (or Before Sunset) offsets that are so large as to be before midnight of the day when Done is hit. Those events will be missed on the first day. The only available fix for this flaw would be to use today's sunrise or sunset time for the events happening before tomorrow's sunrise or sunset that would actually happen today due to the large offset, as there is no location variable that gives tomorrow's sunrise or sunset times, only the event that gives them, which event has been missed. There are probably flaws related to offsets greater than 24 hours also.

Now, is that perfectly clear?

Simple answer: After db restore open any app that uses before sunrise or before sunset offsets and hit Done for it to be properly scheduled for tomorrow.

1 Like

THAT IS EXACTLY WHAT I WAS LOOKING FOR!!!!! :+1::clap::ok_hand: I should say, after the last few hours I would have settle for a "yes", so this is far above my expectations. Thank you!!!!!

I truly am just trying to learn. Can't do that unless you geniuses with all the info cough it up every now and then, right? :smiley: Again, thank you.

Now, if you REALLY want to hear what I'd love...a way to programatically "hit done" on a rule. So, basically, through software, hit done on a rule to cause that scheduling to happen. Then, eventually, maybe even have that happen automatically after a database restore. After all, RM knows which rules are subscribed to those handlers. Maybe in rule 6.0. :wink:

All I can tell you is that this is a godawful chunk of code that handles this scheduling of offsets, and it's still incomplete as described above.

Oh, I don't doubt it. But this at least is a good start and much more information than I had an hour ago. :slight_smile: