Alternative to Coldfusion CFSCHEDULE

Share on FacebookTweet about this on TwitterShare on LinkedIn

Share on FacebookTweet about this on TwitterShare on LinkedIn

Do you have a Coldfusion project that needs some functions to run automatically on a daily basis?  ColdFusion’s CFSCHEDULE is a great way to do this. But what if your program runs on a secure server and needs some sort of credentials like user id, password, and a certificate authority? It is possible to get around this by importing a valid certificate, but there is a simpler way to accomplish your task.

segue-blog-alternative-coldfusionCFSchedule

 

The Problem

I am part of a development team that works to develop and maintain an Air Force website that runs a significant Air Force program. While I can’t be any more specific than that, I will say that we had a requirement to run some functions on a daily basis without taking too many system resources. These functions happen “off stage” one time per day so users were unaware that they were running. First, we tried using the built in ColdFusion function CFSCHEDULE. We set up the function so it could run independent of the main system, i.e. without any user input. After doing some research on the Adobe site and hitting development forums on the web, we found the right arguments to use to get the function to run on our local development server. When we moved the code to our test server on the Air Force network, we encountered a problem. We could call the functions manually from code once we were signed in to the system, but our functions would not work as they should with CFSCHEDULE. With a little more research, we discovered that the problem was with getting on to the Air Force network. Because no user was signing in with proper Air Force network credentials, the network did not allow our program access to the server so it quickly because apparent that we needed another method to run our tasks.

Our Solution

Because we are on an Air Force system, there are numerous users logging in-sometimes even multiple times per day. Because of this, our senior programmer suggested a method of using our login setup. Every time a user logs in, our login setup first checks if an application variable (a date) is defined. If it is not, the system sets the variable to the day before yesterday. Then the system checks if today is at least one day later than the application variable and if the hour portion of the current time is later than seven. If yes, then the system resets the application variable to today. This causes the function to only run one time a day. It will also run if no one logged in the previous day, as on a weekend. Now all we need to do is define any needed queries and include the template that does all the heavy lifting. The beauty of this method is that the function runs invisibly to the users. Use caution if this function will call a query that takes more than a few milliseconds to run or users may notice a slow login. Here’s the code:

<!———————————- ——————————————->
<!—————–Logic to run data dump for VRS export files —————–>
<!———————————- ——————————————->
<!—Check if application.scheduled_vrs_dump is defined (date variable), if not set it —>
<cfif !IsDefined(‘application.scheduled_vrs_dump’)>
<cfset myDate = CreateDate(Year(Now()),Month(Now()),Day(Now()))>
<cfset application.scheduled_vrs_dump = DateFormat(DateAdd(‘d’,-1,myDate),’yyyy-mm-dd’)>
</cfif>
<!—Checks if vrs dump function has run today and time is after 0800 local —>
<cfif DateDiff(‘d’,application.scheduled_vrs_dump,now()) gte 1 and TimeFormat(Now(),”H”) gt 7>
<!—Reset application.scheduled_vrs_dump to today so function only runs one time per day—>
<cfset application.scheduled_vrs_dump = CreateDate(Year(Now()),Month(Now()),Day(Now()))>
<!—Instantiate component where we have the query function —>
<cfobject name=”scheduledQuery” component=”#webDir#/scheduled/qry/qry_scheduled”>
<!—Query function to get the data—>
<cfset getVRSData = scheduledQuery.cf_get_VRS_Data()>
<!—Template with code to export the data to MS Excel—>
<cfinclude template=”../../scheduled/act/act_export_vrs_data.cfm” >

This method is a simple, yet elegant solution to run automated, scheduled functions when CFSCHEDULE won’t work for you because of secure server credential requirements. It is especially useful since the function works behind the scenes without user input or visibility.

Need Help? Contact us