内存已满后,如何在服务器上运行beanstak管

时间:2014-02-05 15:27:37

标签: php beanstalkd

beanstalkd 中,我必须设置内存限制

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

}

退出时,如何再次运行?我正在通过 cli 脚本运行脚本。

我需要确保beanstalk worker一直在运行。我正在使用此工作程序从用户获取活动并插入其他成员提要

1 个答案:

答案 0 :(得分:2)

实际上很简单,具体取决于你如何启动PHP脚本。如果只是从命令行开始,你只想重新运行它直到你想要停止它,那么像下面这样的shell脚本就可以了。

bash脚本运行PHP文件,然后通过exit(NUMBER);从PHP获取响应。在此示例中,如果PHP执行exit(98);,则shell脚本将立即重新启动PHP。如果是其他内容,它会暂停一段时间,或完全停止shell脚本。如果除了它知道如何处理的值之外还有其他任何东西,它会等待一段时间,然后重新启动。

您可以安排使用Upstart(在Ubuntu上),inittab或其他处理过程控制的软件(如Supervisord)启动初始shell脚本。

#!/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 $@