• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

session counter vs cfschedule

Community Beginner ,
Dec 07, 2012 Dec 07, 2012

Copy link to clipboard

Copied

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

TOPICS
Advanced techniques

Views

2.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2012 Dec 11, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 11, 2012 Dec 11, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2012 Dec 11, 2012

Copy link to clipboard

Copied

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 >

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 13, 2012 Dec 13, 2012

Copy link to clipboard

Copied

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 ..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2012 Dec 13, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 13, 2012 Dec 13, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 13, 2012 Dec 13, 2012

Copy link to clipboard

Copied

Hi guys,

cfchedule event starts a new session itself. As well as that it seems it's a session just like the one user would start loading the site. Therefore, as any other session it runs the session counter (Application.sessions) and increases it.

I have managed to successfully detect it upon start with the code provided in my initial post.

To answer to BKBK, if on session end, which was started by cfschedule event I would not run the same test to check whether the session in fact was initiated by the cfscheduler, the counter would be reduced.

Your suggestion does not help to solve the issue as counter meant to display only the sessions started by visitors, hence genuine data on the current site activity. An example to explain it in detail:

At a sample given time I have 5 users online. Then cfscheduler runs and creates extra session. I detect it and skip counter logging. At this moment I still have 5 users. Now let's assume the same users are browsing the website and sessions are extended beyond the original 30 minute timeout of inactivity. After 30 minutes cfscheduler's session will expire and run through the onSessionEnd function. If I would not detect it as such, the counter would be reduced to 4, even though 5 users are currently online. There's the problem I'm trying to solve.

The solution I have posted is still not 100% confirmed as sometimes it indeed shows some weird statistics.

Simon

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2012 Dec 13, 2012

Copy link to clipboard

Copied

I see. Then a possible solution consistent with your description is:

<cflock name="appLock" timeout="3" type="exclusive">

<cfif FindNoCase("CFSCHEDULE",arguments.sessionScope.cgitracking) EQ 0 and arguments.ApplicationScope.session GT 0>

     <cfset arguments.ApplicationScope.session = arguments.ApplicationScope.session - 1>

</cfif>

</cflock>

That should possibly cure it of the weird occurrences.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 15, 2012 Dec 15, 2012

Copy link to clipboard

Copied

Still assuming I understand it, then the following version is more efficient. It avoids repetition.

In onSessionStart:

<cfset session.isUserSession = NOT findNoCase("CFSCHEDULE",CGI.HTTP_USER_AGENT)>

<cfif session.isUserSession>

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

</cfif>

In onSessionEnd:

<cfif arguments.sessionScope.isUserSession and arguments.ApplicationScope.session GT 0>

<cflock name="appLock" timeout="3" type="exclusive">

     <cfset arguments.ApplicationScope.session = arguments.ApplicationScope.session - 1>

</cflock>

</cfif>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 09, 2013 Jan 09, 2013

Copy link to clipboard

Copied

Thanks for that! I've updated the code and was testing it over the past weeks. It still seems though, that at times it fails as current users online counter at times grows to ridiculous numbers, even though the built in visits log is showing 'slightly' more moderate numbers.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2013 Jan 09, 2013

Copy link to clipboard

Copied

I think you have to restart ColdFusion or, at least, change the application's name in Application.cfc, so as to reset the number of sessions to zero.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 09, 2013 Jan 09, 2013

Copy link to clipboard

Copied

LATEST

@BKBK Done that numerous times. Well, used application reset function, if to be precise. It's kinda strange behaviour as it stays within normal limits for a while (meaning counter does go up and down) and then goes up quite significantly. Will do some more testing and post it here should something new come up.

Simon

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation