What is Scheduled Tasks?
Those tasks planned earlier to be performed at a particular time in future is known as Scheduled Tasks.
Examples:
1 - Birthday notification in any social networking sites.
For the above process to take place, there is some task need to run on everyday at 12:00 AM to find out the persons having birthday on that day and notify to the connected users regarding the birthday notification.
2 - Monthly bank statement reports via SMS/Email.
Here, there must be some task which will run on a particular day in a month and gather all the transaction details and create a report and send it to the user via SMS/Email.
How Scheduled Tasks work?
This is done by "Scheduler" that provides the ability to schedule the launch of programs or scripts at predefined times or after specified time intervals.
In ColdFusion we have an inbuilt Scheduler which is available with the installation of ColdFusion package.ColdFusion scheduled task enable us to schedule any ColdFusion template to run at any time.
How to create Scheduled Tasks?
ColdFusion scheduled tasks can be configured either via the ColdFusion Administrator or programmatically (using CFSCHEDULE tag).
Via ColdFusion Administrator
Steps:
1) Once we login to CF Admin page, under server setting, we have scheduled task menu.
If we have some existing scheduler present it will show under scheduled task menu for Starting/Pausing/Editing/Deleting individual scheduled tasks.
2) For creating a new scheduled task, click on "Schedule New Task" button.
3) Provide Task name,Duration,Frequency and URL and click "Submit" for the scheduled task to be created in ColdFusion.
Task name refers to the name for the scheduled task.
Duration refers to the starting and ending date(optional) in which the task will run.(If we don't specify an end date ColdFusion calculate the remaining count of the task basing on the frequency provided)
Frequency refers to the no of times the task will run.(In 5 different category we can set the frequency).
- One time task will run at the specified time for a single time and then will be Expired.
- Recurring task provide us to configure the scheduled task(daily,weekly,monthly) at a specific time till the End date.
- Daily every task provide us to configure the scheduled task per(hour,minute,second) basis with Start,End time and no of times on each day.
- Crontime takes an Corn-Expression* for the configuration of the scheduled task.
- Chained task provide us to configure a child task that is chained to another parent task.(It executes after the parent task)
Note:If we don't provide an end date the scheduler will run it for infinite period of time for Recurring and Daily every task schedules.
URL is the path of the file that is used to perform the task.(Sending notificaion/Email/SMS).
Submit completes the configuration of the scheduled task successfully.Now the scheduled task will run at the scheduled time.
Programmatically:
Steps:
- Create a ColdFusion template.
- Use the CFSCHEDULE tag using the UPDATE action.
- Save the template inside the ColdFusion application.
- Run the template by opening it in any browser.
- Running the template will either update the ColdFusion scheduled task, or create it if it doesn't already exist.
We can now find this scheduled task listed in the ColdFusion Administrator under scheduled tasks.
Code:
<!--- For creating or editing a scheduled task --->
<cfschedule
action="update"
task="Notification task"
URL="http://dev.testsite.com/scheduledtasks/notifyScheduler.cfm"
interval="daily"
operation="HTTPRequest"
startDate="#CreateDate(2014,10,21)#"
startTime="#CreateTime(0,0,0)#">
We can also use the below code to pause,resume,delete and run the scheduled tasks.
Code:
<!--- For pausing a scheduled task --->
<cfschedule
action="pause"
task="Notification task">
<!--- For resuming a scheduled task --->
<cfschedule
action="resume"
task="Notification task">
<!--- For deleting a scheduled task --->
<cfschedule
action = "delete"
task = "Notification task">
<!--- For running a scheduled task --->
<cfschedule
action = "run"
task = "Notification task">
All the above operations have same effect on the tasks as doing via ColdFusion Administrator.
In order to perform scheduled task configuration operations programmatically we must ensure the CFSCHEDULE tag is not disabled by Administrator Sandbox security setting for the template we are using to configure it.
NOTE: The CFSCHEDULE tag (and many others) can be disabled via the ColdFusion Administrator(Sandbox security page).
NOTE: The CFSCHEDULE tag (and many others) can be disabled via the ColdFusion Administrator(Sandbox security page).
How to know the status of the Scheduled Tasks?
In order to know the status of the scheduled tasks we need to check the scheduler log file located under CF_directory.(<CF_Directory>\cfusion\logs\scheduler.log)
In the log file the required informations(Severity, ThreadID, Date, Time, Application, Message) for the scheduled task are logged once it runs.
In logged Message mostly we will find :
- "Task <TaskName> triggered." - This refers that the scheduled task started at correct time.
- "[<TaskName>] Pausing the task because of user request at <logged time>" - This refers that the scheduled task has been paused by the user.
- "Task <TaskName> misfired." - Misfire** is a fancy name for a task that didn't start when it should have.
*Cron-Expressions are strings made up of seven sub-expressions (Seconds, Minutes, Hours, Day-of-Month, Month, Day-of-Week, and Year (optional field)), that describe individual details of the schedule(http://www.quartz-scheduler.org/docs/tutorial/TutorialLesson06.html)
**If the task was scheduled to start at 10:00:00 and it is now 10:00:01 and the task has not started, it is now considered misfired.(Misfire can be caused by various reasons like configuring the start of a scheduled task in past,unavailability of worker threads,scheduler engine down etc.)
great
ReplyDeleteTY
ReplyDelete