github.com/hy3/cuto@v0.9.8-0.20160830082821-aa6652f877b7/master/jobnet/path.go (about)

     1  // Copyright 2015 unirita Inc.
     2  // Created 2015/04/10 honda
     3  
     4  package jobnet
     5  
     6  import "fmt"
     7  
     8  // 並列ジョブ実行の1経路を表す構造体。
     9  type Path struct {
    10  	Head Element
    11  	Goal Element
    12  	Err  error
    13  }
    14  
    15  // 新しい並列実行経路を生成する
    16  func NewPath(head Element) *Path {
    17  	p := new(Path)
    18  	p.Head = head
    19  	return p
    20  }
    21  
    22  // 並列実行経路を実行する。
    23  // 実行完了後、doneチャンネルに空のstruct{}リテラルを入力する。
    24  //
    25  // param : done 通知用チャネル。
    26  func (p *Path) Run(done chan<- struct{}) {
    27  	current := p.Head
    28  
    29  	for {
    30  		if current == nil {
    31  			p.Err = fmt.Errorf("Network is terminated in branch.")
    32  			break
    33  		}
    34  
    35  		if current.Type() == ELM_GW {
    36  			p.Goal = current
    37  			break
    38  		}
    39  
    40  		next, err := current.Execute()
    41  		if err != nil {
    42  			p.Err = err
    43  			break
    44  		}
    45  
    46  		current = next
    47  	}
    48  
    49  	done <- struct{}{}
    50  }