package stages import ( "gitea.anxinyun.cn/container/common_models" "log" "sync" ) type StageManager struct { in chan []*common_models.ProcessData Stages []Stage out chan []*common_models.ProcessData resultChan chan []*common_models.ProcessData // 下一个处理环节的输入通道 } func NewStageManager(resultChan chan []*common_models.ProcessData) *StageManager { return &StageManager{ resultChan: resultChan, } } func (the *StageManager) RunStages() { if len(the.Stages) == 0 { log.Panicf("Stages.len=%d 无有效处理流程", len(the.Stages)) } // 连接阶段 for i := 0; i < len(the.Stages); i++ { if i == 0 { the.Stages[i].In = the.in } else { the.Stages[i].In = the.Stages[i-1].Out } the.Stages[i].StageRun() // 将启动环节协程 } the.out = the.Stages[len(the.Stages)-1].Out log.Printf("********* stageManage.go the.out=%p", the.out) var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() for { select { case _, ok := <-the.out: if !ok { log.Printf("StageManager: out channel closed") return } } } }() wg.Wait() } func (the *StageManager) AddSource(source chan []*common_models.ProcessData) { log.Printf("stageManager add source=%p 通道数据:%d/%d", source, len(source), cap(source)) the.in = source } func (the *StageManager) GetOut() chan []*common_models.ProcessData { return the.out } func (the *StageManager) GetIn() chan []*common_models.ProcessData { return the.in } func (the *StageManager) AddOut(source chan []*common_models.ProcessData) { the.out = source } func (the *StageManager) AddStages(stages ...Stage) { the.Stages = append(the.Stages, stages...) }