部分代码
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');
}