-
Notifications
You must be signed in to change notification settings - Fork 33
/
task.go
116 lines (105 loc) · 2.56 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package model
// Task represents a group of action
type Task struct {
*AbstractNode `yaml:",inline"` //abstract node
Actions []*Action ` yaml:",omitempty"` //actions
*TasksNode ` yaml:",inline"`
Fail bool ` yaml:",omitempty"` //controls if return fail status workflow on catch task
Template *Template ` yaml:",omitempty"`
//internal only for inline workflow meta data
multiAction bool //flag directing grouping actions (otherwise each action has its own task)
//publish data in parent workflow
data map[string]string
//these attribute if present dynamically load actions from subpath
tagRange string
subpath string
}
//func (t Task) MarshalYAML() (interface{}, error) {
// var result = make(map[string]interface{})
//
//
// return result, nil
//}
func (t *Task) Clone() *Task {
var result = *t
result.Actions = make([]*Action, len(t.Actions))
copy(result.Actions, t.Actions)
for i, item := range result.Actions {
result.Actions[i] = item.Clone()
}
result.TasksNode = t.TasksNode.Clone()
result.AbstractNode = t.AbstractNode.Clone()
return &result
}
func (t *Task) init() error {
if len(t.Actions) == 0 {
t.Actions = []*Action{}
}
for _, action := range t.Actions {
if t.Logging != nil && action.Logging == nil {
action.Logging = t.Logging
}
if err := action.Init(); err != nil {
return err
}
}
if t.TasksNode == nil {
return nil
}
if len(t.Tasks) > 0 {
for _, task := range t.Tasks {
if t.Logging != nil && task.Logging == nil {
task.Logging = t.Logging
}
if err := task.init(); err != nil {
return err
}
}
}
return nil
}
// HasTagID checks if task has supplied tagIDs
func (t *Task) HasTagID(tagIDs map[string]bool) bool {
if tagIDs == nil {
return false
}
for _, action := range t.Actions {
if tagIDs[action.TagID] {
return true
}
}
return false
}
// AsyncActions returns async actions
func (t *Task) AsyncActions() []*Action {
var result = make([]*Action, 0)
for _, candidate := range t.Actions {
if candidate.Async {
if candidate.Repeat > 1 {
repeat := candidate.Repeat
action := candidate.Clone()
_ = action.Init()
action.Repeat = 1
for i := 0; i < repeat; i++ {
result = append(result, action)
}
} else {
result = append(result, candidate)
}
}
}
return result
}
// NewTask creates a new task
func NewTask(name string, multiAction bool) *Task {
return &Task{
AbstractNode: &AbstractNode{
Name: name,
},
Actions: make([]*Action, 0),
multiAction: multiAction,
TasksNode: &TasksNode{
Tasks: make([]*Task, 0),
},
}
}