Dynamic Scheduled Tasks With ColdFusion

A while back, I implemented a quick solution for automatically moving files from a remote ftp site to our local prepress network using scheduled tasks in ColdFusion.

Coldfusion makes scheduled tasks very easy, via the cfschedule tag, so I set to make it happen.

Dynamic Coldfusion scheduled task

The files at the remote ftp site were being generated dynamically (custom designs) and saved in pdf format. This is part of a service we offer our customers, and it is integrated in a checkout system.

Previously, our prepress department would have to retrieve the necessary files manually, and that after having to identify which file exactly matched the order, that it was the correct format, etc.

This is how we took care of that:



<!--- generate a unique task name --->
<cfset variables.taskName = "myTask_" & #DateFormat(Now(),"mm_dd_yyyy")# & "_" & #TimeFormat(Now(), "hh_mm_ss")#>

<!--- file that would be executed by the scheduled task --->
<!--- this location has been edited, NEEDS to be web server accessible to be executed --->
<cfset variables.taskFile = "c:\scheduled\"& #variables.taskName# & ".cfm">

<!--- the things that the scheduled task has to do --->
<cfset variables.taskContents = '

<!--- TASK FILE: #taskFile# --->
<!--- TASK NAME: #variables.taskName# --->
<cfftp connection="ftpConn" server="ftp.yoursitehere.com" username="xxxx" passive="yes" password="xxxx" action="open">


<cfftp action="getfile" connection="ftpConn" remotefile="somefile.txt" localfile="somefile.txt" failifexists="no" timeout="600">

<cfftp action="close" connection="ftpConn">

<!--- delete task --->
<cfschedule action = "delete" task = "#variables.taskName#">

<!--- delete the file the task executed --->
<cffile action = "delete" file = "#variables.taskFile#">
'>


<!--- write task file --->
<cffile action = "write" file = "#variables.taskFile#"    output = "#variables.taskContents#">

<!--- schedule task --->
<cfschedule action = "update"
            task = "#variables.taskName#"
            operation = "HTTPRequest"
            url = "http://www.yoursitehere.com/scheduled/#taskname#.cfm"
            startDate = "#DateFormat(Now(), 'mm/dd/yyyy')#"
            StartTime = "#TimeFormat(Now(), 'hh:mm:ss tt')#"
            interval = "once" requesttimeout="600"
            >

And here is what that created...

Generated Coldfusion scheduled task file


<!--- TASK FILE: myScheduledTask.cfm --->
<!--- TASK NAME: myScheduledTask_11_25_2008_04_00_02 --->
<cfftp connection="ftpConn" server="ftp.yoursitehere.com" username="xxxx" passive="yes" password="xxxx" action="open">

<cfftp action="getfile" connection="ftpConn" remotefile="somefile.txt" localfile="somefile.txt" failifexists="no" timeout="600">

<cfftp action="close" connection="ftpConn">

<!--- delete task --->
<cfschedule action = "delete" task = "myScheduledTask_11_25_2008_04_00_02">

<!--- delete the file the task executed --->
<cffile action = "delete" file = "c:\scheduled\myScheduledTask_11_25_2008_04_00_02.cfm">

The first snippet would create a dynamic file that contained the scheduled task's sequence of things to do, and also the code to tell Coldfusion to dynamically add the scheduled task to the scheduler.

For the sake of this example, the scheduled task is set to activate immediately, but you can make it at some later time.

Once the scheduled task executes and reaches the end, the task deletes itself. No traces left. Nothing to cleanup. No confusion.

Related Posts

Mike Schierberl - cfschedule delete - workaround

Ben Nadel - Programmatically Deleting Scheduled Tasks In ColdFusion
Ben Nadel - Deleting ColdFusion Scheduled Tasks With CFThread And CFSchedule

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.001. Contact Blog Owner