Skip to main content
Known Participant
December 8, 2012
Question

session counter vs cfschedule

  • December 8, 2012
  • 2 replies
  • 2719 views

Hi all,

Got a bit of a riddle here. Trying to count current active number of sessions and as per ColdFusion documentation that should be done as follows:

onSessionStart:

<cfset Application.sessions = Application.sessions + 1>

onSessionEnd:

<cfset Arguments.ApplicationScope.sessions = Arguments.ApplicationScope.sessions - 1>

That all works as expected as long as one doesn't use cfschedule events. Now in my application cfschedule runs every minute as a result creating  new sessions as it goes. What I'm trying to do is detect cfschedule event as a session 'runner' and ignore it for my session totals.

On session start, that is done as follows:

<cfif FindNoCase("CFSCHEDULE",CGI.HTTP_USER_AGENT) EQ 0>

     <cfset Application.sessions = Application.sessions + 1>

</cfif>
This way cfschedule is not counted as a site user, hence not logged.

Now my question to the community is:
How do you detect cfschedule event on session end? I was unable to locate any documentation explaining what sort of information is being passed inside the sessionScope to onSessionEnd function, hence how to access CGI info (if it's possible at all).

Absolutely any input on this would be highly appreciated!

Regards,

Simon

This topic has been closed for replies.

2 replies

Known Participant
December 13, 2012

FWIW: Just wondering: WHAT kind of sessions to you start by the scheduler? Unattended ones? If so, they for sure will come to an end without timing out the session etc. If this is the case the end is foreseeable I think. This finalization code could then just decrease the session counter ..

BKBK
Community Expert
Community Expert
December 13, 2012

Unattended sessions coming to an end without timing out the session? What are you on about?

Known Participant
December 13, 2012

i assume there is (unattended) work to do in a session created by the scheduler. this work will come to an end somehow. then, the session could be deliberately ended or at least counted as terminated.

BKBK
Community Expert
Community Expert
December 11, 2012

Since your code excludes cfschedule in onSessionStart, you need not make any allowance for it in onSessionEnd. The code,

<cfset Arguments.ApplicationScope.sessions = Arguments.ApplicationScope.sessions - 1>

should therefore be sufficient in onSessionEnd.

Simon.DauAuthor
Known Participant
December 12, 2012

Thanks BKBK, however the problem with your suggestion is that if onSessionStart number of sessions is 0 and cfschedule is skipped, onSessionEnd event if the same test to detect the cfscheduled event is not performed the total of session will go to -1.

I actually solved the problem (or at least I think I did) earlier today. In the onSessionStart function save CGI.HTTP_USER_AGENT bit as a session variable, hence:

session.cgitracking = CGI.HTTP_USER_AGENT;

This variable will then become accessible inside onSessionEnd function  via arguments.sessionScope.

At this point I am just performing the same check as in the onSessionStart function and skip session totals amendment should cfschedule gets detected.

<cfif FindNoCase("CFSCHEDULE",arguments.sessionScope.cgitracking) EQ 0>

     <cfset Application.sessions = Application.sessions - 1>

</cfif>

Hope this helps to someone facing same/similar issue(s) in the future.

Regards,

Simon

BKBK
Community Expert
Community Expert
December 12, 2012

What you have just done may work, but it is unnecessary. Think about it. There is no need to remove something you have already excluded.

However, what you say made me think of a slight improvement to the code. This improves the overall design, whether or not cfschedule is taken into consideration.

<cfif  Arguments.ApplicationScope.sessions GT 0>

<cfset Arguments.ApplicationScope.sessions = Arguments.ApplicationScope.sessions - 1>

</cfif >