What I’m trying to do:
Schedule a background for a set interval that is reliable. For example every minute or every five minutes and have some confidence (with some margin of error) that the task will be triggered on that interval.
What I’ve tried and what’s not working:
I set up a background task to run every minute, then left it overnight. When I came back in the morning it seems that the tasks did not actually run every minute and missed some. Is there some inherent limitation to how this is done?
Workaround:
As a workaround, I set up an external API for the app that is triggered by an external source (a cron job scheduler basically). This seems to work well. However ideally this will be possible without an external tool.
It’s possible that some tasks aren’t launching because the previous task hadn’t finished before the next task was set to launch. How long does your background task take to run each time?
You can call an API concurrently and each one will run separately, unlike scheduled tasks which act as @brooke says. That’s probably what you’re seeing.
edit you can track this by logging the scheduled task start and end times to a data table.
I wrote a small Anvil app to investigate the regularity of Scheduled Tasks planned to run “every 1 minute”.
The function called by the task simply adds a row containing the current datetime to a data table.
At this link you can see the percentiles of time between successive datetime values saved in the data table.
Since the function returns in an instant, I don’t think this should be affected by what @brooke and @david.wylie referred to above (previous ST not terminating before the next one is triggered)
My summary:
In the majority of cases (>= 96%), the ST is triggered 60-67s after the previous one.
Due to this period sometimes exceeding 60s, it can happen that some “calendar minutes” never have a task triggered (may or may not be relevant depending on your purposes).
In some cases (< 3%), a task is triggered less than 3 seconds after the previous one.
In some cases (< 1%), a scheduled run seems to be skipped and so the time between consecutive tasks greatly exceeds 60s (~120s).
I guess my conclusion is the same as yours @robert: if you need something that runs absolutely like clockwork like a cronjob, you may need to roll it yourself (and it seems that getting creative with scheduled tasks could be a good base for that). But for most purposes like running batch jobs, Scheduled Tasks seem to fit the bill.
If you want to see for yourself, here’s a demo to clone. You’ll have to re-enable the scheduled task. This app logs start and end times of the task (with a 30 sec delay).
Ah gotcha I didn’t even think about the fact that the task may take longer than a minute. But this background task is just checking whether something else needs to be done, then triggers a separate background task if needed.
From the other responses, it looks like my workaround may be the best option for now, and honestly it’s pretty easy. Thanks to everyone that chimed in.
Just to add that this is currently causing issues for me also.
My scheduled task for every minute takes around 30 seconds. If I call it every minute then it misses quite a few due to the random start time overlapping with my previous task. I can iterate over my routine within the scheduled task, but then the stakes are higher - I could iterate 10 times within my task to guarantee 10 successes, but then if my 9 min 30 sec routine is called every 10 mins, if there is an overlap my next scheduled task doesn’t start and I miss a whole 10 minutes without the task running.
Are there any neat tricks to overcome this within Anvil?
Since this topic is marked as solved, your (new) question will be more visible if you start a new topic, with a title matching your specific situation.