I have never been a huge fan of ColdFusion's scheduled tasks. That's just an odd fear based on the fact that I have to rely on something outside of my applications. But, the fact is, scheduled tasks are essential if you want to do anything on a scheduled basis that is NOT triggered by user actions. I am also not a huge fan of creating / modifying / deleting scheduled tasks using the ColdFusion CFSchedule tag as it tends to leave phantom tasks running until the ColdFusion service gets restarted.
In order to deal with all of that emotionally, what I have started doing is creating a single point of entry for scheduled tasks in my ColdFusion applications. I have a single ColdFusion template that launches all of my application's scheduled tasks:
Launch code in new window » Download code as text file »
As you can see, this single point of entry turns around and launches additional scheduled tasks using CFHttp calls. Each CFHttp call is given one second to execute before it times out (without error) and the current page moves onto the next task.
The down side to this is that each task has to know a bit more information. Tasks cannot assume they are only run once every N hours or days. Since this one point of entry is responsible for every scheduled task on the site, it must run as often as the smallest required time interval. This means that a task designed to run once a day might end up getting called once every 15 minutes. That task has to know to ONLY run once per day even though it is "asked" to run much more often.
The up side to this, however, is that you only need to set up one scheduled task in the ColdFusion administrator (per application). If you have any additional scheduled task requirements all you have to do is update your point of entry. You never need to go back to the ColdFusion administrator for task set up for the application (unless, of course, you need to increase / decrease the interval at which the task runs).
So far, I have used this with great success. And, I find that it forces me to write much better, smarter scheduled tasks that don't rely on the scheduled task interval.
Download Code Snippet ZIP File
Comments (7) | Post Comment | Ask Ben | Permalink | Print Page
Printing The Entire jQuery API As A PDF (Using CFDocument And XML Parsing)
Josh Adams Of New Atlanta On AJAX And Blue Dragon At NY CFUG
I like this idea, and I think the cons that you brought up could be remedied with some magic...I am going to investigate more into this design because you and I share the same fears.
Posted by Derek P. on Feb 21, 2007 at 1:59 PM
Derek,
I agree. For instance, one of the sample tasks was an automated email sender. If you were grabbing emails out of a database, you could certainly have a field for date_last_sent which could be updated / read for task execution.
Let me know what kinds of stuff you come up with.
Posted by Ben Nadel on Feb 21, 2007 at 2:07 PM
Ben,
Take a look at this as well. Another way to deal with Scheduled Tasks:
http://labs.redbd.net/projects/cfcron.cfm
Sami
Posted by Sami Hoda on Feb 21, 2007 at 2:32 PM
Sami,
That looks pretty cool. I have not seen that before. I like that the CFC handles the "interval" hand-holding. Very cool.
Posted by Ben Nadel on Feb 21, 2007 at 2:43 PM
The other downside is that if you don't put try-catch in each item if one fails they all fail. Also if you put a bug in your script, none run.
Posted by Chris Dawes on Feb 22, 2007 at 3:17 AM
Chris,
The CFTry / CFCatch is not necessary since the failure will happen in the page being called via CFHttp, not the primary task page. If the page getting called fails, the only thing the calling page knows is that the error message gets returned in the CFHTTP.FileContent attribute; it sees it as just some text... not an error.
But yes, if there is an error in primary page, some or all will fail to run. But of course, that is what testing is for :)
Posted by Ben Nadel on Feb 22, 2007 at 6:27 AM
Ben,
neat idea. I also like the idea of sending your girlfriend text messages (even if it's a fictional idea, he he). On that note, you should replace the "girlfriend" with a variable so it could be used for a "wife" also, lol. Keep up the good work.
Posted by Boyan on Feb 22, 2007 at 10:01 AM