Copy link to clipboard
Copied
We're running some tests and were wondering if code inside a CFTHREAD block actually runs the calling page's Application.cfc file? Our initial throught is: no. We've run a couple tests to indicate the answer is: no. But, I thought I'd post the question here since we found no documentation online on the matter.
Our test involved putting a 'hit counter' in an Application file's onRequest() method to log page hits to a DB. We then ran a CFTHREAD block on the page. The hit count showed just one page hit which, to us, would indicate only the page-level thread hit the application file and not the CFTHREAD invoked.
Is this a sound experiment?
Thanks, all
Copy link to clipboard
Copied
It's a reasonable one, yes. Perhaps another one to try is instead to add a cflog (or writelog in cfscript) to track reaching a given point or not, putting that in both.
And I'd expect the cfthread call would NOT run the application.cfc/cfm, but just the call to the page DOING the cfthread call. But it will be interesting to hear what you find.
Indeed, when you may report results, tell us also what cf version you're running (for others who find this discussion in the future, wondering the same question you asked).
Finally, I'd recommend you add a log line not only in your application.cfc's onrequest method, but also in its pseudo constructor as well, meaning outside of ANY methods/functions, such as the very top.
Copy link to clipboard
Copied
It is difficult to say without seeing the code. Could you share a sketch of the code or pseudocode?
Copy link to clipboard
Copied
@sdsinc_pmascari , On second reading, I see something that might be important to the discussion. Namely, your statement, " onRequest() method to log page hits to a DB". I think that page hits to the database should preferably be logged in onRequestStart instead.
OnRequestStart runs at the beginning of the request, before onRequest, and is therefore the more suitable event-handler for gathering request statistics such as page hits.. Whereas, onRequest is more suitable for use-cases that involve pre-processing or filtering the request, or post-.processing the request afterwards.
The difference is subtle, but there is a difference all the same.
Copy link to clipboard
Copied
@sdsinc_pmascari , the following demo. consists of 3 files. Place them within the same directory.
To run the demo, just launch testpage1.cfm in the browser.
Result:
Application.cfc
<cfcomponent>
<cfscript>
this.name = "MyTestApp3";
this.applicationTimeout = "#createTimespan(1,0,0,0)#";
this.loginStorage = "session";
this.sessionManagement = "true";
this.sessionTimeout = "#createTimeSpan(0,0,20,0)#";
this.setClientCookies = "true";
this.setDomainCookies = "false";
this.scriptProtect = "all";
this.enableNullSupport = false;
</cfscript>
<cffunction name="onApplicationStart" returntype="boolean">
<!--- Initialize number of page hits at the beginning of the application --->
<cfset application.numberOfPageHits=0>
<cfreturn true>
</cffunction>
<cffunction name="onRequestStart" returntype="boolean">
<cfargument name = "targetPage" type="String" required="true">
<!--- Count the number of page hits --->
<cflock scope="Application" timeout="5" type="Exclusive">
<cfset application.numberOfPageHits=application.numberOfPageHits+1>
</cflock>
<cflog text="#arguments.targetPage#" file="PagesHits">
<cfreturn true>
</cffunction>
</cfcomponent>
testpage1.cfm
<cfthread action="run" name="myThread">
<!---
Test, in turn, with cfhttp then with cfinclude.
Each time, change this.name in Application.cfc to a new value,
for example, "MyTestApp1", "MyTestApp2", "MyTestApp3", etc.
to ensure that a new application starts each time.
--->
<cfhttp url="http://localhost:8500/workspace/CF_Project/forum9/testpage2.cfm" method="get" result="variables.result">
<!---<cfinclude template="testpage2.cfm">--->
</cfthread>
<!--- Join to ensure the thread terminates --->
<cfthread action="join" name="myThread" />
<cfdump var="#variables.result#">
<!--- Display the current number of DB visits --->
<cfoutput><p>application.numberOfPageHits: #application.numberOfPageHits#</p></cfoutput>
testpage2.cfm
<cfset variables.result="We're in page testpage2.cfm. The time is #now()#.">
<cfoutput><p>#variables.result#</p></cfoutput>
As I said, it all depends on the code. So, can you please share the code you intend to use?