Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1353
  • Last Modified:

Wait until file exits before proceeding

I have a web page (in CF) that generates a swf flash chart from a complex mysql query.
The problem ... I try to display the flash chart but the page loads it before its finished!

SO ... I need a way to 'pause' [wait] my coldfusion page UNTIL a file exists in a specific directory on my server.
Is this possible?

Thanks!
Richard
0
rcbuchanan
Asked:
rcbuchanan
  • 6
  • 6
  • 4
  • +1
2 Solutions
 
pinaldaveCommented:
Hi rcbuchanan,
you can use loop for certain values and wait according to that.
Though the better architecture is that you do this in following order.

mysql query
html code
flash
htmlends
you will never find this problem of page loads before sql prepares the results...

Regards,
---Pinal
0
 
pinaldaveCommented:
if you can post your archi or design may be you will not have to wait for the results ...they will come before you start displaying your html code.
0
 
rcbuchananAuthor Commented:
No. I'm hoping I can use 'fileexists' or such like ... where I can delay or LOOP until the filexists condition is TRUE.

Is that possible? i.e. LOOP (continuously) UNTIL fileexists = yes?

re your suggestion for better architecture. That won't help ... as I have multiple template cfm pages loading on my page ... with many mysql selects etc.

Richard
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
pinaldaveCommented:
Hi rcbuchanan,
you can do that for sure http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/functi79.htm#wp1104406
now let us look the good thing : this may solve your problem
bad thing: not sure but if due to some reason this falls in the infinite look and may take your complite processing power and brings your server down. This is arguable but if things are very complex this may happen and server uses its all the memory and it stops responding.

If this does not happen you can happily use the fileexists you have mentioned there is example in the link, just put that in CFIF and it will work great. I have done this before( not in loop just checking and if it is there just move to some direction like that.)

Regards,
---Pinal
0
 
rcbuchananAuthor Commented:
can I use <CFLOOP></CFLOOP> that loops until #fileexists('filename')# is 'yes'? or such like?
do you know the syntax?
0
 
pinaldaveCommented:
Hi rcbuchanan,
I think this is the way to do this if you want to follow your logic. That is fine too... you may have to change things according to your code, I have wrote here basic design and usage of cfexit which will bring you out from your loop and go further in the progress.
you can make this fundtion and call from other page also if you want.  


<cfloop index=i from=1 to=100000>
<cfif fileexists('filename') eq 'yes'>
<cfexit>
</cfif>
</cfloop>


Regards,
---Pinal
0
 
rcbuchananAuthor Commented:
pinaldave:
thanks. well - ... i'm not good at cf. can I then do a loop that repeats EITHER for - say - 20 seconds OR until fileexists is 'yes' ?  that way ... I can either forgot about displaying chart if technical problem means it didn't get generated or I display it!  (if 20 seconds condition is reached - DON'T DISPLAY else if fileexists = yes ... DO display'

Any ideas? for the cf code that might do this for me? i suck at understanding the cfloop condition function.

Richard
0
 
rcbuchananAuthor Commented:
PERFECT! I'll try it!  thanks!
0
 
pinaldaveCommented:
Hi rcbuchanan,
good good. There is nothing in CF which can loop for some time. Though, you can just assume your processing power and page execution lenth and put the to condtion in cfloop which suites you. this is trick but the time could have been better solution. after certain time the loop will be over itself and will process futhre. Do not say that you are not good at CF. I am learning everyday something. Today you tought me this trick. I am just writing syntax but you gave me idea of file exist usage in loop. So you are designer. Smiles,

Regards,
---Pinal
0
 
anandkpCommented:
A conditional loop iterates over a set of instructions while a given condition is TRUE. To use this type of loop correctly, the instructions must change the condition every time the loop iterates until the condition evaluates as FALSE. Conditional loops are commonly known as WHILE loops, as in "loop WHILE this condition is true. "

Syntax
<CFLOOP CONDITION="expression">

CONDITION
Required. Sets the condition that controls the loop. The loop will repeat as long as the condition evaluates as TRUE. When the condition is FALSE, the loop stops.
u cld use this to check for file exists - see if it helps u ...
----------------
thou i wld suggest u look into CFFLUSH & use that to determine ur output ...
syntax : <cfflush interval = integer number of bytes>

that way u avoid the unwanted processing by the loops & as soon as u have "X" no. of bytes with u - u can flsuh it to the client browser ... pretty much the way u require - is it ?

K'Rgds
Anand
0
 
pinaldaveCommented:
Hi rcbuchanan,
did it helped? just curious.

Regards,
---Pinal
0
 
jyokumCommented:
<!--- this will loop until the file [filename] exists or 20 seconds has passed --->
<cfscript>
filename=ExpandPath('filename.txt');
tStop=DateAdd('s',20,now());
for( ; ; ){
      if (FileExists(filename)) {
            WriteOutput('the file was found');
            break;
      } else if (DateCompare(now(),tStop) gte 0) {
            WriteOutput('operation timed out');
            break;
      }
}
</cfscript>

it's important to note that this is a VERY expensive operation... on my machine, it typically eats up about 90% cpu for the duration of the loop.
0
 
jyokumCommented:
here's a much better method that's not processor intensive

<cfscript>
filename=ExpandPath('filename.txt');
tStop=DateAdd('s',20,now());
o_file = CreateObject('java','java.io.File').init(filename);
thread = CreateObject("java", "java.lang.Thread");
fileFound = false;
while(DateCompare(now(),tStop) lt 0) {
      if(o_file.exists()) {
            fileFound = true;
            break;
      }
      else thread.sleep(1000);
}
</cfscript>
<cfif fileFound>
      THE FILE WAS FOUND
<cfelse>
      OPERATION TIMED OUT
</cfif>
0
 
rcbuchananAuthor Commented:
jyokum:
thanks. ... question : the 'filename' I need to 'check for' ... is based as follows:

<cfset string_opid = #numberformat(url.op,"00000")#>
<cfset string_date = #dateformat(now(),"yyyymmdd")#>
<cfset swfFilename = #string_opid# & #string_date# & ".swf">
<CFSET dirpath = "c:\web\opinionist\dev\htdocs\charts\">
<CFSET lookupFilename = #dirpath# & #swfFilename#>

how can I modify your suggested <cfscript> to look for the file as defined by 'lookupFilename' rather than 'filename.txt') ?

ALSO ... the file I need to look for ... is a flash file (.swf) created by SwiffCharts (see globfx.com).
I wonder ... I cannot 'show' the flash chart UNTIL it's completed (processed).  Would it be wiser for me to use your code to ONLY view the file when it exists and its filesize > 0 bytes?

My problem: I think my page is trying to a) generate the flash chart and b) display it BEFORE it is finished being created!  I think I should wait until the file exists AND it's file size > 0 (blank?)

Does this sound feasible to you?

pinaldave : i'm sorry ... but your suggestion just doesn't want to work in my page!

Richard



some  more info:
--------------------

I have a .cfm web page that DOES these two things:
      <cfinclude template="process_opinion_linechart.cfm">
      <cfinclude template="template_opiniontimeline.cfm">

the "process_opinion_linechart.cfm":
------------------------------------------

<cfset string_opid = #numberformat(url.op,"00000")#>
<cfset string_date = #dateformat(now(),"yyyymmdd")#>
<cfset swfFilename = #string_opid# & #string_date# & ".swf">
<CFSET dirpath = "c:\web\fred\dev\htdocs\charts\">

<cfif fileexists(#dirpath# & #swfFilename#) eq 'yes'>
<cfelse>

<!--- Sets the filename of the style.  This assumes that the style is in the same directory as this script --->
<cfset chartStyle = "newline460.scs">

<!--- Load Opinion Data --->
<cfquery name="loadData" datasource="fred">
select opinionvotes.vote_date, sum(opinionvotes.vote_want_yes) as want_yes, sum(opinionvotes.vote_want_no) as want_no, sum(opinionvotes.vote_agree_yes) as agree_yes, sum(opinionvotes.vote_agree_no) as agree_no from opinionvotes where opinionvotes.opinion_id=#url.op# group by opinionvotes.vote_date order by opinionvotes.vote_date
</cfquery>       
        
<!--- Don't have to touch this --->
<CFSETTING ENABLECFOUTPUTONLY="Yes" SHOWDEBUGOUTPUT="No">

<CFOBJECT TYPE="COM"
        NAME="chart"
        CLASS="SwiffChartObject.ChartObj.1"
        ACTION="CREATE">

<!--- Define seperators rules. Don't have to touch this --->
<CFSET sep= ";">
<CFSET ignoremultseparators= true>
<CFSET chart.SetSeparators(sep, ignoremultseparators)>

<cfset agree_total_yes = 0><cfset agree_total_no = 0>
<cfset want_total_yes = 0><cfset want_total_no = 0>
<cfset agree_string = ''><cfset want_string = ''>
<cfset myCategories = ''>
<cfset pointer = 0>

<cfloop query="loadData">

<!--- Create a string for each series --->
<CFSET myCategories= myCategories & "#dateformat(loadData.vote_date,'mm/dd')#" & sep>

<cfset agree_total_yes = agree_total_yes + #loadData.agree_yes#>
<cfset agree_total_no = agree_total_no + #loadData.agree_no#>
<cfset want_total_yes = want_total_yes + #loadData.want_yes#>
<cfset want_total_no = want_total_no + #loadData.want_no#>

<!--- Create a string for each series --->
<cfset agree_percent = #round(agree_total_yes / (agree_total_yes + agree_total_no) * 100)#>
<cfset want_percent = #round(want_total_yes / (want_total_yes + want_total_no) * 100)#>
<cfset agree_string = agree_string & "#agree_percent#" & sep>
<cfset want_string = want_string & "#want_percent#" & sep>

</cfloop>
 
<!--- Fill the chart with the series --->
<CFSET chart.SetCategoriesFromString(myCategories)>
<CFSET chart.AddSeries()>
<CFSET chart.SetSeriesValuesFromString(0, agree_string)>
<CFSET chart.AddSeries()>
<CFSET chart.SetSeriesValuesFromString(1, want_string)>
<CFSET style = dirpath & chartStyle>
<CFSET chart.LoadStyle( style )>

<!--- Set the dimensions of the movie.  IF YOU CHANGE THIS, YOU NEED TO CHANGE THE SIZE BELOW TO REFLECT THIS! --->
<CFSET chart.SetWidth( 460 )>
<CFSET chart.SetHeight( 200 )>

<!--- This just sets where the graph gets generated --->
<CFSET account_id = "100">
<CFSET filename = dirpath & swfFilename>
<CFSET chart.ExportAsFile(filename)>
<cfset chartInfo.filename = swfFilename>

</cfif>


the "template_opiniontimeline.cfm" does this:
----------------------------------------------------

<cfset string_opid = #numberformat(url.op,"00000")#>
<cfset string_date = #dateformat(now(),"yyyymmdd")#>
<cfset swfFilename = "http://www.fred.com/charts/" & #string_opid# & #string_date# & ".swf">

<table width="460" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td width="460" colspan="1"><img name="label_hottopics" src="/newopex_graphics/label_opiniontimeline460x30.jpg" width="460" height="30" alt="" border="0"></td>
  </tr>
  <tr>
        <td width="460" height="200">
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="460" height="200">
        <param name="movie" value="<cfoutput>#swfFilename#</cfoutput>">
        <param name="quality" value="high">
        <embed src="<cfoutput>#swfFilename#</cfoutput>" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="460" height="200"></embed>
      </object>
      </td>
  </tr>
</table>



SO - When I run these pages ... sequentially : the process cfm then the display page ... I DON'T get the flash chart ... but instead ... only the swf page's file name as text!

BUT if I look at my /chart/ directory .. I CAN see the flash file HAS been successfully created!
AND when I run the page a 2nd time ... the flash chart DOES successfully LOAD!

Any ideas!?
Richard
0
 
jyokumCommented:
this adds some checking for the file size and uses your file name

<cfset string_opid = numberformat(url.op,"00000")>
<cfset string_date = dateformat(now(),"yyyymmdd")>
<cfset swfFilename = string_opid & string_date & ".swf">
<cfset dirpath = "c:\web\opinionist\dev\htdocs\charts\">
<cfset lookupFilename = dirpath & swfFilename>

<cfscript>
tStop=DateAdd('s',5,now());
o_file = CreateObject('java','java.io.File').init(lookupFilename);
thread = CreateObject("java", "java.lang.Thread");
fileFound = false;
while(DateCompare(now(),tStop) lt 0) {
      if(o_file.exists() AND o_file.length() gt 0) {
            fileFound = true;
            break;
      }
      else thread.sleep(1000);
}
</cfscript>
<cfif fileFound>
      THE FILE WAS FOUND
<cfelse>
      OPERATION TIMED OUT OR THE FILE IS 0 BYTE
</cfif>
0
 
jyokumCommented:
oops, just noticed I left some test data in

change this
     tStop=DateAdd('s',5,now());
to
     tStop=DateAdd('s',20,now());

The number is the seconds to wait before timing out
0
 
rcbuchananAuthor Commented:
did exactly what i asked for - thanks!
alas I'm still experiencing my error BUT I'lll open a new thread and will try again!

R.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now