I've not used cfthread before but i'm using the following code and it's not working.
<cfloop from="1" to="5" index="local.pageNo">
<cfthread name="thr#local.pageNo#" threadIndex="#local.pageNo#" action="run">
<cfif local.pageNo GT 1>
<cfhttp url="#local.apiURL#&page=#local.pageNo#" method="get" result="local.myResults" >
</cfhttp>
<cfset local.myResponse = deserializejson(local.myResults.filecontent)>
</cfif>
<cfloop from="1" to="#arrayLen(local.myResponse.result)#" index="i">
<cfset local.apartmentList = listAppend(local.apartmentList,local.myResponse.result[i].id & '-0')>
</cfloop>
</cfthread>
</cfloop>
<cfthread action="join" name="thr1,thr2,thr3,thr4,thr5"/>
I'm expecting local.apartmentList to be a big list of ID's but it returns empty. It's almost as if the code inside the thread is just being skipped. Can anybody spot what i'm doing wrong?
When you're using <cfthread>, the code within those tags is not run within the same context as the code around it. So you need to pass any variables you intend to use into it (as attributes of the <cfthread> tag), or "share" them via the request scope.
So your <cfthread> block won't know what variables like local.pageNo are.
Any error occurring in <cfthread> processing is logged, so you need to look at your logs to see what errors are cropping up.
Related
I am downloading data from an API from one of our vendors. The data is an array but some of the fields are empty and come over as undefined. I am able to get most of the information out with a loop but when I add the field "notes" it fails with the error of:
"Element notes is undefined in a CFML structure referenced as part of an expression. The specific sequence of files included or processed is:
C:\websites\Fire\Reports\xml_parse\Crewsense_payroll_loop.cfm, line:
21 "
When I look at the dump I see that the field shows as "undefined". I've run out of ideas. Any help would be greatly appreciated. I've included the entire code and a link to the dump showing the array.
<cfhttp url="https://api.crewsense.com/v1/payroll? access_token=as;lkdfj;alskdfj;laksdfj&token_type=bearer&start=2019-01-05%2019:00:00&end=2019-01-06%2007:59:00" method="GET" resolveurl="YES" result="result">
</cfhttp>
<cfoutput>
<cfset ApiData = deserializeJSON(result.filecontent)>
<cfset API_ArrayLength = arraylen(ApiData)>
<cfloop index="i" from="1" to=#API_ArrayLength#>
#i# #ApiData[i]["name"]#
#ApiData[i]["employee_id"]#
#ApiData[i]["start"]#
#ApiData[i]["end"]#
#ApiData[i]["total_hours"]#
#ApiData[i]["work_type"]#
#ApiData[i]["work_code"]#
#ApiData[i]["user_id"]#
#ApiData[i]["notes"]# <---Fails here when added--->
<cfset i = i+1>
<br>
</cfloop>
<cfdump var="#ApiData#">
</cfoutput>
Dump
When dealing with data structures that have optional elements you will need to check for their existence before trying to access them. Otherwise you will get that error. I have added a snippet with an if condition utilizing the structKeyExists() function to your code as an example.
<cfhttp url="https://api.crewsense.com/v1/payroll? access_token=as;lkdfj;alskdfj;laksdfj&token_type=bearer&start=2019-01-05%2019:00:00&end=2019-01-06%2007:59:00" method="GET" resolveurl="YES" result="result">
</cfhttp>
<cfoutput>
<cfset ApiData = deserializeJSON(result.filecontent)>
<cfset API_ArrayLength = arraylen(ApiData)>
<cfloop index="i" from="1" to=#API_ArrayLength#>
#i# #ApiData[i]["name"]#
#ApiData[i]["employee_id"]#
#ApiData[i]["start"]#
#ApiData[i]["end"]#
#ApiData[i]["total_hours"]#
#ApiData[i]["work_type"]#
#ApiData[i]["work_code"]#
#ApiData[i]["user_id"]#
<cfif structKeyExists(ApiData[i],"notes")>
#ApiData[i]["notes"]# <!--- Show 'notes' if it exists --->
<cfelse>
'notes' is not available <!--- Do something here (or not) --->
</cfif>
<cfset i = i+1>
<br>
</cfloop>
<cfdump var="#ApiData#">
</cfoutput>
I've got the following code in a method:
<cffunction name="serviceTicketValidate" access="public" output="yes" returntype="void" hint="Validate the service ticket">
<cfargument name="service_ticket" type="string" required="yes" hint="The ST to validate" />
<!--- Contact the CAS server to validate the ticket --->
<cfhttp url="#Variables.cas_server#serviceValidate" method="get">
<cfhttpparam name="ticket" value="#Arguments.service_ticket#" type="url" />
<cfhttpparam name="service" value="#Variables.service#" type="url" />
</cfhttp>
<!--- Received a valid XML response --->
<cfif IsXML(cfhttp.FileContent)>
<cfset XMLobj = XmlParse(cfhttp.fileContent)>
<!--- Check for the cas:user tag --->
<cfset CASuser = XmlSearch(XMLobj, "cas:serviceResponse/cas:authenticationSuccess/cas:user")>
<!--- Set the username to the value --->
<cftry>
<cfif variables.username NEQ ''>
<cfdump var="#Variables.username#" /><cfreturn/>
</cfif>
<cfif ArrayLen(CASuser)>
<cfset Variables['username'] = CASuser[1].XmlText />
</cfif>
<cfcatch>
<cfdump var="#cfcatch#" /><cfabort/>
</cfcatch>
</cftry>
<!--- Search for cas:attributes --->
<cfset CASattributes = XmlSearch(XMLobj, "cas:serviceResponse/cas:authenticationSuccess/cas:attributes")>
<!--- Go through all the attributes and add them to the attributes struct --->
<cfif ArrayLen(CASattributes)>
<cfloop array=#CASattributes[1].XmlChildren# index="attribute">
<cfset StructInsert(Variables.attributes,RemoveChars(attribute.XmlName,1,Find(":",attribute.XmlName)),attribute.XmlText)/>
</cfloop>
</cfif>
</cfif>
Note I added the cftry and cfcatch to see what is going on exactly. I've also added the if username != blank to debug as well. This method is called in another method like so:
<cfinvoke method="serviceTicketValidate">
<cfinvokeargument name="service_ticket" value="#service_ticket#" />
</cfinvoke>
<cfdump var="test2" /><cfabort/>
Again I've added the dump and abort for testing. The variable.username is defied and set to an empty string when the component is initiated and the component is initiated into a session variable.
So get this... when the whole process runs the first time I get output on my screen test2 as expected. Then, the next time the same thing is run, the session exists, thus the variable.username is set to something. In the first code block I can dump variables.username and see the username. However if I try to use variables.username in a conditional expression (like in that if statement) or if I remove the if statement and let the script try to change the value of variable.username, there are no errors, it just breaks out of the script completely. It ends that method, and the method that called it and I don't see test2 like I would think. It all just ends for some reason.
If you need further details I can provide more code but I tried to trim out as much as I thought was relevant. All methods are in the same component, all methods are public. Why can't I change the value of variables.username and why is there no error?
EDIT:
I think it may have something to do with the cflock but I'm debugging some stuff right now. I had a redirect inside the code block that is inside the lock. So I guess it never unlocks. But I even waited after the timeout and it still remained locked. I thought the lock was supposed to expire after the timeout.
I'm a little confused but it seems like you're trying to use a cfc's variables scope to set caller variables. The variables scope is not available to the caller the way it seems you are trying to use it.
index.cfm
<cfoutput>
<cfset objTest = createObject("component", "testscope").init()><br><Br>
<cfset objTest.checkValue()><br><br>
Calling page is checking the existence of testvar: #isDefined("variables.testvar")#
</cfoutput>
testscope.cfm
<cfcomponent displayname="testscope">
<cffunction name="init" access="public">
init() just set variables.testvar.
<cfset variables.testvar = "Okay, set">
<cfreturn This>
</cffunction>
<!--- Set some more variables --->
<cffunction name="checkValue" access="public">=
<cfoutput>Checkvalue is checking the value of testvar: #variables.testvar#</cfoutput>
</cffunction>
</cfcomponent>
The output is
init() just set variables.testvar.
Checkvalue is checking the value of testvar: Okay, set
Calling page is checking the existence of testvar: false
Looking through the logs, we're getting hundreds of the following
"Error","jrpp-185","08/21/12","10:05:43","PATH","www.domain.com
Agent:Mozilla/4.0 (compatible; Synapse)
Error: An exception occurred when invoking a event handler method from Application.cfc.
The method name is: onRequest.
They seem to be mostly search bots. The on place on APplication.cfc that I can see reference to the function is below
<cffunction name="onRequest" returnType="void">
<cfargument name="targetPage" type="String" required=true/>
<cfsetting enablecfoutputonly="yes" requesttimeout="20">
<cfparam name="url.refresh" default="0">
<cfset request.strMember = Duplicate(session.strMember)/>
<cfset request.tNow = GetTickCount()>
<cfif url.refresh EQ 0>
<cfset request.iCacheHr = 12/>
<cfelse>
<cfset request.iCacheHr = 0/>
</cfif>
<cflogin>
<cfif IsDefined("session.strMember.sRoles")>
<cfloginuser name="#session.strMember.sFirstName##session.strMember.sLastName#"
password="12345"
roles="#session.strMember.sRoles#"/>
</cfif>
</cflogin>
<cfinclude template="core/incl/SessionLogger.cfm">
<cfinclude template="core/incl/LinkTranslator.cfm">
<cfinclude template="core/incl/udf.cfm">
<cfinclude template="urlcheck.cfm"/>
<cfinclude template="#Arguments.targetPage#">
</cffunction>
From that, can anyone please advise on what's wrong and how to fix it? I'm fairly new to CF and this is making me pull out what little hair I have left
1) You use two different coding styles
<cfparam name="url.refresh" default="0">
<cfset request.strMember = Duplicate(session.strMember)/>
Invalid/left open XML tags in first line and valid (closed) XML tags in the second line.
Try to stick to one (preferably the last one).
2) You are using old way of checking variable being defined
IsDefined("session.strMember.sRoles")
read about newer (and better and faster)
StructKeyExists(session.strMember, "sRoles")
3) Most likely your code is calling
<cfloginuser ... >
at every page request
4) Make sure that paths for all includes are correct and they themselves don't have any errors.
Simplify your method until you stop getting an error and then investigate what exactly is causing it
Are the bots hitting a page that doesn't exist?
Maybe try changing the last line to:
<cfif fileExists(expandPath(Arguments.targetPage))>
<cfinclude template="#Arguments.targetPage#">
<cfelse>
<cfabort>
</cfif>
Maybe you could detect if they are a bot and server them something else? depends on how search friendly you want your site to be:
http://www.bennadel.com/blog/1083-ColdFusion-Session-Management-And-Spiders-Bots.htm
I'm seeing some weird behaviour when using CF's local scope in an object literal, in function arguments. But only when executed inside a loop...
Example code:
<cffunction name="f">
<cfoutput>
<cfset LOCAL.foo = 123 />
<!--- Works fine --->
#serializeJSON({blah = LOCAL.foo})#
<!--- Works fine --->
<cfloop from=1 to=1 index="i">
<cfset bar = {blah = LOCAL.foo} />
#serializeJSON(bar)#
</cfloop>
<!--- Element FOO is undefined in LOCAL --->
<cfloop from=1 to=1 index="i">
#serializeJSON({blah = LOCAL.foo})#
</cfloop>
</cfoutput>
</cffunction>
<cfset f() />
PS: serializeJSON() is just for example purposes. This is happening in any function I've tested where one of the arguments is a struct.
Works just fine in Railo.
It also doesn't make any difference if using any other container instead of local scope, also it's impossible to catch this with cftry.
If you serialize just local scope within the loop:
<cfloop from=1 to=1 index="i">
#serializeJSON(local)#
</cfloop>
Result is:
{"ARGUMENTS":{},"___IMPLICITARRYSTRUCTVAR1":{"BLAH":123},"___IMPLICITARRYSTRUCTVAR0":{"BLAH":123},"FOO":123}
Looks like a bug. Mind filing?
LOCAL is a scope used only within functions. If you try to create a LOCAL scope variable outside of a function, it will fail.
I will write up a test and prove it to you in a minute....
UPDATE Actually, I have CF 8 at work and can't test it.
In CF8 and below, you can set LOCAL.Foo, but it's not really a CF scope.
In CF9 and above, LOCAL can be set only within a function.
<cffunction>
<cfset LOCAL.foo = 1>
<cfreturn LOCAL.foo>
</cffunction>
Thanks for replying!! But I am still not able to do it. Error that I am getting is
"Element objGet1 is undefined in a Java object of type class coldfusion.runtime.VariableScope."
Below is my full code. I just want to dump the value of each thread containing cfhttp information.
http://www.google.com/search?" & "q=Vin+Diesel" & "&num=10" & "&start=") />
<cfset intStartTime = GetTickCount() />
<cfloop index="intGet" from="1" to="10" step="1">
<!--- Start a new thread for this CFHttp call. --->
<cfthread action="run" name="objGet#intGet#">
<cfhttp method="GET" url="#strBaseURL##((intGet - 1) * 10)#" useragent="#CGI.http_user_agent#" result="THREAD.Get#intGet#" />
</cfthread>
</cfloop>
<cfloop index="intGet" from="1" to="10" step="1">
<cfthread action="join" name="objGet#intGet#" />
<cfdump var="#Variables['objGet'&intGet]#"><br />
</cfloop>
and when I use after thread joining inside the loop. I get the desired results
Thanks!!
Two problems happening here.
As pointed out by Zugwalt, you need to explicitly pass in variables that you want to reference within the scope of your thread. He missed the CGI variable, that scope doesn't exist within your thread. So we pass in just what we need to use in the thread, userAgent, strBaseURL, and intGet.
Second problem, once joined, your threads are not in variable scope, they are in the cfthread scope, so we have to read them from there.
Corrected code:
<cfloop index="intGet" from="1" to="2" step="1">
<!--- Start a new thread for this CFHttp call. Pass in user Agent, strBaseURL, and intGet --->
<cfthread action="run" name="objGet#intGet#" userAgent="#cgi.http_user_agent#" intGet="#intGet#" strBaseURL="#strBaseURL#">
<!--- Store the http request into the thread scope, so it will be visible after joining--->
<cfhttp method="GET" url="#strBaseURL & ((intGet - 1) * 10)#" userAgent="#userAgent#" result="thread.get#intGet#" />
</cfthread>
</cfloop>
<cfloop index="intGet" from="1" to="2" step="1">
<!--- Join each thread --->
<cfthread action="join" name="objGet#intGet#" />
<!--- Dump each named thread from the cfthread scope --->
<cfdump var="#cfthread['objGet#intGet#']#" />
</cfloop>
Generally, unscoped variables get put into the Variables scope, so you can use struct bracket notation to refer to them:
Variables['objGet#intGet#']
or
Variables['objGet'&intGet]
These are both basically doing the same thing - just different syntaxes.
Code run inside a cfthread tag has its own scope. Try passing the variable you want it to access as an attribute. I like to name it something different just to help me keep track.
<!--- Start a new thread for this CFHttp call. --->
<cfthread action="run" name="objGet#intGet#" intGetForThread="#intGet#">
<cfhttp method="GET" url="#strBaseURL##((intGetForThread- 1) * 10)#" useragent="#CGI.http_user_agent#" result="THREAD.Get#intGetForThread#" />
</cfthread>