Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event Gateway Assistance

Status
Not open for further replies.

drumsticks

Programmer
Jun 5, 2004
18
0
0
US
Hello,

I'm getting my feet wet using Async Gateways with CF8, and I have created a loop which calls the gateway X amount of times.
The gateway request then logs the data, however, it is logging X requests, but only the actual data from the last request in the loop. Thus, it's populating the table with duplicate records of the last request X amount of time from the loop.

I may be mistaken, but it seems as though it's sharing the same thread as it is being overwritten with the latest request, rather than creating individual threads for each request.
To the best of my knowledge, I have declared all vars within the cfc method with 'var', except for the query names. Could it be that the query names need to be localized somehow?

Do I need to add something like cfthread? I'm not exactly sure why I'm still getting unreliable results. Some of the code is below.

Any feedback is welcomed!

Thanks,
Ryan

coldfusion Syntax (Toggle Plain Text)

1.
<cfcomponent output="no">
2.
<cffunction name="scoresummary" returntype="void" output="no">
3.
<cfargument name="cfevent" required="yes">
4.

5.
<!---Keeps our position as we search through the response--->
6.
<cfset var lnIndex = 0>
7.

8.
<!---Begin the index point after the meaningless header content junk by searching for the tag below--->
9.
<cfset var lcBeginContentStartSearch = '<div class="fctitle"> Scoring Summary</div> '>
10.
<cfset var lcEndContentSearch = "</table> ">
11.
<cfset var lcquarterTRtag = "align=left class=bg1">
12.

13.
<!---Locates Game Quarters and Overtime--->
14.
<cfset var summary = arraynew(1)>
15.
<cfset var scores = arraynew(2)>
16.
<cfset var game = structnew()>
17.
<cfset var x = 0>
18.
<cfset var y = 0>
19.
<cfset var z = 1> <!--- Since this code was placed into a gateway, the 'z' incremental loop variable is not needed to store all website requests. Permanently mark as 1. --->
20.
<cfset var db = arguments.cfevent.data>
21.
<cfset var lccontent = ''>
22.
<cfset var lnendindex = 0>
23.

24.
<!---Locates anchor tags for extraction--->
25.
<cfset var lcanchortag = "<a">
26.
<cfset var lctagend = "> ">
27.
<cfset var lcendtag = "</">
28.

29.
<!---Locates each scoring row--->
30.
<cfset var lcTRtag = "<tr">
31.
<cfset var lcendTRtag = "</tr> ">
32.
<cfset var lcTDtag = "<td">
33.
<cfset var lcTDendtag = "</td> ">
34.

35.
<!---Find Starting Point--->
36.
<cfset var maxloop = 4>
37.
<cfset var lnnextquarterindexTR = 0>
38.
<cfset var lnindexTR = 0>
39.
<cfset var thehttp = ''>
40.
<cfset var teamid = 0>
41.
<cfset var sumidx = 0>
42.
<cfset var score = 0>
43.
<cfset var extrapoint = 0>
44.
<cfset var extratype = 0>
45.

46.
<!---Get Request--->
47.
<!---Get the website content from lcquery above. --->
48.
<cfhttp
49.
result = "local.thehttp"
50.
url = " 51.
method = "get"
52.
resolveurl = "yes">
53.
</cfhttp>
54.

55.
<!---Get essential HTML--->
56.
<cfset lccontent = local.thehttp.filecontent>
57.
<cfset lnindex = findnocase(lcBeginContentStartSearch, lccontent)>
58.
<cfset lnendindex = findnocase(lcendcontentsearch, lccontent, lnindex) + len(lcendcontentsearch)>
59.
<cfset lccontent = mid(lccontent, lnindex, lnendindex - lnindex)>
60.
<cfset db.content = lccontent>
61.

62.
<cfloop from="1" to="#maxloop#" index="x">
63.
<cfset lnindexTR = findnocase(lcquarterTRtag, lccontent, lnindexTR) + len(lcquarterTRtag)>
64.
<cfif lnindexTR lte 0>
65.
<cfexit>
66.
</cfif>
67.
<cfset lnindex = findnocase(db[x]['searchstring'], lcContent, lnindexTR) + len(db[x]['searchstring'])>
68.
<cfset lnnextquarterindexTR = findnocase(lcquarterTRtag, lccontent, lnindex) + len(lcquarterTRtag)>
69.
<cfif lnnextquarterindexTR lt lnindex>
70.
<cfset lnnextquarterindexTR = len(lccontent)>
71.
</cfif>
72.
<cfset lnnextquarterindex = iif(x eq maxloop, de(len(lccontent)), de(findnocase(db[x + 1]['searchstring'], lcContent, lnnextquarterindexTR)))>
73.

74.
<cfset lnindex = findnocase(lctdtag, lccontent, lnindex)>
75.
<cfset y = 0>
76.
<cfloop condition="lnindex lt lnnextquarterindexTR">
77.
<cfset y = y + 1>
78.

79.
<!--- First TD: Scoring Team --->
80.
<cfset lntempindex = findnocase(lctagend, lccontent, lnindex) + len(lctagend)>
81.
<cfset lnindex = findnocase(lcendtag, lccontent, lntempindex)>
82.
<cfset scores[y][1] = mid(lccontent, lntempindex, lnindex - lntempindex)>
83.

84.
<cfif scores[y][1] neq 'None'>
85.
<cfset lnindex = findnocase('.gif', lccontent, lntempindex)>
86.
<cfif lnindex gt 0>
87.
<cfset scores[y][1] = arraytolist(rematchnocase('[A-Za-z]+', mid(lccontent, lnindex - 3, 3)))>
88.
<cfelse>
89.
<cfset scores[y][1] = -1>
90.
</cfif>
91.
<!--- Scoring Team ID --->
92.
<cfif lcase(trim(scores[y][1])) eq lcase(trim(db.hometeam))>
93.
<cfset teamid = db.hometeam_id>
94.
<cfelse>
95.
<cfset teamid = db.awayteam_id>
96.
</cfif>
97.
<cfset scores[y][11] = teamid>
98.

99.
<!---Team[#lnindex#]: #scores[y][1]#<br> --->
100.

101.
<!--- Second TD: Type of Score --->
102.
<cfset lntempindex = findnocase(lctdtag, lccontent, lnindex)>
103.
<cfset lnindex = findnocase(lctagend, lccontent, lntempindex) + len(lctagend)>
104.
<cfset lntempindex = findnocase(lcendtag, lccontent, lnindex)>
105.
<cfset scores[y][2] = mid(lccontent, lnindex, lntempindex - lnindex)>
106.
<cfset lnindex = lntempindex>
107.

108.
<!--- Third TD: Summary of Score --->
109.
<cfset lntempindex = findnocase(lctdtag, lccontent, lnindex)>
110.
<cfset lnindex = findnocase(lctagend, lccontent, lntempindex) + len(lctagend)>
111.
<cfset lntempindex = val(findnocase(lcTDendtag, lccontent, lnindex))>
112.
<!--- Remove all anchor tag data from the summary--->
113.
<cfset summary = mid(lccontent, lnindex, lntempindex - lnindex)>
114.
<cfset lnindex = lntempindex>
115.
<cfset lnsummaryindex = findnocase(lcanchortag, summary)>
116.

117.
<!---Remove all anchor end tags in summary variable--->
118.
<cfif lnsummaryindex neq 0>
119.
<cfset summary = replacenocase(summary, '</a>', '', 'all')>
120.
</cfif>
121.
<cfloop condition="lnsummaryindex neq 0">
122.
<cfset lcsummaryvalue = mid(summary, lnsummaryindex, findnocase(lctagend, summary, lnsummaryindex) - lnsummaryindex + 1)>
123.
<cfset summary = replacenocase(summary, lcsummaryvalue, '')>
124.
<cfset lnsummaryindex = findnocase(lcanchortag, summary)>
125.
</cfloop>
126.

127.
<!--- yards --->
128.
<cfset scores[y][3] = summary>
129.
<cfset sumidx = findnocase(' yards', summary)>
130.
<cfif sumidx gt 0>
131.
<cfset scores[y][4] = trim(mid(summary, sumidx - 2, 2))>
132.
<cfelse>
133.
<cfset scores[y][4] = 0>
134.
</cfif>
135.

136.
<!--- clock time --->
137.
<cfset sumidx = findnocase(':', summary)>
138.
<cfif sumidx gt 0>
139.
<cfset scores[y][5] = trim(mid(summary, sumidx - 2, 5))>
140.
<cfelse>
141.
<cfset scores[y][5] = ''>
142.
</cfif>
143.

144.
<!--- drive time --->
145.
<cfset sumidx = findnocase('in ', summary)>
146.
<cfif sumidx gt 0>
147.
<cfset scores[y][6] = trim(mid(summary, sumidx + 2, 5))>
148.
<cfelse>
149.
<cfset scores[y][6] = ''>
150.
</cfif>
151.

152.
<!--- plays --->
153.
<cfset sumidx = findnocase(' plays', summary)>
154.
<cfif sumidx gt 0>
155.
<cfset scores[y][7] = trim(mid(summary, sumidx - 2, 2))>
156.
<cfelse>
157.
<cfset scores[y][7] = 0>
158.
</cfif>
159.

160.
<!--- Points Scored --->
161.
<cfswitch expression="#scores[y][2]#">
162.
<cfcase value="TD">
163.
<cfset score = 6>
164.
</cfcase>
165.
<cfcase value="FG">
166.
<cfset score = 3>
167.
</cfcase>
168.
<cfcase value="SAFETY">
169.
<cfset score = 2>
170.
</cfcase>
171.
<cfdefaultcase>
172.
<cfset score = 0>
173.
</cfdefaultcase>
174.
</cfswitch>
175.
<cfset scores[y][8] = score>
176.

177.
<!--- Extra Point(s) and Type of Extra Score --->
178.
<cfset sumidx = findnocase('(', summary) + 1>
179.
<cfif sumidx gt 1>
180.
<cfset extrapoint = lcase(mid(summary, sumidx, findnocase(')', summary) - sumidx))>
181.
<cfif findnocase('kick is good', extrapoint)>
182.
<cfset extrapoint = 1>
183.
<cfset extratype = 'Kick'>
184.
<cfelseif findnocase('2 pt. conversion', summary)>
185.
<cfif findnocase('failed', summary)>
186.
<cfset extrapoint = 0>
187.
<cfelse>
188.
<cfset extrapoint = 2>
189.
</cfif>
190.
<cfset extratype = '2pt Conversion'>
191.
<cfelse>
192.
<cfset extrapoint = 0>
193.
<cfset extratype = ''>
194.
</cfif>
195.
<cfelse>
196.
<cfset extrapoint = 0>
197.
<cfset extratype = ''>
198.
</cfif>
199.
<cfset scores[y][9] = extrapoint>
200.
<cfset scores[y][10] = extratype>
201.
</cfif>
202.

203.
<cfset lnindex = findnocase(lctdtag, lccontent, lnindex)>
204.

205.
<cfif lnindex eq 0>
206.
<cfbreak>
207.
</cfif>
208.
</cfloop>
209.

210.
<cfset db[x]['value'] = scores>
211.
<cfset arrayclear(scores)>
212.
<cfif lnindexTR lte 0>
213.
<cfexit>
214.
</cfif>
215.
</cfloop>
216.
<cfset game.game_id = db.game_id>
217.
<cfset game.data = db>
218.
<cfset ss[z] = game>
219.

220.
<!--- INSERT DATA TO SCORESUMMARY TABLE --->
221.
<cfif arraylen(ss) gt 0>
222.
<cftransaction>
223.
<cfquery name="getscores_#game.game_id#" datasource="datasource">
224.
SELECT *
225.
FROM scoresummary s
226.
WHERE <cfif len(ss[z]['data']['game_id'])>
227.
s.game_id = #ss[z]['data']['game_id']#
228.
<cfelse>
229.
1 = 0
230.
</cfif>
231.
</cfquery>
232.
<cfloop from="1" to="#arraylen(ss)#" index="x">
233.
<cfloop from="1" to="5" index="y">
234.
<cfif isarray(ss[x]['data'][y]['value'])>
235.
<cfloop from="1" to="#arraylen(ss[x]['data'][y]['value'])#" index="z">
236.
<cfparam name="#ss[x]['data'][y]['value'][z][1]#" default="none">
237.
<cfif ss[x]['data'][y]['value'][z][1] neq 'none'>
238.
<cfset timescored = ss[x]['data'][y]['value'][z][5]>
239.

240.
<cfquery name="getscore_#game.game_id#" dbtype="query">
241.
SELECT *
242.
FROM getscores_#game.game_id#
243.
WHERE clock = '#timescored#'
244.
AND quarter = #val(y)#
245.
</cfquery>
246.

247.
<cfif variables['getscore_#ss[x]['game_id']#'].recordcount>
248.
<!--- DO NOT UPDATE FOR NOW BECAUSE THIS MAY SLOW DOWN RENDERING TOO MUCH
249.
UPDATE #scoresummary#
250.
--->
251.
<cfelse>
252.
<cfquery name="insertScoreSummary_#game.game_id#" datasource="datasource">
253.
INSERT
254.
INTO scoresummary (
255.
game_id,
256.
team_id,
257.
team_short,
258.
score_type,
259.
points,
260.
extra_points,
261.
summary,
262.
quarter,
263.
drive_time,
264.
clock,
265.
plays,
266.
yards
267.
)
268.
VALUES (
269.
#ss[x]['game_id']#, <!---game ID--->
270.
#ss[x]['data'][y]['value'][z][11]#, <!---team ID--->
271.
'#ss[x]['data'][y]['value'][z][1]#', <!---team short--->
272.
'#ss[x]['data'][y]['value'][z][2]#', <!---score type--->
273.
#ss[x]['data'][y]['value'][z][8]#, <!---points--->
274.
#ss[x]['data'][y]['value'][z][9]#, <!---extra points--->
275.
'#ss[x]['data'][y]['value'][z][3]#', <!---summary--->
276.
'#y#', <!---quarter--->
277.
'#ss[x]['data'][y]['value'][z][6]#', <!---drive time--->
278.
'#ss[x]['data'][y]['value'][z][5]#', <!---game clock--->
279.
#ss[x]['data'][y]['value'][z][7]#, <!---plays--->
280.
#ss[x]['data'][y]['value'][z][4]# <!---yards--->
281.
)
282.
</cfquery>
283.
</cfif>
284.
</cfif>
285.
</cfloop>
286.
</cfif>
287.
</cfloop>
288.
</cfloop>
289.
</cftransaction>
290.
</cfif>
291.

292.
</cffunction>
293.
</cfcomponent>
 
To the best of my knowledge, I have declared all vars within the cfc method with 'var', except for the query names. Could it be that the query names need to be localized somehow?

All function local variables should be declared with 'var', including query names.

I have only skimmed the code, but can see several variables were not declared with 'var'. This may be contributing to the problem.

...
<cfhttp result = "local.thehttp" ...>
...
<cfset ss[z] = game>

I have not worked with gateways, but the function code seems rather large to me. Is there a reason you cannot break up the code into smaller, and more focused, pieces?

 
Hi cfSearching,

First of all, thank you for your reply.

I will attempt to make the query variables declared with 'var' and see what happens, as well as making sure every variable is 'localized', to boot.
I thought I had gotten all of them, so thanks for looking it over and noticing them.

In respect to the size of the method, i agree, it's very large. What I actually have in the method is a link to a cfm file so that it doesn't have all of that code housed within the actual method. Moreover, the code is enacting a 'screen-scrape', which depending on the html content being scraped, can become decently large. If you possibly have any ideas that could slim this down, I'm all ears.

I will report back on the results for you.
Thanks again, cfSearching, for your help!
 
If possible break it into smaller functions. Each function should do one thing. Such as getHTMLContent(), extractScoreType(), extractScoreSummary(), insertScore(), etcetera. Call the smaller functions where needed.

This should make the code more managable and easier to debug. Smaller functions will also make it easier to spot non-var'd variables.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top