Every once in a while, we run across a problem with an implementation of a system that is solved in the ‘next version’ of a software system we currently have in place. Of course the next version would potentially also include the complete degradation of software to the point where anything written up to current code would not work. Considering that we do have some legacy applications for clients who do not want to upgrade just yet, an upgrade could become a big problem. So rather than causing the unnecessary stress on us and our clients, we use our collective genius and make do with what we have. On Thursday, the dilemma was this: we have a system that needs to run an ‘out of process’ queue without the fun threading that CF 8 can do. The solution is a queue that runs a scheduled task which then checks if it needs to be run again at the end, and schedules itself again.
The process involves uploading a file and doing some things with that file using a com object (it could be java, doesn’t matter really). The problem is, we want to queue these files up so that they do not overrun each other, causing the processing to either fail or bring down the server with too many uploads happening at once (it’s not like we have Youtube’s processing power). Coldfusion uploads the file and inserts it in to a database table “file processing queue.” That table is then queried to retrieve the file name of the next file in the queue and processed accordingly, then removed from the queue. In initial programming, the files were being uploaded and processing was taking place within the coldfusion thread, which if we have multiple files being run at once, seemed to cause a big problem. The queue could not then continue the processing as it was no longer running in the window. By using <cfschedule action = “update” interval=”once” task=”mytaskname”> we are able to set up a scheduled task, named ‘mytaskname’ (which we’ve named something more unique to the site and the processing) which then continues to run until the queue is complete by calling a function that checks to see if there are any items in the queue, and if so, calls the scheduler again.