Day 14: ColdFusion 10 Schedule Enhancement Task Handler

Day 14 on ColdFusion 10, I haven’t cover much in last 13 days but go deeper in functionality. Today I keep working with schedule task enhancement and research on event handler feature. This will let us define functions which will call on particular event. Following event/method supported in event handler CFC.

  • onTaskStart : Call every time when task start, If function return false will not execute schedule.
  • onMisFire : Call anytime schedule file misfire (other than schedule time) any how it only fire first time for me when I create schedule file and but later on manual call doesn’t invoke.
  • onTaskEnd: Call execution of task end.
  • onError: Call on exception on task, either 404 or 500 error. P.S. This will not invoke in case of syntax error in your coldfusion page.
  • execute : ColdFusion 10 allow you to write code in cfc function instead of calling any URL, if you do not provide URL while creating schedule file then it will look for execute function in your event handler but URL will get priority if both available.

We just need to specify dot (.) qualified cfc path in eventhandler parameter. May be your code look like below.

[code:cf]
<cfschedule startDate="11/11/2011" starttime="12:01 AM" interval="30" action="update" task="MyGroupedTask" 
eventhandler="cf10.day14.schedulehandler" />
[/code]

Please note that URL attribute is not provided so it will call execute function in my schedulehandler.cfc. Here is code for schedule event handler code.

[code:cf]
<cfcomponent output="false" implements="CFIDE.scheduler.ITaskEventHandler">
<cffunction name="onTaskStart" returntype="boolean"> 
        <cfargument name="context" type="struct"/> 
        <cffile action="append" file="#expandPath(‘./log.txt’)#" output="On task start called #now()#">
        <cfreturn true> 
    </cffunction> 
    <cffunction name="onMisfire" returntype="void"> 
        <cfargument name="context" type="struct" required="false"/> 
        <cffile action="append" file="#expandPath(‘./log.txt’)#" output="Misfire called #now()#">
    </cffunction> 
    <cffunction name="onTaskEnd" access="public" returntype="void"> 
        <cfargument name="context" type="struct" required="false"/> 
        <cffile action="append" file="#expandPath(‘./log.txt’)#" output="Task end called #now()#">
    </cffunction> 
    <cffunction name="onError" returntype="void"> 
        <cfargument name="context" type="struct" required="false"/> 
        <cffile action="append" file="#expandPath(‘./log.txt’)#" output="Error called #now()#">
    </cffunction> 
    <cffunction name="execute" returntype="void"> 
        <cffile action="append" file="#expandPath(‘./log.txt’)#" output="Executed called #now()#">
    </cffunction>
</cfcomponent>[/code]

To track when each event called I just added timestamp in log file. On execution of code to create schedule file invoke misFire event and my log file look like below after first schedule execution.

Misfire called {ts ‘2012-06-09 14:36:42’}
On task start called {ts ‘2012-06-09 14:37:00’}
Executed called {ts ‘2012-06-09 14:37:00’}
Task end called {ts ‘2012-06-09 14:37:00’}

Notice gap between misfire and ontaskstart log statement. "Misfire called {ts ‘2012-06-09 14:36:42’}" wrote in my log file first time when I create schedule task first time. Manually running schedule task from coldfusion administrator will NOT invoke onMisFire.

After playing with misFire, now it is time for onError. onError will called whenever exception occur in task but I get confused when I tried to test it out. Below are results. I just create CFM file with below code

[code:cf]
<cfthrow errorcode="500" detail="Error Occured">
<cffile action="append" file="#expandPath(‘./log.txt’)#" output="Schedulefile called">
[/code]

 

And schedule this file with my above exception handler and thought It will invoke onError handler but it didn’t, it simply called onTaskEnd. My log file contains text like below.

[code:cf]
On task start called {ts ‘2012-06-09 14:55:30’}
Task end called {ts ‘2012-06-09 14:55:30’}
[/code]

I have fire up URL in browser directly to make sure page shows error and it did show new coldfusion 10 custom error template. And here is issue, since custom template return code 200 task scheduler consider that page run correctly. To invoke onerror handler just override coldfusion custom template by adding onError method in my Application.cfc file and added cfheader to return error code instead of 200. onError function will look like below.

[code:cf]
<cffunction name="onError">
<cfargument name="Exception" required=true/>
<cfset var errorcode = 500>
<cfif structKeyExists(arguments.exception,"errorcode")>
<cfset errorcode = arguments.exception.errorcode>
</cfif>
<cfheader statusCode="#errorcode#" statusText="Error occurred">
<cfoutput>#errorcode# : Error occurred</cfoutput><cfabort>
</cffunction>[/code]

Finally I get success to invoke onError handler and my file log has text like below.

[code:cf]
On task start called {ts ‘2012-06-09 15:05:00’}
Error called {ts ‘2012-06-09 15:05:00’}
On task start called {ts ‘2012-06-09 15:05:05’}
Error called {ts ‘2012-06-09 15:05:05’}[/code]

Hope this help.