Scheduler.cfc 1.0

More than a year ago, I released a beta of Scheduler.cfc. Then I promptly forgot to release a full version. So, now I mean to correct that oversight.

Once it is set up, Scheduler.cfc allows any component to schedule a task for itself. This can really grant more power to many components than they would otherwise have.

The code to instantiate Scheduler (into Application scope) is as follows (assuming a datasource stored in the variable "mydsn"):

<cfset Application.DataMgr = CreateObject("component","DataMgr").init(mydsn)>
<cfset Application.Scheduler = CreateObject("component","Scheduler").init(Application.DataMgr)>

The essential step to get Scheduler to work correctly is that you must call the "runTasks" method at least once an hour. I typically do this by creating on page on my site that is called using a ColdFusion scheduled task. The code for that page is as follows:

<cfset Application.Scheduler.runTasks()>

In one component, I make sure that it has a task every hour from 6 AM to 8 PM. So, I have the following code in the init method:

<cffunction name="init" access="public" output="no">
   <cfargument name="Scheduler" type="any" required="yes">

   <cfset variables.Scheduler = arguments.Scheduler>
   <cfinvoke component="#variables.Scheduler#" method="setTask">
      <cfinvokeargument name="Name" value="Send Articles Email">
      <cfinvokeargument name="ComponentPath" value="sys. ArticleNotices">
      <cfinvokeargument name="Component" value="#this#">
      <cfinvokeargument name="MethodName" value="sendMessages">
      <cfinvokeargument name="Interval" value="hourly">
      <cfinvokeargument name="hours" value="6,7,8,9,10,11,12,13,14,15,16,17,18,19,20">
   <cfreturn this>

In this example, the path of the component is sys.ArticleNotices. It doesn't really matter if this path is accurate, only that the string is unique for every component.

This code will cause the "sendMessages" method of the component to be called every hour from 6 AM to 8 PM.

I could have also added an "Args" argument and passed in a structure of arguments that I wanted to be passed to the method when it is called.

Beyond the power that Scheduler.cfc offers to components is the advantage of tracking scheduled tasks that it offers. It stores the tasks themselves in a table named "schTasks" and it stores a record for every time that a scheduled task is called in "schActions".

Scheduler.cfc is open source and free for any use. Feel free to download it and try it out. Scheduler requires DataMgr (also free and open source) so be sure to download that as well.

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.