Browse Source

业务生成逻辑优化,sql报错不直接panic

路佳明 5 days ago
parent
commit
9b2a59eef2
1 changed files with 26 additions and 5 deletions
  1. 26 5
      cmd/consumer/biz/biz.go

+ 26 - 5
cmd/consumer/biz/biz.go

@@ -243,8 +243,20 @@ func getNodeAndEdge(root *BizTree) ([]*models.BizNode, []*models.BizEdge) {
 	return nodes, edges
 }
 
+const ERROR_SLEEP_TIME = 60 * time.Second
+const ERROR_MAX_TIMES = 10
+const GAP_TIME = 10 * time.Second
+
+var error_times int = 0
+
 func GenBiz2() {
 	for {
+		if error_times >= ERROR_MAX_TIMES {
+			log.Fatalf("错误次数超过%d次, 退出", ERROR_MAX_TIMES)
+		}
+		if error_times > 0 {
+			time.Sleep(ERROR_SLEEP_TIME)
+		}
 		sql := `WITH tt as (
 					SELECT
 						TraceId,
@@ -315,7 +327,9 @@ func GenBiz2() {
 		end := start + 600
 		err := chdb.Raw(sql, start, end).Scan(&list).Error
 		if err != nil {
-			panic(err)
+			log.Errorf("查询biz数据失败: %s", err)
+			error_times++
+			continue
 		}
 		bizNodeMap := map[string][]*BizNode{}
 		rootMap := map[string]*BizTree{}
@@ -383,7 +397,9 @@ func GenBiz2() {
 				DoUpdates: clause.AssignmentColumns([]string{"updated_at"}),
 			}).CreateInBatches(bizList, 100).Error
 			if err != nil {
-				panic(err)
+				log.Errorf("创建biz数据失败: %s", err)
+				error_times++
+				continue
 			}
 		}
 		if len(nodeMap) > 0 {
@@ -396,7 +412,9 @@ func GenBiz2() {
 				DoUpdates: clause.AssignmentColumns([]string{"updated_at"}),
 			}).CreateInBatches(bizNodeList, 100).Error
 			if err != nil {
-				panic(err)
+				log.Errorf("创建biz node数据失败: %s", err)
+				error_times++
+				continue
 			}
 		}
 		if len(edgeMap) > 0 {
@@ -409,11 +427,14 @@ func GenBiz2() {
 				DoUpdates: clause.AssignmentColumns([]string{"updated_at"}),
 			}).CreateInBatches(bizEdgeList, 100).Error
 			if err != nil {
-				panic(err)
+				log.Errorf("创建biz edge数据失败: %s", err)
+				error_times++
+				continue
 			}
 		}
 		log.Infof("生成业务数据完成:%s-%s", time.Unix(start, 0), time.Unix(end, 0))
-		time.Sleep(time.Second * 10)
+		error_times = 0 // 重置错误次数
+		time.Sleep(GAP_TIME)
 	}
 }