Commit bd1dd5bb authored by Liu lu's avatar Liu lu

日志批处理

parent 43fdf66e
......@@ -36,8 +36,6 @@ class LogTraceHandler
//流程3 抛出一个异常
throw new Exception('test111');
//流程执行完成标记结束
LogTraceHandler::markComplete();
}catch (\Throwable $exception){
//记录异常日志
......@@ -51,13 +49,19 @@ class LogTraceHandler
public static function createLogTrace($source, $params)
{
if(!Coroutine::inCoroutine()) return;
$userName = '';
try {
$userName = \Meibuyu\Micro\Model\Auth::user()['name'];
}catch (\Throwable $exception){
}
LogTrace::insertOrIgnore([
'request_id' => self::getRequestId(),
'origin_params' => json_encode($params),
'source' => $source,
'created_at' => now(),
'process_info' => ''
'process_info' => '',
'user_name' => $userName
]);
}
......
......@@ -38,6 +38,10 @@ class LogTraceQueue extends RedisQueueBatchHandler
UNIQUE KEY `request_id` (`request_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5950 DEFAULT CHARSET=utf8mb4;
");
Db::update("
ALTER TABLE `trace_logs`
ADD COLUMN `user_name` varchar(6) NOT NULL DEFAULT '' COMMENT '用户名' AFTER `process_info`;
");
}
/**
......
......@@ -20,7 +20,9 @@ abstract class RedisQueueBatchHandler
const MAX_RETRY_TIMES = 3;
const BATCH_DEAL_NUM = 36;
// const BATCH_DEAL_NUM = 36;
protected $batch_deal_num = 8;
const ERROR_RETRY_CODE = 9000;
......@@ -44,13 +46,12 @@ abstract class RedisQueueBatchHandler
}
/**
* 将执行失败的数据重回队列
* 将执行失败的数据重回队列 尾部 不影响其他数据执行
* @param $arr
*/
protected function backToQueue($arr)
{
array_unshift($arr,$this->queue_name);
call_user_func_array([$this->redis,'lPush'],$arr);
$this->redis->rPush($this->queue_name,...$arr);
}
//批处理具体逻辑
......@@ -70,42 +71,44 @@ abstract class RedisQueueBatchHandler
$this->redis->lPush($this->queue_name,$exist[1]);
//每次从列表取100
$arr = $this->redis->lRange($this->queue_name,0,self::BATCH_DEAL_NUM-1);
$arr = $this->redis->lRange($this->queue_name,0,$this->batch_deal_num-1);
//数据格式化
$formatArr = array_map(function ($item){
return json_decode($item,true);
},$arr);
try {
//具体批处理逻辑
$this->batchDeal($formatArr);
//取完 从队列删掉
$this->redis->lTrim($this->queue_name,count($arr),-1);
}catch (\Throwable $exception){
//错误码为100 重新推到队列
if($exception->getCode()==self::ERROR_RETRY_CODE){
if($this->retry<self::MAX_RETRY_TIMES){ //重试次数不超过3次
//$this->backToQueue($arr);
$this->retry++;
}else{
$this->redis->lTrim($this->queue_name,count($arr),-1);
$this->retry = 0; //重置当前次数
$this->errorWriteToFile($formatArr,$exception);
}
}else{
$this->redis->lTrim($this->queue_name,count($arr),-1);
$this->retry = 0;
$this->errorWriteToFile($formatArr,$exception);
}
//取完 从队列删掉
$this->redis->lTrim($this->queue_name,count($arr),-1);
if(!$this->tryTreeTimesBatchDeal($formatArr)){
$this->backToQueue($arr);
}
}
}
/**
* 重试三次批处理 每次延迟6秒执行
* @param $formatArr
* @param int $retry
* @author Liu lu
* date 2023-05-10
*/
private function tryTreeTimesBatchDeal($formatArr,$retry=0)
{
if($retry>=3) return false;
try {
//具体批处理逻辑
$this->batchDeal($formatArr);
return true;
}catch (\Throwable $exception){
$retry++;
sleep(6);
$this->errorWriteToFile($formatArr,$exception);
return $this->tryTreeTimesBatchDeal($formatArr,$retry);
}
}
/**
* @param $data
* @param \Throwable $exception
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment