Scheduling a New Work using cfschedule

I want to schedule a JOB programatically to check if schedule for specified action exists, use that else create a new ione and execute immediately if time is more than 9:00 am for today else trigger every weekday to trigger in the morning 9:00 am
LVL 16
Gurpreet Singh RandhawaWeb DeveloperAsked:
Who is Participating?
I had to remove alot of the things i put in this, but this snippet should work

<cfsetting requesttimeout="9999">
<cfset myminutes = 15>

<cfquery name="qry_oldTasks" datasource="Tasks">
	update tbl_tasks 
	   set task_status = 'Done'
		task_status NOT IN ('Done','Error')
	and task_CreateDate < DateADD(n, -#myminutes#, GetDate()) 

<cfquery name="qry_Tasks" datasource="Tasks">
 select top 1 * from tbl_Tasks
 where 1=1
   and task_CreateDate < GetDate() 
   and task_NumberOfTries < task_MaxNumberOfTries 
   and task_status IN ('Queued','Error')
	order by task_CreateDate asc
<cfdump var="#qry_Tasks#">
<cfif qry_Tasks.RecordCount>
 <cfloop query="qry_Tasks">

		<!--- single task --->
	  <cfquery name="qry_UpdateMailQueue" datasource="Tasks">
    UPDATE tbl_Tasks
       SET task_status='In Progress'
	 WHERE ID='#qry_Tasks.ID#'  
		<cfhttp method="GET" url="#httpcmd#&taskid=#qry_Tasks.ID#" timeout="#val(qry_Tasks.task_Timeout*1000)#" resolveurl="No"></cfhttp>
	   <cfquery name="qry_UpdateMailQueue" datasource="Tasks">
	    UPDATE tbl_Tasks
	       SET task_status='Done'
		 WHERE ID='#qry_Tasks.ID#'  
	   <cfcatch type="Any">
	    <cfdump var="#cfcatch#">
	    <cfquery name="qry_UpdateMailQueue" datasource="Tasks">
	     UPDATE tbl_Tasks
	        SET task_status='Error',
		  WHERE ID='#qry_Tasks.ID#'  

Open in new window

I had addressed issues like this in the past by creating a task table in the db with a startdate, status and httpcmd field to store the info to execute.
THen i just have one scheduled task called CFTASKS which i run every 5 minutes that checks for a task to be run .
This solution is very powerfull and can be used for reporting and history of tasks run etc...
This will also not clutter up the cf server with tons of singelton tasks
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
Can you show me some sample code that might help
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
Thanks for the Sample! I just had a talk with my senior and he said we can cannot create a New Table so we have to find solution for this!

My idea was like this

1. Run a query, Loop over the query

2. Make conditions like today's date and check for its value to today's date and more than 9:00 AM

My queries!

1. where should i place my cfschedule code in which file and how it will find when to run the file
2. i do not have access to cfdmin, how do i know it runs

3. i need to run it weekly, so i must run it on everyday so whosoever's date come today and checks that its been 7 days long, send him an email

does my question makes sense
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
i see you have not used the cfschedule anywhere rather than you have just used cfhttp, can i know why and can you explain the bits in this
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
Any Update on this,

I have anotehr uestion, I am able to use the cfschedule successfully, but how my cfschedule knows which ID to run for !

Like it ahs to run every 7 seven days, and i have the ID as dynamic which is different.

Suppose me as ID 1, you as ID 2, so for me today + 7 days it shouild run and for you your ID + 7 days it should run, how do i pas the dyamic ID to it and how do i let cfschedule knows which to refer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.