« Unbelievable Stroke of Luck | Main | Upgrading Movable Type from 3.2 to 3.3 »

Coldfusion CFSchedule fortnightly recurrence workaround

coldfusionmx.jpg
I am working on a project that requires reminder emails every 2 weeks for some users. For some reason, I just assumed that this would be a no-brainer with the Scheduled Task screen in the Coldfusion Administrator screen. The only options for recurrence are dail, weekly or monthly. I tried faking out the daily section with more than 24 hours, but no good. How can I easily get this task to run every 2 weeks without resorting to a persistent flag like in a database, filesystem or (less reliable) long expiring application scope? We are using MX 6, so perhaps this is resolved in version 7?

At first I thought, "OK, 52 weeks in a year", but there are 52.14 weeks in a regular year (52.28 in leap year). So it would be possible for a consecutive execution or skipping of the task. Granted, it would probably go unnoticed during the holidays, but it's the principle. Then I thought about looping over the days and counting off the weeks--not scalable or elegant. So here is the low tech solution I came up with. Yeah, sure, there has to be a simpler, and slicker way, but this works. I even made it so you could choose to run your task every 2, 3, 4, 5, etc.... weeks. Simply plug the following into your task, adjust the frequency and mod adjustment to fit desired starting point, then set the task to run weekly and you are done.

<!--- multi-weekly CFSCHEDULE workaround, since only recurring choices are daily, weekly and monthly. January 1, 2006 is convenient in that it is 1st day of week. We set start date to equal day of current week (1 -7) to get a nice round number when we divide by 7 days (a week) and add 1 to get how many of today's weekday have happened since Jan 1, 2006 (e.g. this is the 15th Wednesday since 01-Jan-2006). Set this task to run weekly in Scheduled Tasks. --->
<cfset variables.today = now()><!--- for testing override with any date
after Jan 1, 2006 --->
<cfset variables.frequency = 2><!--- every this many weeks to run --->
<cfset variables.modadjustment = 1><!--- adjust this from 0 up to frequency to fit your starting date --->
<cfif incrementValue(dateDiff("d",'JAN #datePart("w",variables.today)#, 2006',variables.today) / 7) mod variables.frequency is variables.modadjustment>
     Task to Run
</cfif>

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)