tags:

views:

53

answers:

2

See also http://stackoverflow.com/questions/2977105/having-a-php-script-loop-forever-doing-computing-jobs-from-a-queue-system/, but that doesn't answer all my questions.

If I want to run a PHP script forever, accessing a queue and doing jobs:

  1. What is the potential for memory problems? How to avoid them? (any flush functions or something I should use?)

  2. What if the script dies for some reason? What would be a good method to automatically start it up again?

  3. What would be the best basic approach to start the script. Since it runs forever, I don't need cron. But how do I start it up? (See also 2.)

+3  A: 

Set the queue up as a cron script. Have it execute every 10 seconds. When the script fires up, check if there's a lock file present (something like .lock). If there is, exit immediately. If not, create the .lock and start processing. If any errors occur, email/log these errors, delete .lock and exit. If there's no tasks, then exit.

I think this approach is ideal, since PHP isn't really designed to be able to run a script for extended periods of time like you're asking. To avoid potential memory leaks, crashes etc, continuously executing the script is a better approach.

Sam Day
Sounds great, maybe I should also rewrite my daemon in this way. cheers.
mhambra
A: 
mhambra