Many applications need to perform tasks on a regular (or even varying) schedule. Neptune comes with Scheduler.cfc to handle scheduled tasks.
To set up Scheduler on our site, we first need to create an entry for it in _config/components.cfm and pass it to the component that will create and use the scheduled task (ContactOMatic in this example).
<component name="Scheduler" path="com.sebtools.Scheduler"> <argument component="DataMgr" name="DataMgr" /> </component> <component name="ContactOMatic" path="contacts.model.ContactOMatic"> <argument name="Manager" component="Manager" /> <argument name="Scheduler" component="Scheduler" /> </component>
Scheduler.cfc uses DataMgr to track all scheduled tasks, including when they ran and for how long.
The most important thing to making sure Scheduler.cfc works is that the "runTasks" method is called every hour. So, we will create a "schedule.cfm" file and use CFSCHEDULE or the ColdFusion Administrator to schedule that file to be called every hour.
Here is the code for schedule.cfm:
If a ProgramManager component has Scheduler passed in during initialization then it will call the "loadScheduledTask" method. So, to add a scheduled task to our Contact-O-Matic, we could use the following code:
<cffunction name="loadScheduledTask" access="private" returntype="any" output="false" hint=""> <cfif StructKeyExists(variables,"Scheduler")> <cfinvoke component="#variables.Scheduler#" method="setTask"> <cfinvokeargument name="Name" value="ContactOMatic Types Count"> <cfinvokeargument name="ComponentPath" value="ContactOMatic"> <cfinvokeargument name="Component" value="#This#"> <cfinvokeargument name="MethodName" value="runScheduledTask"> <cfinvokeargument name="interval" value="hourly"> </cfinvoke> </cfif> </cffunction>
The name of the scheduled task must be unique across the site. The "ComponentPath" argument is a bit of a misnomer. It needs only be a unique string to identify the component. It need not be the actual path to the component. For more information, see the Scheduler documentation.
Next we create the method to be called by Scheduler:
<cffunction name="runScheduledTask" access="public" returntype="any" output="false" hint=""> <!--- Action code here ---> </cffunction>
Now we have a scheduled task that will automatically be set up and called by our component.
Next: Third Party Integration