Commit bd1dd5bb authored by Liu lu's avatar Liu lu
Browse files

日志批处理

parent 43fdf66e
Loading
Loading
Loading
Loading
+7 −3
Original line number Original line Diff line number Diff line
@@ -36,8 +36,6 @@ class LogTraceHandler
                //流程3 抛出一个异常
                //流程3 抛出一个异常
                throw new Exception('test111');
                throw new Exception('test111');


                //流程执行完成标记结束
                LogTraceHandler::markComplete();


            }catch (\Throwable $exception){
            }catch (\Throwable $exception){
                //记录异常日志
                //记录异常日志
@@ -51,13 +49,19 @@ class LogTraceHandler
    public static function createLogTrace($source, $params)
    public static function createLogTrace($source, $params)
    {
    {
        if(!Coroutine::inCoroutine()) return;
        if(!Coroutine::inCoroutine()) return;
        $userName = '';
        try {
            $userName = \Meibuyu\Micro\Model\Auth::user()['name'];
        }catch (\Throwable $exception){


        }
        LogTrace::insertOrIgnore([
        LogTrace::insertOrIgnore([
            'request_id'            => self::getRequestId(),
            'request_id'            => self::getRequestId(),
            'origin_params' => json_encode($params),
            'origin_params' => json_encode($params),
            'source'        => $source,
            'source'        => $source,
            'created_at'    => now(),
            'created_at'    => now(),
            'process_info'  => ''
            'process_info'  => '',
            'user_name' => $userName
        ]);
        ]);
    }
    }


+4 −0
Original line number Original line Diff line number Diff line
@@ -38,6 +38,10 @@ class LogTraceQueue extends RedisQueueBatchHandler
              UNIQUE KEY `request_id` (`request_id`)
              UNIQUE KEY `request_id` (`request_id`)
            ) ENGINE=InnoDB AUTO_INCREMENT=5950 DEFAULT CHARSET=utf8mb4;
            ) 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`;
       ");
    }
    }


    /**
    /**
+32 −29
Original line number Original line Diff line number Diff line
@@ -20,7 +20,9 @@ abstract class RedisQueueBatchHandler


    const MAX_RETRY_TIMES = 3;
    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;
    const ERROR_RETRY_CODE = 9000;


@@ -44,13 +46,12 @@ abstract class RedisQueueBatchHandler
    }
    }


    /**
    /**
     * 将执行失败的数据重回队列
     * 将执行失败的数据重回队列 尾部 不影响其他数据执行
     * @param $arr
     * @param $arr
     */
     */
    protected function backToQueue($arr)
    protected function backToQueue($arr)
    {
    {
        array_unshift($arr,$this->queue_name);
        $this->redis->rPush($this->queue_name,...$arr);
        call_user_func_array([$this->redis,'lPush'],$arr);
    }
    }


    //批处理具体逻辑
    //批处理具体逻辑
@@ -70,40 +71,42 @@ abstract class RedisQueueBatchHandler
            $this->redis->lPush($this->queue_name,$exist[1]);
            $this->redis->lPush($this->queue_name,$exist[1]);


            //每次从列表取100
            //每次从列表取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){
            $formatArr = array_map(function ($item){
                return json_decode($item,true);
                return json_decode($item,true);
            },$arr);
            },$arr);
            try {
                //具体批处理逻辑
                $this->batchDeal($formatArr);
            //取完 从队列删掉
            //取完 从队列删掉
            $this->redis->lTrim($this->queue_name,count($arr),-1);
            $this->redis->lTrim($this->queue_name,count($arr),-1);
            }catch (\Throwable $exception){


                //错误码为100 重新推到队列
            if(!$this->tryTreeTimesBatchDeal($formatArr)){
                if($exception->getCode()==self::ERROR_RETRY_CODE){
                 $this->backToQueue($arr);
                    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);
        }
        }


    }
    }


    /**
     * 重试三次批处理 每次延迟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);
        }
        }

    }
    }


    /**
    /**