fix(region): wait all goroutine finished when execute scheduled_tasks

This commit is contained in:
rainzm
2021-10-14 16:00:27 +08:00
parent 1332c7de4f
commit de397db1bc
+7 -3
View File
@@ -574,9 +574,6 @@ func (stm *SScheduledTaskManager) timeScope(median time.Time, interval time.Dura
var timerQueue = make(chan struct{}, cop.Options.ScheduledTaskQueueSize)
func (stm *SScheduledTaskManager) Timer(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
if len(timerQueue) == 0 {
timerQueue = make(chan struct{}, cop.Options.ScheduledTaskQueueSize)
}
// 60 is for fault tolerance
interval := 60 + 30
timeScope := stm.timeScope(time.Now(), time.Duration(interval)*time.Second)
@@ -588,12 +585,16 @@ func (stm *SScheduledTaskManager) Timer(ctx context.Context, userCred mcclient.T
return
}
log.Debugf("timeScope: start: %s, end: %s", timeScope.Start, timeScope.End)
waitQueue := make(chan struct{}, len(sts))
for i := range sts {
log.Infof("sts[%d]: %s", i, jsonutils.Marshal(sts[i]))
st := sts[i]
timerQueue <- struct{}{}
waitQueue <- struct{}{}
go func(ctx context.Context) {
defer func() {
<-timerQueue
<-waitQueue
}()
if st.NextTime.Before(timeScope.Start) {
// For unknown reasons, the scalingTimer did not execute at the specified time
@@ -619,6 +620,9 @@ func (stm *SScheduledTaskManager) Timer(ctx context.Context, userCred mcclient.T
}(ctx)
}
// wait all finish
for i := 0; i < len(sts); i++ {
waitQueue <- struct{}{}
}
}
func init() {