Question

In beanstalkd I have to set a limit for memory

if ($memory > 1073741824) { // 67108864 bytes = 64 MB
                    $this->log('exiting run due to memory limit');
                    exit;

}

When its exit , how can to run it again ? I am running the script through cli script.

I need to make sure that beanstalk worker is running all the time. I am using this worker to get activities from user and insert into other members feed

Was it helpful?

Solution

It's pretty easy actually, depending a little on how you start the PHP script. If it's just from the command line and you want to just re-run it till you want to stop it, then a shell script like the below will pretty much do it.

The bash script runs the PHP file, and then gets a response from the PHP via the exit(NUMBER); In this example if the PHP does an exit(98);, the shell script will immediately restart the PHP. If its something else, it will pause for a while, or stop the shell script entirely. If it got anything other than the values it knows how to handle, it waits for a while again, and then restarts.

You can arrange to start the initial shell script itself with Upstart (on Ubuntu), inittab, or another piece of software that handles process control like Supervisord.

#!/bin/bash

# runBeanstalkd-worker.sh, from 
#  http://phpscaling.com/2009/06/23/doing-the-work-elsewhere-sidebar-running-the-worker/

# a shell script that keeps looping until an exit code is given
# if it does an exit(0), restart after a second - or if it's a declared error
# if we've restarted in a planned fashion, we don't bother with any pause
# and for one particular code, exit the script entirely.
# The numbers 97, 98, 99 must match what is returned from the PHP script

nice php -q -f ./cli-beanstalk-worker.php -- $@
ERR=$?

## Possibilities
# 97    - planned pause/restart
# 98    - planned restart
# 99    - planned stop, exit.
# 0     - unplanned restart (as returned by "exit;")
#        - Anything else is also unplanned paused/restart

if [ $ERR -eq 97 ]
then
   # a planned pause, then restart
   echo "97: PLANNED_PAUSE - wait 1";
   sleep 1;
   exec $0 $@;
fi

if [ $ERR -eq 98 ]
then
   # a planned restart - instantly
   echo "98: PLANNED_RESTART";
   exec $0 $@;
fi

if [ $ERR -eq 99 ]
then
   # planned complete exit
   echo "99: PLANNED_SHUTDOWN";
   exit 0;
fi

# unplanned exit, pause, and restart
echo "unplanned restart: err:" $ERR;
echo "sleeping for 1 sec"
sleep 1

exec $0 $@
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top