-
Notifications
You must be signed in to change notification settings - Fork 31
/
task.go
60 lines (49 loc) · 1.33 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
package apps
import (
"encoding/json"
"strings"
)
type Task struct {
ID TaskID `json:"id"`
TaskStatus string `json:"taskStatus"`
AppID AppID `json:"appId"`
Host string `json:"host"`
Ports []int `json:"ports"`
HealthCheckResults []HealthCheckResult `json:"healthCheckResults"`
}
// Marathon Task ID
// Usually in the form of AppId.uuid with '/' replaced with '_'
type TaskID string
func (id TaskID) String() string {
return string(id)
}
func (id TaskID) AppID() AppID {
index := strings.LastIndex(id.String(), ".")
return AppID("/" + strings.Replace(id.String()[0:index], "_", "/", -1))
}
type HealthCheckResult struct {
Alive bool `json:"alive"`
}
type TasksResponse struct {
Tasks []*Task `json:"tasks"`
}
func ParseTasks(jsonBlob []byte) ([]*Task, error) {
tasks := &TasksResponse{}
err := json.Unmarshal(jsonBlob, tasks)
return tasks.Tasks, err
}
func ParseTask(event []byte) (*Task, error) {
task := &Task{}
err := json.Unmarshal(event, task)
return task, err
}
func (t *Task) IsHealthy() bool {
if len(t.HealthCheckResults) < 1 {
return false
}
register := true
for _, healthCheckResult := range t.HealthCheckResults {
register = register && healthCheckResult.Alive
}
return register
}