Infinite Loop - Queue Processing - Running as a Service - Advice needed

I have a Scriptcase Blank application that runs a query to get records from a database table. It “processes” these records, then removes them.
This is my “Queue” of what needs to be done.
It works perfectly, the problem I have now is I need it to run this check every 5 seconds. I can not create a CRON job for less than 1 minute interval, not to mention that when it calls this blank application it might run for more than 1 minute and I don’t want two instances of it running.

I have googled solutions but not really found a good solution as it pertains to Scriptcase.
PHP code that checks if the current script is running by its name does not work since all Scriptcase applications are called “index.php”.

I need an always running script, like an infinite loop… I tried doing a SC_redir at the end of my application redirecting back to itself with a 5 second SLEEP command… this works perfectly, but eventually dies… and I need it to run forever.

Can anyone offer a suggestion on how to achieve this?

Thank you for any suggestions!

Just an update on this… my application of type “Blank” works perfectly from the browser but if I call it directly from the command line ex: php Myapp.php it just returns CSS code to me and the PHP of the onExecute event does not appear to run.
So my first step might be re-writing this application to not use Scriptcase specific macros and getting it to run from the command line. Then I can tackle getting only one instance of it running at a time.

I will report back if this works… First thing, I did not have to rewrite my application… instead I am using wget in the cron job to call it on a scheduled basis, this runs it like its from a browser instead of from the command interpreter.

Now to get it to run only one time and not any more than one time I am doing this:


// In OnExecute of type "Blank" Application
$running = exec("ps -aux | grep QueueProcessor|grep -v grep|wc -l");

while($running<=2){ // while loop
    $running = exec("ps -aux | grep QueueProcessor|grep -v grep|wc -l");

    /* All my code goes here */

}

To explain whats happening here… I am calling this application via a Cron job where I am setting the output file for the wget to be a name “QueueProcessor.txt”.

My CronJob runs this command:

 wget -O /var/log/QueueProcessor.txt http://XXX.XXX.XXX.XXX/PATH/TO/APPLICATION

So in my blank type application in the OnExecute I am checking the running processes for that name “QueueProcessor” by issuing the linux command:

ps -aux | grep QueueProcessor|grep -v grep|wc -l

This command is actually 4 statements with pipes between them.

  1. ps-aux - Lists all running processes
  2. grep QueueProcessor - Shows only those with the word “QueueProcessor” in them.
  3. grep -v grep - Removes the line of the grep statement itself
  4. wc -l - Thats a lower case L there… it counts how many lines are returned.

For me the number of lines is always 2 when the cron job is running… so the loop only runs while there is only one instance of my application running.

I am about to test to see if this works for me and will update/edit this post if not.

Just an update to this… its not working. The fact is since I am calling the application via “wget” its creating an apache process that runs my code… causing an infinite loop since I am unable to find the apache process thats running my code. Even if I could find the apache process thats running my code I wouldn’t want to kill it as the same process could be handling other user requests.

So my next step is to figure out if there is any way to run a Scriptcase application from the console… aka command line. I don’t think thats possible.

You can achieve the infinite-loop in PHP which runs every 5 seconds with something like this:


<?php
ini_set("max_execution_time", 0);
set_time_limit(0);

function doAllTheJob(){
    // here you should do everything you need (database changes, etc)
}

while(true){
    doAllTheJob();
    sleep(5);
}

Then you just have to make sure that script/application doesn’t run multiple times using some kind of lock:
https://github.com/mamchenkov/locker

(the first answer, using socket, seems to fit better) :
https://stackoverflow.com/questions/…es-of-a-script