• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

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
0
Gurpreet Singh Randhawa
Asked:
Gurpreet Singh Randhawa
  • 4
  • 2
1 Solution
 
cfEngineersCommented:
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
0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
Can you show me some sample code that might help
0
 
cfEngineersCommented:
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'
	where 
		task_status NOT IN ('Done','Error')
	and task_CreateDate < DateADD(n, -#myminutes#, GetDate()) 
</cfquery>


<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
</cfquery>
<cfdump var="#qry_Tasks#">
<cfif qry_Tasks.RecordCount>
 <cfloop query="qry_Tasks">
	  <cftry>

		<!--- single task --->
	  <cfquery name="qry_UpdateMailQueue" datasource="Tasks">
    UPDATE tbl_Tasks
       SET task_status='In Progress'
	      ,task_server='#inet.getHostName()#'
	 WHERE ID='#qry_Tasks.ID#'  
   </cfquery>
		<cfhttp method="GET" url="#httpcmd#&taskid=#qry_Tasks.ID#" timeout="#val(qry_Tasks.task_Timeout*1000)#" resolveurl="No"></cfhttp>
		<cfoutput>
		#httpcmd#&taskid=#qry_Tasks.ID#<hr>
		#cfhttp.filecontent#
		</cfoutput><cfflush> 
	   <cfquery name="qry_UpdateMailQueue" datasource="Tasks">
	    UPDATE tbl_Tasks
	       SET task_status='Done'
		 WHERE ID='#qry_Tasks.ID#'  
	   </cfquery>
	   <cfcatch type="Any">
	    <cfdump var="#cfcatch#">
	    <cfquery name="qry_UpdateMailQueue" datasource="Tasks">
	     UPDATE tbl_Tasks
	        SET task_status='Error',
			    task_NumberOfTries=#Val(qry_Tasks.task_NumberOfTries+1)#
		  WHERE ID='#qry_Tasks.ID#'  
	    </cfquery>   
	   </cfcatch>
	  </cftry>
 </cfloop>
</cfif>

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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
0
 
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
0
 
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
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now