cfschedule delete – workaround
Last week I learned that cfschedule has some annoying behavior when trying to call the delete action. If a scheduled task tries to delete itself (or update itself) through the cfschedule tag, it won’t actually delete the task and will continue to run until the server is restarted. This post describes a way for a scheduled task to delete itself.
This has been identified as a known issue in ColdFusion MX and is addressed in a TechNote. Macradobe suggests that we “Delete tasks through cf administrator” or “restart the cf server”. Neither one of these is an option for me if I want to programmatically delete my task.
The Adobe techNote is somewhat misleading, it turns out that delete does work through the cfschedule tag, but the problem arises when the scheduled task tries to delete itself.
The solution? Since cfschedule – delete works on a normal http request, but not on a “scheduled” http request, we need to make an asyncronous HTTP call from the task. This can be chieved using a cool tool called asyncHTTP written by Mark Mandell (Look at the blog posting here)
Now, within the request that is called from the scheduled task, I can include the following code.
1 2 3 4 5
<!--- scheduledTaskExecute.cfm ---> <cfset asyncHTTP = createObject("component","asyncHTTP.AsyncHTTP").init() /> <cfset asyncHTTP.get("http://localhost/destroyScheduler.cfm") />
The code to delete the scheduled task will now work…
1 2 3 4 5 6 7 8
<!--- destroyScheduler.cfm ---> <cfschedule action="delete" task="myTask" > <!--- or (another unsupported way to do this) ---> <cfset factory = CreateObject("java", "coldfusion.server.ServiceFactory") /> <cfset factory.CronService.deleteTask("myTask") />
Implementing this functionality allows us to process a queued stack in the background then delete the task when it’s complete. This is a powerful tool that allows us to execute code at a scheduled time, then continue executing until a condition has been met.
Mike Schierberl is a software engineer located in San Francisco, CA. He specializes in Cloud Computing, E-Commerce, and Continuous Integration.