I have a mysql database table filled with 1000+ records, lets say 5000 records. Each record has a processed
boolean flag, default to false (0)
. What I'd like to do is have a PHP script run on cron every minute. Its code would be something like this:
<?php
process();
function process()
{
$sql = "SELECT id FROM items WHERE processed = '0' ORDER BY id ASC LIMIT 1";
$result = $this->db->query($sql);
if (! $result->has_rows())
die;
$id = $result->getSingle('id');
processItem($id); //Will set processed to 1 after processing is done
process();
}
?>
It should be pretty clear what the above code does, it gets the id for the next record which is un-processed, processes it, and then calls the process()
function again which repeats this process until there are no more items to be processed, at which point the execution would stop.
By putting this script on Cron to run every minute, I hope to have multiple instances of this script all working at processing the items simultaneously, so rather than processing one item at a time, 5-10+ items could be getting processed simultaneously.
1) Is this going to work the way I'm planning it? Any suggestions for improvements / things to watch out for?
2) Should I have the script set a counter for the number of running instances, so whenever a cron job starts, it checks the counter, if 50 (?) instances are running it would exit without processing. That might keep the server from crashing by having too many running processes using up too much memory? Any thoughts?