Multiple templates running concurrently
I have scheduled a coldfusion template to run every 10 minutes how do i prevent it from running w开发者_如何学Pythonhen the previous run exceeds 10 minutes.
I've tried using a counter variable in the application scope unfortunately when the template times out or errors out the counter is not decremented.
PS. Is there a coldfuison framework for integrating applications (backend stuff)
Use an exclusive named cflock:
<cflock
timeout = "#createTimeSpan(0,0,0,1)#"
name = "myProcess"
throwOnTimeout = "no"
type = "exclusive">
<cfset start = now()>
<!--- CFML to be synchronized. --->
<cfset interval = 10 * 60> <!--- 10 minutes in seconds --->
<cfif dateDiff("s", start, now()) GT interval>
<cfhttp url="yourtemplate.cfm">
</cfif>
</cflock>
This ensures only thread ever runs the block of code in question. Simultaneous access will fail in one second without error.
To ensure the next run gets kicked off if the prior exceeds the time interval, track the start time inside the lock and then at the end, if its exceeded the interval, have the last statement inside the cflock be a cfhttp call to itself.
One possible route you could explore:
You could set up a database table to track the progress of the task. Maybe table name "task", with columns "taskName" and "inProgress", with the latter being a boolean. When the task starts, set inProgress to true. When it finishes, set inProgress to false.
In the template called by the scheduled task, have it first check the "inProgress" status of the specified task. If it's true, just abort. Otherwise, proceed.
EDIT:
Hmm... that actually wouldn't work any better than your application variables in the case of timeouts or errors. So now thinking that instead of a boolean, you use a timestamp. When the scheduled task fires, update the value with the current time. When it finishes, clear it out.
So when the task starts again, it will see that the previous task either finished (a null value), or it's still in progress. -If- it's still in progress, you can do a dateDiff() on the value to see if it was more than 'x' minutes ago. If it was, you can assume the previous task timed out (or errored out... but in that case I'd think you could put some error handling into the task itself) and run the current instance of the task.
精彩评论