A Few of My Favorite Things from ColdFusion 10: The All-New Scheduler
Posted 15 May 2012
Now that ColdFusion 10 has been officially released, I’d like to take some time to talk about some of the new features that I find to be real productivity enhancers and features that open new possibilities for work. I’m not going to discuss how the features work (i.e.; the code) because there are a lot of great resources one Google search away that show you how to do that (and there’s the always-underutilized ColdFusion docs!). If you just want to see the list of new functions in ColdFusion 10, you can also check out this page from the ColdFusion 10 docs.
Although ColdFusion 10 has a lot of new features, one of my personal favorites is the all-new scheduler. My team relies heavily on scheduled tasks for the work that we do. In versions of ColdFusion prior to CF10, you had very limited control over the running of a scheduled task. If something went wrong in the scheduled task, you had to handle the errors in a pretty non-graceful way: try/catch blocks with an email message to the developers letting you know things had bombed out, or another scheduled task to check a log file to ensure that the scheduled task ran. If you wanted fine-grained control over the running of scheduled tasks (i.e.; run this task every Tuesday and Thursday at 3am), you’d have to write custom logic in your scheduled task to handle this because the timing options were somewhat limited. If you wanted to have one task chain into another to ensure a sequence of events, you’d have to write everything as a single task and chain things together in that single task (which, again, could fail and then you’d have to handle the failure for multiple parts and not just one).\r\n\r\nColdFusion 10 incorporates the pretty awesome Quartz Scheduler and makes using scheduled tasks a whole lot more powerful. Again, if you want to see the code and all the feature options, you can check out the ColdFusion 10 docs for <cfschedule>. Here’s a recap of what I find to be so useful about the new scheduler:
-
Task Chaining: You can now chain one task to another, so that if you have a series of dependent tasks, you can break them up into discrete, re-usable components and then chain them together in a series of linked scheduled tasks. Let’s say that before you send off a mass email to all of your customers, you need to first run a task that checks for expired accounts. Once that’s run, you then send the mass email. You could build this as a single scheduled task, but then you’d have code you’d probably need to use elsewhere (the check for expired accounts) mixed up in your scheduled task. In ColdFusion 10, you can chain tasks together so that task A runs, and then calls task B, which can call task C, and so on. You keep each of your individual components as separate tasks, which makes each one more encapsulated (a good thing) and reusable (also a good thing). You can also specify what to do if something goes wrong in the chain, using the onException() attribute of any scheduled task in the chain. Which leads me to…
- Task Events: As mentioned above, in previous versions of ColdFusion, you had no real way of knowing if a task completed other than checking the <cfschedule> log in the ColdFusion administrator, or writing code which emailed you or wrote to a database table if a task succeeded or failed. If the task aborted mid-process for some reason, you might not even get that information in an email or database entry. In ColdFusion 10, you can specify a CFC (ColdFusion component) to handle events surrounding the running of a scheduled task. These events include:
- beforeExecute — works like an init() method for this component
- onTaskStart — runs when your task is just about to start
- onTaskEnd — runs when your task (as defined in execute()) finishes running
- onMisfire — runs when CF can’t run the scheduled task (usually due a lack of available threads or other system resource problems)
- onError — when there’s an error in your scheduled task code
- execute — where your scheduled task code actually goes
Note that if you need to use task events, you have to put your task code in the specified CFC’s execute() method. You don’t use a CFM to hold your task code any more. This is actually really nice because this CFC can be part of your existing application, and managed by the always-awesome ColdSpring, or another dependency injection framework.
- cron / New Date/Time Scheduling Options: cron is the Unix-standard way of scheduling tasks in any number of environments. ColdFusion 10 brings the power of cron to scheduled tasks in our favorite programming language. In previous versions of ColdFusion, if you wanted to schedule a task every half hour between the hours of 8am and 5pm, you had to write custom logic in your scheduled task to check the time to do this. ColdFusion couldn’t handle this for you. cron makes this a breeze. cron will let you say "Fire this task at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday" or "Fire this task on the 5th and 20th of every month." Cron uses a very simple, text-based format to specify the day of week, month, day of month, hour, and minute on which a task should run. It’s highly flexible, and enables you to set some pretty crazy schedules for your tasks without having to write all those date/time checks in to your scheduled task logic.
There’s a lot more to the all-new scheduler in ColdFusion 10 (groups, app-specific scheduled tasks, repeat options, priority scheduling), but these three features, I think, have the biggest implications for changing how you use scheduled tasks in ColdFusion. I find them to be a very welcome addition to ColdFusion 10, and hope you do too!