I have an application that is a data poller/updater and there are tons of If/Thens around whether it’s a certain time on the clock.
In other words, this program runs from the scheduler on multiple schedules, each time passing an operation argument. From there the first check is a check of the current hour and minute and from that the program decides whether this is the right time to run the data poller.
Now you might wonder why if I run on a schedule then do I care about what time of day it is, why not just run at every interval? Well the data source in this case is from a different group in my company and they have a governor on the number of hits it’ll take. They can’t be reasoned with either, so I limit the number of hits I make to those that have a good chance of returning data.
So back to the problem, in most cases I am after a set of data that’s updated at the source once a day. Currently I run on like a 5 minute interval, but only go looking for new data when it’s after 8 PM.
My question is, I have all these nasty if/then statements that compare the current time to a set value, which then always leads me to think that I should make them runtime configurable, which leads to configuration code and in the end I have a rats nest of code.
Is there any better pattern that I am not thinking of, or some type of package that’s built for problems like this?
Here is something that I have used:
A Schedule object with the Task to do and a Time to do it (or a list of times to do it).
Collect all your tasks in a list, and read through this list every five minutes, to check whether some of them should be run.
This should reduce the If/then statements, and also make it much easier to unit test.
Hope this helps
/Morten