Эх сурвалжийг харах

wip:查询暂停及查询时间超时休眠提交

wangkaiyue 2 жил өмнө
parent
commit
8f6299ea78

+ 10 - 2
config.json

@@ -7,9 +7,17 @@
     "dailyTimes": 1000
   },
   "custom": {
-    "prop": 1,
+    "open": true,
     "searchPool": 1,
-    "updateCron": "0 0 0 ? * 0"
+    "updateCron": "0 0 0 ? * 0",
+    "searchLimit": {
+      "switch": {
+        "stop": "0 0 8 ? * *",
+        "start": "0 0 14 ? * *"
+      },
+      "timeOver": 10,
+      "waitTime": 30
+    }
   },
   "testUid": [
     "61f3a3c746af8f8a5c513175",

+ 41 - 8
entity/mananger/customManager.go

@@ -23,9 +23,10 @@ const (
 
 // CustomManager 定制化报告管理
 type CustomManager struct {
-	Conf      vars.CustomConfig
-	UserGroup map[string]int // 月活用户
-	BatchFlag string         // 批次标识
+	Conf         vars.CustomConfig
+	UserGroup    map[string]int // 月活用户
+	BatchFlag    string         // 批次标识
+	IsSearchTime bool           // 是否是运行时间段
 	sync.RWMutex
 }
 
@@ -45,10 +46,8 @@ func InitCustomManager(conf vars.CustomConfig) *CustomManager {
 		Conf:      conf,
 		UserGroup: make(map[string]int),
 	}
-	//圈用户
+	//定时任务
 	go manager.ScheduledTasks()
-	//执行查询
-	go manager.DoSearch()
 	return manager
 }
 
@@ -83,6 +82,7 @@ func (this *CustomManager) GetData(userId, keyWords string, isNew bool) map[stri
 
 // ScheduledTasks 定时任务
 func (this *CustomManager) ScheduledTasks() {
+	// 定时圈用户
 	if this.Conf.UpdateCron != "" {
 		c := cron.New(cron.WithSeconds())
 		// 给对象增加定时任务
@@ -91,15 +91,38 @@ func (this *CustomManager) ScheduledTasks() {
 		}
 		c.Run()
 	}
+	// 查询时间段限时
+	if this.Conf.SearchLimit.Switch.Start != "" && this.Conf.SearchLimit.Switch.Stop != "" {
+		//开始
+		startJob := cron.New(cron.WithSeconds())
+		if _, err := startJob.AddFunc(this.Conf.UpdateCron, func() {
+			this.IsSearchTime = true
+			go this.DoSearch()
+		}); err != nil {
+			panic(err)
+		}
+		startJob.Run()
+		//结束
+		endJob := cron.New(cron.WithSeconds())
+		if _, err := endJob.AddFunc(this.Conf.UpdateCron, func() {
+			this.IsSearchTime = false
+		}); err != nil {
+			panic(err)
+		}
+		endJob.Run()
+	} else {
+		go this.DoSearch()
+	}
 	//首次运行圈选用户
 	this.UpdateUserGroupJob()
 }
 
 // UpdateUserGroupJob 更新用户群组
 func (this *CustomManager) UpdateUserGroupJob() {
-	if this.Conf.Prop <= 0 {
+	if !this.Conf.Open {
 		return
 	}
+
 	log.Printf("[MANAGER-INFO]CustomManager UserGroup Change Start\n")
 	//更新批次标识
 	this.BatchFlag = public.GetWeekBatchName(time.Now())
@@ -186,9 +209,14 @@ func (this *CustomManager) activityUserQueue(batchFlag string, userIds []string)
 
 // DoSearch 定制化分析报告查询队列
 func (this *CustomManager) DoSearch() {
+	log.Printf("[MANAGER-INFO]CustomManager DoSearch Start\n")
 	for {
 		//是否在可执行时间段内
-		//???
+		if this.IsSearchTime {
+			log.Printf("[MANAGER-INFO]CustomManager DoSearch End\n")
+			return
+		}
+
 		var obj *SearchEntity
 		select { //优先级 newRegisterUserQueue > activityUserQueue
 		case obj = <-newRegisterUserQueue:
@@ -204,7 +232,12 @@ func (this *CustomManager) DoSearch() {
 			continue
 		}
 		//查询结果处理
+		searchStart := time.Now()
 		data := search.PotentialCustomizeAnalysis(obj.UserId, obj.Value)
+		//查询超时,则休息一下
+		if time.Now().Sub(searchStart).Seconds() > float64(this.Conf.SearchLimit.TimeOver) {
+			time.Sleep(time.Second * time.Duration(this.Conf.SearchLimit.WaitTime))
+		}
 		if data == nil || len(data) == 0 {
 			log.Printf("[MANAGER-ERR]CustomManager %s DoSearch %s Is Empty\n", obj.UserId, obj.Value)
 			continue

+ 11 - 3
vars/config.go

@@ -21,9 +21,17 @@ type AheadConfig struct {
 }
 
 type CustomConfig struct {
-	Prop       float64 `json:"prop"`       //月活用户百分比
-	SearchPool int     `json:"searchPool"` //检索并发池
-	UpdateCron string  `json:"updateCron"` //更新周活用户
+	Open        bool   `json:"open"`       //是否运行查询
+	SearchPool  int    `json:"searchPool"` //检索并发池
+	UpdateCron  string `json:"updateCron"` //更新周活用户
+	SearchLimit struct {
+		Switch struct {
+			Stop  string `json:"stop"`
+			Start string `json:"start"`
+		} `json:"switch"`
+		TimeOver int `json:"timeOver"` //
+		WaitTime int `json:"waitTime"`
+	} `json:"searchLimit"`
 }
 
 func init() {