Expired SESSION Available In ColdFusion CFThread Tag

Posted December 12, 2007 at 9:01 AM

Tags: ColdFusion

Not too much to see here; I just took my previous session expiration post and applied it to ColdFusion's asynchronous threading. Basically, I wanted to make sure that a SESSION which has ended mid-page request would still be available in subsequent threads launched by the current page request (after the session has expired). But, before I even show the code, I just want to point out that it is probably not good practice to refer to the SESSION scope within the CFThread body unless explicitly passed via the CFThread tag attributes (think proper encapsulation and low coupling).

First, I had to update my Application.cfc. Since CFThreads cannot output data to the screen, I added a logging function that would write a value and time stamp to a local log file. Then, I also included the OnRequest() event method so that the log function would be available in the THIS scope (the whole page request becomes an Application.cfc mixin).

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

  • <cfcomponent
  • output="false"
  • hint="Handle the application configuration.">
  •  
  •  
  • <!---
  • Set the application settings. Notice that we
  • are creating sessions that only last for 2 seconds.
  • --->
  • <cfset THIS.Name = "TimeoutTest" />
  • <cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 0, 1, 0 ) />
  • <cfset THIS.SessionManagement = true />
  • <cfset THIS.SessionTimeout = CreateTimeSpan( 0, 0, 0, 2 ) />
  •  
  •  
  • <!--- Set the page request settings --->
  • <cfsetting
  • showdebugoutput="false"
  • />
  •  
  •  
  • <cffunction
  • name="OnSessionStart"
  • access="public"
  • returntype="boolean"
  • output="false"
  • hint="Fires when the user's session begins.">
  •  
  • <!--- Store the date created. --->
  • <cfset SESSION.DateCreated = Now() />
  •  
  • <!--- Return out. --->
  • <cfreturn true />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="OnRequest"
  • access="public"
  • returntype="void"
  • output="true"
  • hint="Executes the requested template.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Template"
  • type="string"
  • required="true"
  • hint="The ColdFusion template that has been requested."
  • />
  •  
  • <!--- Include the template. --->
  • <cfinclude template="#ARGUMENTS.Template#" />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="OnSessionEnd"
  • access="public"
  • returntype="void"
  • output="false"
  • hint="Fires when the session is terminated.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Session"
  • type="struct"
  • required="true"
  • hint="The expired session scope."
  • />
  •  
  • <!--- Store the date destroyed. --->
  • <cfset ARGUMENTS.Session.DateEnded = Now() />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="LogValue"
  • access="public"
  • returntype="void"
  • output="false"
  • hint="Logs the given value to a local log.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Value"
  • type="string"
  • required="true"
  • hint="The value to be logged."
  • />
  •  
  • <!--- Set up the output. --->
  • <cfset var Output = (
  • ARGUMENTS.Value & " : " &
  • TimeFormat( Now(), "hh:mm:ss" )
  • ) />
  •  
  • <!--- Log value / time stamp to txt file. --->
  • <cffile
  • action="append"
  • file="#ExpandPath( './log.txt' )#"
  • output="#Output#"
  • addnewline="true"
  • />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  • </cfcomponent>

Then, I created a simple Index.cfm page that sleeps for 10 seconds (so as to ensure session expiration) and then launches an asynchronous thread:

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

  • <!---
  • Sleep the primary page request thread to make sure
  • that the SESSION has expired by the time we launch our
  • asynchronous thread.
  • --->
  • <cfthread
  • action="sleep"
  • duration="#(10 * 1000)#"
  • />
  •  
  •  
  • <!---
  • Launch asynchronous thread. At this point, the user's
  • SESSION will have already ended.
  • --->
  • <cfthread
  • action="run"
  • name="thread1">
  •  
  • <!---
  • Check to see if the session has timed out (this should
  • always be the case, but just a precaution).
  • --->
  • <cfif StructKeyExists( SESSION, "DateEnded" )>
  •  
  • <!--- Log session timeout time. --->
  • <cfset THIS.LogValue(
  • "Session Ended " &
  • "(#TimeFormat( SESSION.DateEnded, 'hh:mm:ss' )#)"
  • ) />
  •  
  • </cfif>
  •  
  • </cfthread>

There's a couple of things to notice here. For starters, I am sleeping the primary thread (originally requested page) for 10 seconds. This will cause the user's session to expire before our CFThread tag is executed (confirmed via the logging output). Additionally, I am referring to the THIS scope within the CFThread tag body. Because we are using the OnRequest() event method, the entire page essentially becomes part of the Application.cfc and therefore has access to all of its scopes.

Running this page, we get the following log data:

Session Ended (08:48:30) : 08:48:35

As you can see, the SESSION expired at 30 seconds and the thread fired at 35 seconds, 5 seconds after the OnSessionEnd() method was called. Sweet. This just means that as with the primary page request, we never have to worry about session expiration within an asynchronous thread.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page





Reader Comments

Dec 12, 2007 at 9:13 AM // reply »
204 Comments

"I added a logging function that would write a value and time stamp to a local log file"

Err... why not cflog? The only reason I can see to do this would be for cases when you don't have access to normal CF logs.


Dec 12, 2007 at 9:49 AM // reply »
6,371 Comments

@Ray,

To be honest, I find it a real pain to go to the CFLogs. Our logs are like a million miles long and take forever to load (I don't manage that stuff and I don't know much about them). As such, I find it easier to make something quick and small for a local file.


Dec 12, 2007 at 9:51 AM // reply »
204 Comments

The CF logs can get big - you can use cflog to log to a custom log, like one based on the application name.


Dec 12, 2007 at 9:56 AM // reply »
6,371 Comments

@Ray,

Where is that log file located? If I can give it a custom path, then I am all for it. But, if I still need to actually go to the ColdFusion Admin to view it, still feels a little overkill... no, overkill is not the right word. I guess I mean that it requires to much "system context".


Dec 12, 2007 at 10:01 AM // reply »
204 Comments

Yeah, it is still the main CF log folder. I guess I don't mind the Log Viewer so much. Plus, Scott Stroz's Flex version makes it much sexier.


Dec 12, 2007 at 10:04 AM // reply »
6,371 Comments

@Ray,

Ha ha, I was about to say something like, Wouldn't it be cool if they made a CFLog tag where you could define the output and path the file.... then I was like, oh wait, that's what CFFile does :)

I guess the convenient thing about CFLog is that you don't have to worry about file paths.


Dec 12, 2007 at 11:09 PM // reply »
92 Comments

If you need to see a data dump logged I suggest using the format and output attributes of cfdump! Logs are great but usually whenever I debug something I prefer cfdumps since I can see any sort of complex data. I know this isn't really about debugging but since cflog was mentioned I just thought I'd bring up some of the new features cfdump has gotten in CF8.


Dec 13, 2007 at 7:17 AM // reply »
6,371 Comments

@Javier,

You raise a great point. I am a huge fan of CFDump, but have not really leveraged it yet in ColdFusion 8. Since so much of my CF8 stuff is theoretical (not on production yet), these types of things are not second nature yet. CFDump in CF8 has some great stuff including writing directly to a text file.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »