Search code examples
coldfusioncoldfusion-9

Suspend and resume ColdFusion code for server intensive process


I have a loop the runs over a couple thousand records and for each record it hits is does some image resizing and manipulation on the server. The process runs well in testing over a small record set but when it moves to the live server I would like to suspend and resume the process after 50 records so the server is not taxed to the point of slow performance or quits altogether.

The code looks like this:

<cfloop query="imageRecords">
    <!--create and save images to server - sometimes 3 - 7 images for each record -->
</cfloop>

Like I said, I would like to pause after 50 records, then resume where it left off. I looked at cfschedule but was unsure of how to work that into this.

I also looked at the sleep() function but the documentation talks about using this within cfthread tags. Others have posted about using it to simulate long processes.

So, I'm not sure sleep() can be safely used in the fashion I need it to.

Server is CF9 and db is MySQL.


Solution

  • Here's a different approach which you could combine with probably any of the other approaches:

    <cfscript>
    th=CreateObject("java","java.lang.Thread");
    th.setPriority(th.MIN_PRIORITY);
    // Your work here
    th.setPriority(th.NORM_PRIORITY);
    </cfscript>
    

    That ought to set the thread to have lower priority than the other threads which are serving your other requests. In theory, you'll get your work done in the shortest time, but with less affect on your other users. I've not had opportunity to test this yet, so your mileage may vary.