-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtask.go
74 lines (61 loc) · 1.91 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
package digdag
import (
"fmt"
"net/http"
"strings"
)
type tasksWrapper struct {
Tasks []*Task `json:"tasks"`
}
// Task is struct for attempts task result
type Task struct {
ID string `json:"id"`
FullName string `json:"fullName"`
ParentID interface{} `json:"parentId"`
Config map[string]interface{} `json:"config"`
Upstreams []string `json:"upstreams"`
State string `json:"state"`
ExportParams map[string]interface{} `json:"exportParams"`
StoreParams map[string]interface{} `json:"storeParams"`
StateParams map[string]interface{} `json:"stateParams"`
UpdatedAt string `json:"updatedAt"`
RetryAt interface{} `json:"retryAt"`
StartedAt interface{} `json:"startedAt"`
IsGroup bool `json:"isGroup"`
}
// GetTasks to get tasks list
func (c *Client) GetTasks(attemptID string) ([]*Task, error) {
spath := fmt.Sprintf("/api/attempts/%s/tasks", attemptID)
var tw *tasksWrapper
resp, err := c.NewRequest(http.MethodGet, spath, nil)
if err != nil {
return nil, err
}
if err := decodeBody(resp, &tw); err != nil {
return nil, err
}
return tw.Tasks, err
}
// GetTaskResult to return task result
func (c *Client) GetTaskResult(attemptIDs []string, taskName string) (*Task, error) {
// Check the taskName has prefix `+`
if !strings.HasPrefix(taskName, "+") {
return nil, fmt.Errorf("task `%s` is invalid task name", taskName)
}
for _, attemptID := range attemptIDs {
tasks, err := c.GetTasks(attemptID)
if err != nil {
return nil, err
}
for k := range tasks {
if tasks[k].FullName == taskName {
state := tasks[k].State
if state == "success" {
return tasks[k], nil
}
return nil, fmt.Errorf("task `%s` state is %s", taskName, state)
}
}
}
return nil, fmt.Errorf("task `%s` result not found", taskName)
}