Using service components, one of the challenges that I have run into (and I think others have as well) is that I need to schedule a task from a component, but it doesn't make sense to do so.
So, I have a Scheduler.cfc to handle scheduling a task from a component. It should be loaded into a persistent scope (Application scope preferably) and does require that the runTasks() method should be run from a ColdFusion template that is called every hour (probably by scheduled task).
I typically pass Scheduler into a component that needs to schedule a task. From there I can call the setTask() method of Scheduler.cfc with the following arguments:
- Name: A unique name for the scheduled task.
- ComponentPath: A path to the calling component (just used as a unique indicator for the component).
- Component: The calling component, on which Scheduler will call a method (usually This).
- MethodName: The name of the method in your component that Scheduler should call when it fires the task.
- Interval: The interval Scheduler should use for calling the method with the following options.
- once: Scheduler will call the task immediately, but only one time.
- hourly: Scheduler will call the task every hour.
- daily: Scheduler will call the task every 24 hours.
- weekly: Scheduler will call the task once every 7days.
- monthy: Scheduler will call the task once a month.
- A number of seconds between each call.
- Args: A structure of arguments that Scheduler should pass to the method that it is calling for this task.
- Hours: A comma-delimited list of the hours during which the task can be called (on a 24 hours clock). So, if the value is "2,3" then Scheduler will only call the task during the hours of 2:00AM-2:59AM or 3:00AM-3:59AM.
Scheduler.cfc is open source and free for any use. Feel free to download it and try it out.