|
|
@ -12,6 +12,7 @@ type Stage struct { |
|
|
|
In <-chan *common_models.ProcessData |
|
|
|
processFuncs []func(*common_models.ProcessData) *common_models.ProcessData |
|
|
|
Out chan *common_models.ProcessData |
|
|
|
execOver chan bool //阶段执行完毕,用以排查超时
|
|
|
|
} |
|
|
|
|
|
|
|
func NewStage(name string) *Stage { |
|
|
@ -20,6 +21,7 @@ func NewStage(name string) *Stage { |
|
|
|
processFuncs: make([]func(*common_models.ProcessData) *common_models.ProcessData, 0), |
|
|
|
In: make(<-chan *common_models.ProcessData, 1), |
|
|
|
Out: make(chan *common_models.ProcessData, 1), |
|
|
|
execOver: make(chan bool, 1), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -45,18 +47,28 @@ func (s *Stage) AddProcess(fun func(*common_models.ProcessData) *common_models.P |
|
|
|
} |
|
|
|
|
|
|
|
func (s *Stage) process(data *common_models.ProcessData) *common_models.ProcessData { |
|
|
|
go s.handlerTimeOutCheck(s.Name, data.DeviceData.DeviceId) |
|
|
|
for _, processFunc := range s.processFuncs { |
|
|
|
//tag := fmt.Sprintf("%d/%d", i+1, len(s.processFuncs))
|
|
|
|
//log.Printf("stage[%s][%s]流程处理 start=> %s", s.Name, tag, data.DeviceData.Name)
|
|
|
|
//log.Printf("stage[%s]流程处理 start=> %s", s.Name, data.DeviceData.Name)
|
|
|
|
func() { |
|
|
|
defer timeCost(s.Name, data.DeviceData.DeviceId, time.Now()) |
|
|
|
data = processFunc(data) |
|
|
|
}() |
|
|
|
//log.Printf("stage[%s]流程处理 over=> %s", s.Name, data.DeviceData.Name)
|
|
|
|
} |
|
|
|
|
|
|
|
s.execOver <- true |
|
|
|
return data |
|
|
|
} |
|
|
|
func (s *Stage) handlerTimeOutCheck(stageName, deviceId string) { |
|
|
|
defaultTimeout := 10 * time.Second |
|
|
|
select { |
|
|
|
case <-s.execOver: |
|
|
|
case <-time.After(defaultTimeout): |
|
|
|
log.Printf("=====================") |
|
|
|
log.Panicf("stage[%s] ->[%s] 流程处理,超时[%v],请排查", stageName, deviceId, defaultTimeout) |
|
|
|
} |
|
|
|
} |
|
|
|
func timeCost(nodeId, deviceId string, start time.Time) { |
|
|
|
tc := time.Since(start) |
|
|
|
log.Printf("stage[%s] ->[%s] 耗时 = %v", nodeId, deviceId, tc) |
|
|
|