Deleting ColdFusion Scheduled Tasks With CFThread And CFSchedule

Posted December 29, 2008 at 12:09 PM by Ben Nadel

Tags: ColdFusion

If you can remember this far back, a little over two years ago I stumbled upon the odd fact that in ColdFusion a scheduled task cannot delete itself. As a very hacky work around to this, I came up with a way to use ColdFusion's CFHTTP tag to have the scheduled task launch an asynchronous request that was able to delete the original scheduled task. This worked, but is clearly not ideal. Now, with ColdFusion 8's CFThread tag, we have a much cleaner way to launch asynchronous tasks. As such, I figured I would take a few minutes to see if it could be used to delete scheduled tasks.

I created a scheduled task named, "CFThreadDelete," that calls this template ever 61 seconds (ColdFusion scheduled tasks cannot fire more often than 61 seconds):

  • <!--- Get the file path of our log file. --->
  • <cfset strLogFile = ExpandPath( "./log.txt" ) />
  •  
  • <!---
  • Log task output. We are doing this so we can see when the
  • task fires and whether or not the CFSchedule tag successfully
  • deleted the task.
  • --->
  • <cffile
  • action="append"
  • file="#strLogFile#"
  • output="Triggered: #DateFormat( Now(), 'HH:mm:ss' )#"
  • addnewline="true"
  • />
  •  
  •  
  • <!--- Check to see if we should delete this task. --->
  • <cfif (RandRange( 1, 3 ) EQ 2)>
  •  
  • <!---
  • Delete the scheduled task. Since a task cannot delete
  • itself from the same thread, we need to launch a seprate
  • thread to actually execute the CFSchedule tag.
  • --->
  • <cfthread
  • action="run"
  • name="DeleteTask"
  • logfile="#strLogFile#">
  •  
  • <!--- Log the deletion attempt. --->
  • <cffile
  • action="append"
  • file="#ATTRIBUTES.LogFile#"
  • output="Deleted: #DateFormat( Now(), 'HH:mm:ss' )#"
  • addnewline="true"
  • />
  •  
  • <!--- Delete the task. --->
  • <cfschedule
  • action="delete"
  • task="CFThreadDelete"
  • />
  •  
  • </cfthread>
  •  
  • </cfif>
  •  
  •  
  • <p>
  • Task Done Executing
  • </p>

As you can see, each time the template runs, I am logging the request to a text file. Then, I randomly generate a number to see if we should delete the current task. If, by chance, we need to delete the task, I am launching a parallel thread using ColdFusion 8's CFThread tag. This asynchronous process then logs the delete request and uses CFSchedule to delete the named task.

After letting this run for about 10 minutes, I checked the log file:

Triggered: 11:12:48
Deleted: 11:12:49

Looks like the task ran once and, by chance, deleted itself on the first run. Now, since I left it to run for 10 minutes, I am quite confident that the task is not still running. The ColdFusion Administrator also reports the task as no longer existing.

Looks like ColdFusion 8's CFThread tag can be used to quite elegantly delete scheduled tasks that want to auto-distruct.




Reader Comments

Dec 29, 2008 at 2:22 PM // reply »
2 Comments

I got intrigued by this post, as it struck me a little too complex for something that should be fairly simple.

I have never had any issues just inserting the code to delete the scheduled task inside the actual task, so it deletes itself when it is done.

Here's some older info I reposted from my notes. http://blog.shortfusion.com/index.cfm/2008/12/29/Dynamic-Scheduled-Tasks-With-ColdFusion#more


Dec 29, 2008 at 2:31 PM // reply »
25 Comments

Hi Ben,

I always like seeing new ways of using cfthread, but I have to second Alex on this. This may be new to CF8, but I've also managed to have a scheduled task delete itself (it's actually the first thing I do in the task).


Dec 29, 2008 at 2:32 PM // reply »
11,247 Comments

@Alex,

Interesting post. The way you have it set up, it doesn't matter. Without a parallel thread, when you scheduled task deletes itself, it does successfully remove itself form the ColdFusion Admin; however, the task will continue to execute on its existing scheduled basis until the ColdFusion service is restarted.

However, since your task interval was "ONCE", even if the scheduled task hangs out in memory (as it should with the "bug"), it will never fire again. If you changed your task to something reoccurring, you would probably experience some issues.


Dec 29, 2008 at 2:32 PM // reply »
11,247 Comments

@Francois,

See my previous comment. I wonder what repeating occurrence your tasks have.


Dec 29, 2008 at 2:35 PM // reply »
25 Comments

@Ben,

Ah, there you go. I imagine the little detail about the interval "bug" was in your previous post. My bad ;).


Dec 29, 2008 at 2:44 PM // reply »
11,247 Comments

@Francois,

No worries my man. And to be honest, I have only ever dealt with tasks that happen on a regular basis. It's certainly possible that there is no bug for one-off tasks. Perhaps the mechanism for how the task interval is defined is where the bug is.


Jim
Dec 29, 2008 at 6:46 PM // reply »
16 Comments

Hi Ben,

I'm wondering if you have had any experience using alternative scheduling software?

I recently took a look at Quartz (http://www.opensymphony.com/quartz/), and I may end up using in one of projects. I really like the fact that it solves all the inherent weakness of scheduled tasks, e.g. proper queuing, flexible start times (no 61 second wait).

Integration may be a bit of a challenge, so I'm wondering if you have any different recommendations?

Thanks,

Jim


Dec 29, 2008 at 9:17 PM // reply »
11,247 Comments

@Jim,

I have not tried anything else.


Sep 9, 2009 at 7:18 AM // reply »
8 Comments

I am having an issue now that even if I delete a scheduled task in the admin. It keeps firing off and the old scheduled interval. Even restarting CF doesn't help. As a temporary measue I have just deleted the template it is trying to run but every two minutes I get an entry in application log that it cannot find the file.


Sep 12, 2009 at 10:52 PM // reply »
11,247 Comments

@Don,

Sounds like the config file that stores the scheduled tasks is corrupted or something?? Very strange. If restarted the ColdFusion service doesn't get rid of it AND it's no longer in the ColdFusion admin, then something very odd is going on.


Aug 17, 2011 at 12:03 PM // reply »
7 Comments

Hi,
I have a tera data database user session that expires after every 30 mins of idl time. I am using JDBC type 4 to create the datasource. Everything works fine :).... But the session expires every 30 mins of idle time. I decided to write a schediler that will run a select query every two mins so that there is no idle time for session to expire.... here is my schedular:::: (schdule.cfc)

<cfschedule action="update"
url="http://urlname/jdbctest.cfm"
interval="120"
username="username"
password="pwd"
task="AutomatedMessage"
startdate="8/8/11"
starttime="8:20 AM"
operation="httprequest"
>

now this page is refereing to a cfm page in url (jdbctest.cfm)

<cfquery datasource="#teradat#" name="abc">
select
*
from
visa_shipment
where
shp_trk_nbr=453056324459
</cfquery>

<cfoutput>#abc.shp_trk_nbr#</cfoutput>

The schedular runs fine but still the session is expiring, is there any way I could test it.

Thanks,
Purnima


Mar 1, 2012 at 9:23 AM // reply »
4 Comments

How could you use a template similar to the one above, but not delete the scheduled tasks, and change a single parameter in the above template every time it was executed?


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools