I've got a procedure that needs to be went against some things also it is not appropriate to become went in the customers finish (15+ seconds to process) and so i considered utilizing a cron job however, this is unacceptable since it can create a back log. I've simplified my options lower either to running a never-ending procedure that monitors for mysql changes, or setting up mysql to trigger the script if this picks up a big change however the latter isn't something I wish to enter into unless of course it's my only option, which leaves me using the "endless" monitoring option.

The kind of factor I am thinking about with PHP is:

while (true) {
  $db->query('SELECT * FROM database');
  while($row = $db->fetch_assoc()){
    // do the stuff here
  }
  sleep(5);
}

after which running it through the command line. This is theoretically seem but used it is not doing in addition to I wished, using more assets than I'd expect (although not from my range, simply not what I am striving for brilliantly). So my questions are the following:

  1. Is PHP the incorrect language to get this done in? PHP is exactly what Sometimes with, however i realize that you will find occasions when it is the incorrect choice and i believe maybe this really is. If it's, what language must i use?

  2. It is possible to better approach which i haven't considered which is not the ideas These are merely?

  3. If PHP may be the correct option, how do i optimise the code I published, it is possible to method much better than sleeping for five seconds after each completed operation?

Thanks ahead of time! I am available to any ideas as lengthy as they are much less far available, I am running my very own server with free reign so there is no theoretical limit on which I'm able to do.

I suggest moving the loop out right into a spend script after which performing a brand new PHP process for each iteration. By doing this PHP won't ever use unbounded assets (even when there's a memory/connection leak somewhere) since the operation is ended on each iteration. Something similar to the next ought to be fine (Party):

while true; do
  php /path/to/your/script.php 2>&1 | logger ...(logger options) 
  sleep 5
done

I have found this method to become much more robust for lengthy-running scripts in PHP, most likely since this is very such as the way PHP works when run like a CGI script.

  1. It is best to use the word what you are most acquainted with. If this sounds like PHP, then it is not really a wrong choice.

  2. Disconnect in the database prior to sleeping. By doing this your script will not have a connection reserved, and it'll work fine despite database restart.

  3. Free mysql result after utilizing it. Check for error conditions in daemonized processes, and cope with them properly.

PHP may be the wrong language as it is really created for serving demands with an ad-hoc basis, instead of creating lengthy-running daemons. (It had been initially produced like a preprocessor language, then afterwards arrived to general use like a web application language.)

Something similar to Python might are more effective to your requirements it's a bit more naturally created for "daemon-like" programs.

Nevertheless, it is easy to do what you would like in PHP.

what type of troubles are you going through? i dont know of the database class you've there in $db, however it could produce a memory leak.

in addition i recommend closing all of your connections and unsetting all of your variables if required in the finish from the loop and re open around the beginning! if it is only 5 second sleep maby only on every tenth interation or something like that. you are able to perform a counter for your...

theese points considered theres no problem with this particular approach.