Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have a PHP script run forever, access a queue

Tags:

php

amazon-sqs

See also 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.)

like image 289
PeterV Avatar asked Sep 21 '10 13:09

PeterV


People also ask

How do I make PHP scripts run forever?

For allowing to run the script forever and ignore user aborts, set PHP inbuilt function ignore_user_abort(true). By default, it set to False which throws fatal error when client aborts to stop the script.

Can a PHP script run forever?

Whenever a PHP application rebuilds MySQL indexes, the process may run for a long time. Generally, allowing a PHP script to run forever is not desirable.

How to run PHP script every 5 minutes?

try this solution: <? php $interval=5; //minutes set_time_limit(0); while (true) { $now=time(); include("alert_exe.


2 Answers

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.

like image 173
Sam Day Avatar answered Oct 08 '22 21:10

Sam Day


While PHP can access (publish and consume) MQ's, if at all possible try to use a fully functional MQ application to do this.

A fully functional MQ application (in ruby, perl, .NET, java etc) will handle all of the concurrency, error logging, state management and scalability issues that you discuss.

like image 29
spoutingshite Avatar answered Oct 08 '22 21:10

spoutingshite