Scheduling a New Work using cfschedule

Posted on 2011-04-27
Last Modified: 2012-05-11
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
Question by:myselfrandhawa
    LVL 6

    Expert Comment

    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
    LVL 15

    Author Comment

    Can you show me some sample code that might help
    LVL 6

    Accepted Solution

    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

    LVL 15

    Author Comment

    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
    LVL 15

    Author Comment

    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
    LVL 15

    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
    Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now