forked from HuaweiTech/lattice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
150 lines (119 loc) · 3.71 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package models
import (
"encoding/json"
"net/url"
"regexp"
)
type TaskState int
const (
TaskStateInvalid TaskState = iota
TaskStatePending
TaskStateRunning
TaskStateCompleted
TaskStateResolving
)
const maximumAnnotationLength = 10 * 1024
var taskGuidPattern = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
type Task struct {
TaskGuid string `json:"task_guid"`
Domain string `json:"domain"`
RootFSPath string `json:"rootfs"`
Stack string `json:"stack"`
EnvironmentVariables []EnvironmentVariable `json:"env,omitempty"`
Action Action `json:"-"`
MemoryMB int `json:"memory_mb"`
Privileged bool `json:"privileged"`
DiskMB int `json:"disk_mb"`
CPUWeight uint `json:"cpu_weight"`
LogGuid string `json:"log_guid"`
LogSource string `json:"log_source"`
MetricsGuid string `json:"metrics_guid"`
CreatedAt int64 `json:"created_at"` // the number of nanoseconds elapsed since January 1, 1970 UTC
UpdatedAt int64 `json:"updated_at"`
FirstCompletedAt int64 `json:"first_completed_at"`
ResultFile string `json:"result_file"`
State TaskState `json:"state"`
CellID string `json:"cell_id"`
Result string `json:"result"`
Failed bool `json:"failed"`
FailureReason string `json:"failure_reason"`
CompletionCallbackURL *url.URL `json:"completion_callback_url,omitempty"`
Annotation string `json:"annotation,omitempty"`
EgressRules []SecurityGroupRule `json:"egress_rules,omitempty"`
}
type InnerTask Task
type mTask struct {
ActionRaw *json.RawMessage `json:"action,omitempty"`
*InnerTask
}
type StagingTaskAnnotation struct {
AppId string `json:"app_id"`
TaskId string `json:"task_id"`
}
func (t *Task) UnmarshalJSON(payload []byte) error {
mtask := mTask{InnerTask: (*InnerTask)(t)}
err := json.Unmarshal(payload, &mtask)
if err != nil {
return err
}
var a Action
if mtask.ActionRaw == nil {
a = nil
} else {
a, err = UnmarshalAction(*mtask.ActionRaw)
if err != nil {
return err
}
}
t.Action = a
return nil
}
func (task Task) Validate() error {
var validationError ValidationError
if task.Domain == "" {
validationError = validationError.Append(ErrInvalidField{"domain"})
}
if !taskGuidPattern.MatchString(task.TaskGuid) {
validationError = validationError.Append(ErrInvalidField{"task_guid"})
}
if task.Stack == "" {
validationError = validationError.Append(ErrInvalidField{"stack"})
}
if task.Action == nil {
validationError = validationError.Append(ErrInvalidActionType)
} else {
err := task.Action.Validate()
if err != nil {
validationError = validationError.Append(err)
}
}
if task.CPUWeight > 100 {
validationError = validationError.Append(ErrInvalidField{"cpu_weight"})
}
if len(task.Annotation) > maximumAnnotationLength {
validationError = validationError.Append(ErrInvalidField{"annotation"})
}
for _, rule := range task.EgressRules {
err := rule.Validate()
if err != nil {
validationError = validationError.Append(ErrInvalidField{"egress_rules"})
}
}
if !validationError.Empty() {
return validationError
}
return nil
}
func (task Task) MarshalJSON() ([]byte, error) {
actionRaw, err := MarshalAction(task.Action)
if err != nil {
return nil, err
}
rm := json.RawMessage(actionRaw)
innerTask := InnerTask(task)
mtask := &mTask{
ActionRaw: &rm,
InnerTask: &innerTask,
}
return json.Marshal(mtask)
}