What is the best way to go about creating an app that waits for a long time before running a task? Let’s say I need my application to wait for 1 year and be precise to the second, what would be the best way to achieve this in ASP.NET?
I know about background services but if the I’m not sure they’re intended for these cases.
11
Assuming that these tasks run very infrequently, it probably isn’t worth having a process run all the time. Instead I would suggest using an OS feature such as Scheduled tasks or at
to start the process shortly before (typically seconds) it is required.
As the application starts up it should open database connections, prefetch any data it needs do any pre-calculations required. Once initialized, the app can check the current time and sleep
for however long required until the task needs to run.
Note: Even system sleep
calls can be a little imprecise, but that should be good enough for “second” precision
Once the job is complete the process can simply exit.
Per gnasher729’s comment, the computer does have to be on in order to run a program and you may want to ensure it’s running NTP to ensure that the clock is correct.
If it is absolutely critical that the process runs at the specific time you may need to look into HA solutions running across geographically diverse data centers.
You should also investigate solutions for distributed scheduling since you will likely run into CAP restrictions.
TL;DR – You probably can’t absolutely guarantee it runs exactly once at the precise time you are looking for – you will need to make some compromises / assumptions.
1
Are we there yet?
No.
Are we there yet?
No.
This is called polling. It lives at the heart of every system. One thing is very true about polling. The fewer processes doing it the better.
That’s why we invented scheduling. Rather than running every random thing that decides it needs to ask if we’re there yet over and over we run one thing that calls things that want to be woken up once we’re there.
Every system has a solution for this. Please use it.
1
1 year is an extreme wait, but I think the best design is to have an always running process which continually checks for or delays between runs. For example a windows service
The benefit of this over a scheduler is that you can give it a health check and alerting for when it goes down or fails to run.
Too often with schedule tasks they don’t run for some reason and you don’t notice. It’s hard to take an alerting action when nothing is running, and to check every year that the once a year process has run is just scheduled tasks all the way down.
A windows service (or k8s console app with a loop) can
- expose a http health check endpoint
- which can be constantly monitored by the same altering tool you use for the rest of your stuff.
- save the last run date to disk
- restart if it crashes
- auto start on reboot
- return a bad healthcheck
- if the run did not occur as expected
- if the run took too long to complete
- if the service hangs in some non fatal way