How reliable are alarms?

My extension will run a task every 20 minutes. Basically, my code triggers and then when the function is complete, it creates a new alarm at the end of the code. To do this I’m using the Alarms Javascript API in Firefox.

I seems to work for me, but a couple of my users have complained that after a certain period of time, the alarm will stop triggering or triggers inconsistently.

I’m trying to determine the relability of the Alarms API. Basically, I’m just trying to figure out if there is a bug in my code that stops an alarm from being properly set/triggered or if it’s something to do with the API itself.

Are there any known circumstances in which the Alarm API will fail to be set/triggered?

I have not yet been able to reproduce the issue, but I will be running a long-term test to see if I can get it to happen.

EDIT: The code in question is here, if anyone wants to look to make sure I’m implemented it correctly.

1 Like

I have had such reports too, however I’ve been able to nail down most of them to me passing an invalid interval (because I wasn’t using a recurring alarm). This shouldn’t be an issue anymore either way since Firefox 65 (alarms will immediately trigger if the first trigger time is invalid).

There are also issues with how it behaves with suspending your computer. The timer is bound to the process time iirc, and thus when the computer is suspended, the alarm is also suspended and will keep the time. I think there may also be other issues with this process time approach.

1 Like

@freaktechnik If you suspend your computer and resume later, what happens if the scheduled time for the alarm is now a time in the past?

For example, if the alarm is set for 5 minutes in the future, but I suspend my computer for 10 minutes, what will happen when I go back? Will the alarm just execute automatically? Will it sit there and be invalid forever?

One possible solution I was exploring was to have a function that will check that the alarm is set and that the scheduledTime is set to a time in the future. If it fails those checks, it will be reset. I would just need to tie it to an event that will run regularly. My thought was just to tie to the webrequest listener so that it will run whenever the user loads a new page.

Naturally, this would use more resources, so it’s not something I want to do. But if the alarms won’t work consistently or have issues with suspending computers, I won’t have any other choice but to constantly check the alarm’s validity.

What I was trying to say is that if your alarm is set for 5 minutes in the future it will ignore that the computer was suspended in the mean time. https://bugzilla.mozilla.org/show_bug.cgi?id=1359403

1 Like

Ok, sorry I misunderstood what you wrote above.

So basically, it appears that if the alarm is set for 5 minutes in the future and you suspend your computer, it will still take 5 minutes for the alarm to execute after the computer is resumed. That makes sense.

I would assume this behaviour only occurs if you set the alarm using either delayInMinutes or periodInMinutes, so it’s odd that it also happens if you use the when option instead.

I have two extensions that both use the Alarms API.

The first just uses the delayInMinutes option to itself and it doesn’t really matter to me if it executes strictly on schedule or not.

My second extension though uses the when option and needs to be executed exactly at the time its set to without delay. I guess for this extension, I will have to constantly check the alarm validity.

Under the hood all options to specify when the alarm should first trigger are converted to the same (relative) unit: https://searchfox.org/mozilla-central/source/toolkit/components/extensions/parent/ext-alarms.js#31

3 Likes

Good to know.

Kind of a pretty big flaw. Makes it difficult to implement anything that requires an exact time, like an alarm clock extension (for example).

Thanks for the info!

Actually the Alarm API should be pretty precise (well…, to the exact minute, not second).

I needed to implement scheduler for the Night mode / Day mode (for my Darko Thunderbird addon) and it turns out the Alarm API is great for this.

To clarify:

  • if you setup alarm in the past, it will fire right away
  • if you setup future date and your PC wakes up from hibernation after the date, it will fire right after waking up
  • if you setup 8 hours from now, it will fire 8 hours from now (if your PC is on) no matter how long was your PC sleeping / hibernating in the meantime

This is the first time I’ve used this API and I have to say it’s pretty cool. Much clearer and easier than using window.setInterval and checking current date/time…

Also the permission it requires is in the Not alerted list so it can be added to existing addons.

3 Likes

Thanks for these answers. As [this also answers an old question about the difference to setInterval I’ved once asked, I’have cited and cross-linked it there.

1 Like