github.com/songzhibin97/gkit@v1.2.13/distributed/task/workflow.go (about)

     1  package task
     2  
     3  // Chain 创建链式调用的任务
     4  type Chain struct {
     5  	Name  string
     6  	Tasks []*Signature
     7  }
     8  
     9  // Group 创建并行执行的任务组
    10  type Group struct {
    11  	Name    string
    12  	GroupID string
    13  	Tasks   []*Signature
    14  }
    15  
    16  // GroupCallback 具有回调任务的任务组
    17  type GroupCallback struct {
    18  	Name     string
    19  	Group    *Group
    20  	Callback *Signature
    21  }
    22  
    23  // GetTaskIDs 获取组任务的所有ID
    24  func (g *Group) GetTaskIDs() []string {
    25  	ids := make([]string, 0, len(g.Tasks))
    26  	for _, task := range g.Tasks {
    27  		ids = append(ids, task.ID)
    28  	}
    29  	return ids
    30  }
    31  
    32  // NewChain 创建链式调用任务
    33  func NewChain(name string, signatures ...*Signature) (*Chain, error) {
    34  	for i := len(signatures) - 1; i > 0; i-- {
    35  		if i > 0 {
    36  			signatures[i-1].CallbackOnSuccess = []*Signature{signatures[i]}
    37  		}
    38  	}
    39  	return &Chain{Name: name, Tasks: signatures}, nil
    40  }
    41  
    42  // NewGroup 创建并行执行的任务组
    43  func NewGroup(groupID string, name string, signatures ...*Signature) (*Group, error) {
    44  	ln := len(signatures)
    45  	for i := range signatures {
    46  		signatures[i].GroupID = groupID
    47  		signatures[i].GroupTaskCount = ln
    48  	}
    49  	return &Group{
    50  		GroupID: groupID,
    51  		Name:    name,
    52  		Tasks:   signatures,
    53  	}, nil
    54  }
    55  
    56  // NewGroupCallback 创建具有回调任务的个任务组
    57  func NewGroupCallback(group *Group, name string, callback *Signature) (*GroupCallback, error) {
    58  	for _, task := range group.Tasks {
    59  		task.CallbackChord = callback
    60  	}
    61  	return &GroupCallback{
    62  		Group:    group,
    63  		Name:     name,
    64  		Callback: callback,
    65  	}, nil
    66  }