Ask Ben: Limiting The Amount Of Time A Block Of Code Can Run (Attempt II)

Posted April 2, 2007 at 10:00 AM

Tags: ColdFusion, Ask Ben

I have this application that uses a black-boxed piece of functionality. 98% of the time, this black-boxed functionality runs very fast as expected. Sometimes though, for reasons I cannot yet debug, this function takes so long that the page times out. Is there a way to make sure that just this block of code only executes for X number of seconds?

Ok, my last attempt at answering this question was a total failure that demonstrated my lack of understanding regarding the CFLock tag. Thankfully, Christoph Schmitz has shown my the light. I have re-attempted an answer using the CFSetting / RequestTimeOut tag and attribute. I am not 100% sure it can be done this way, but testing seems to say that Yes, it can.

 Launch code in new window » Download code as text file »

  • <!--- Try to execute the following block of code. --->
  • <cftry>
  •  
  • <!---
  • By setting a request time out for this page, we are
  • forcing it to have a maximum amount of time that it
  • can run. Since the black-boxed code comes after it,
  • it will be dominated by this CFSetting.
  • --->
  • <cfsetting
  • requesttimeout="5"
  • />
  •  
  •  
  • <!---
  • This is our black-boxed piece of code. Not
  • sure how it works, but we know that 2% of the
  • time it runs way longer than it should and
  • crashes the page.
  • --->
  • <p>
  • This will only be allowed to execute for
  • a maximum of 5 seconds.
  • </p>
  •  
  •  
  • <!---
  • Catch an errors that get thrown from our
  • code exceeding the CFSetting timeout attribute.
  • --->
  • <cfcatch>
  •  
  • <!---
  • Our black-boxed piece of code has run too
  • long. Use this opprotunity to set default
  • values so that you might be able to recover
  • from this timeout.
  • --->
  •  
  • <!---
  • Since the rest of the page will take some time
  • to execute, set a new time out for this request.
  • --->
  • <cfsetting
  • requesttimeout="60"
  • />
  •  
  • <!--- Perform recovery code here. --->
  •  
  • </cfcatch>
  •  
  • </cftry>

Hopefully this is better.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Apr 3, 2007 at 8:54 AM // reply »
56 Comments

while this solution would work for a calling template, it won't work within a function. I think what the person is asking is if they can make a CFC method time out after a certain amount of time. Now personally I have no idea how to do this.


Apr 3, 2007 at 9:02 AM // reply »
6,516 Comments

Yeah, this seems to be a very difficult task (or something simple that I am just not thinking of). I saw a presentation on Blue Dragon not so long ago and they had a CFThread tag... I wonder if the CFThread tag has a timeout attribute; that seems like something that would do the trick here - wrapping the "locked down" code in a CFThread tag and forcing a small timeout.


Apr 5, 2007 at 12:26 AM // reply »
164 Comments

When I use "while" loops, I often do something like this:

-----------------------
<!--- Set the time before entering the loop --->
<cfset var timeout = now() />

<cfloop condition="TRUE">
...do something here...

<!--- End the loop if it has run for more than five seconds --->
<cfif dateDiff("s", timeout, now()) GTE 5>
<cfbreak />
</cfif>
</cfloop>
-----------------------

Of course, that won't prevent the processing during an individual iteration of the loop from running more than five seconds.


Feb 23, 2009 at 11:54 AM // reply »
3 Comments

Just came across this issue ... as there is no timeout property for CFFEED, it's not possible to abort a request e.g. after 5 seconds ... http://cfstuff.blogspot.com/2009/02/cffeed-not-usable-due-to-missing.html


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »