php使用redis作为消息队列--守护进程模式执行

 

出队脚本

编写脚本,通过crontab定时执行脚本,从list的右边取出错误处理。

这种方式的优点是不会出现数据的丢失,只有定时脚本处理完相关信息之后,才从队列中取出;缺点也是显而易见的,响应不够及时,因为是定时访问redis的list队形,所以不能实时的处理队列中的消息。

$params = $redis->Rpop($key);

php实现

$redis = new Redis();

$redis->pconnect(RedisConfig::SERVERNAME, RedisConfig::PORT) or die("redis error connecting");

while(true){
    try {
    	$json = $this->redis->brPop('key', 0);//0表示无限阻塞等待
        //dosomething
    }
}

我们为了防止脚本意外终止(发生错误等情况)不再工作,我们写一个定时任务监控该脚本的执行情况。

monitor.sh

#!/bin/bash
alive=`ps aux|grep **|grep -v grep|wc -l`
if [ $alive -eq 0 ]
then
nohup /usr/local/php-7.1/bin/php /**/**.php >> /**/**.log 2>&1 &
fi

1分钟检查一次 

crontab -e
*/1 * * * * /var/www/monitor.sh

猜你喜欢

转载自blog.csdn.net/qq_16059847/article/details/83899146