command 消息队列处理实例

部分代码

public function groupCreate()
    {
        set_time_limit(0);
        $queueName  = 'create-chat-group';
        $sleepTime  = 2;
        while(true){
            if(memory_get_usage()>100*1024*1024){
                exit(0);//大于100M内存退出程序,防止内存泄漏被系统杀死导致任务终端
            }
            //批量接收消息,1次接收3条,经试验不宜过大,容易造成网络端口堵塞
            $dataList       = $this->mnsServer->batchGet($queueName, $num=3, $waitSeconds=0);
            if(empty($dataList)){
                sleep($sleepTime);
                continue;
            }
            //循环处理
            $jobIdArr       = array();
            $reSendJobArr   = array();
            $failNum        = 0;
            $dataList       = (array)$dataList;
            foreach($dataList as $jobData){
                $jobId      = $jobData['id'];
                //不管成功还是失败全部删除
                empty($jobId) || $jobIdArr[] = $jobId;
                $dealData   = json_decode($jobData['msg'], true);
                if(empty($dealData)){
                    continue;
                }
                if(empty($dealData['circle_id'])){
                    file_put_contents('/mnt/logs/queue/createGroup/'.date('Y_m_d', time()).'.txt', '本次处理数据:'.print_r($dealData,1).'异常,时间:'.date('Y-m-d H:i:s')."\n", FILE_APPEND);
                    continue;
                }
                //
                try{
                    $result = $this->groupServer->groupCreate($dealData['circle_name'], $dealData['circle_id'], $dealData['user_id']);
                }catch (Exception $e){
                    $result = false;
                }

                //如果失败达到指定次数,则要删除,并且记录日志
                if($result){
                    continue;
                }

                //如果失败达到指定次数,则要删除,并且记录日志
                $dealData['failNum']   = (int)$dealData['failNum'];
                if($dealData['failNum']<5){
                    $dealData['failNum']++;
                    $reSendJobArr[]      = json_encode($dealData);
                    $failNum++;
                }else{
                    $path = '/mnt/logs/queue/createGroup/'.date('Y-m-d').'/'.date('YmdHis').'-'.$jobId.'.txt';
                    file_put_contents($path, json_encode($dealData));
                }
            }

            //批量删除成功的
            empty($jobIdArr) || $this->mnsServer->batchDelete($queueName, $jobIdArr);
            //批量推送需要重新推送的
            //$failNum && var_dump($failNum,$reSendJobArr);
            ($failNum>0) && $this->mnsServer->batchSend($queueName, $reSendJobArr);
            //写入日志
            //file_put_contents('cache/res.txt', '本次处理数据:'.count($jobIdArr).'个,时间:'.date('Y-m-d H:i:s')."\n", FILE_APPEND);
            //action清理变量,model中的变量及时清理
            $dealData = null;
            $jobId    = null;
            $dataList = null;
            $result   = null;
            $jobIdArr = null;
            $path     = null;
            $failNum  = 0;
            $reSendJobArr = null;
            $jobData  = null;
            //sleep 3秒
            sleep($sleepTime);
        }
        exit('ok');
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值