/
tasks_endpoint.go
181 lines (150 loc) · 4.62 KB
/
tasks_endpoint.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package openstate
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/walkergriggs/openstate/api"
"github.com/walkergriggs/openstate/openstate/structs"
)
// tasksRequest routes a request to the various functions which apply to all tasks.
func (s *HTTPServer) tasksRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
switch req.Method {
case "GET":
return s.tasksList(resp, req)
case "POST", "PUT":
return s.tasksUpdate(resp, req)
default:
return nil, fmt.Errorf("ErrInvalidMethod")
}
}
// taskSpecificRequest routes a request to various functions which apply to an
// individual task definition.
func (s *HTTPServer) taskSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
path := strings.TrimPrefix(req.URL.Path, "/v1/task/")
switch {
case strings.HasSuffix(path, "/run"):
taskName := strings.TrimSuffix(path, "/run")
return s.taskRun(resp, req, taskName)
case strings.HasSuffix(path, "/ps"):
taskName := strings.TrimSuffix(path, "/ps")
return s.taskPs(resp, req, taskName)
default:
return nil, fmt.Errorf("No such endpoint")
// return s.jobCRUD(resp, req, path)
}
}
// tasksList returns the list of tasks from the server's FSM.
// TODO return a list of metadata, not just the total count of tasks.
// TODO allow stale reads from the follower, so the request isn't forwarded to
// the leader.
func (s *HTTPServer) tasksList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if ok, err := s.forward(resp, req); ok {
return nil, err
}
defs, err := s.server.fsm.state.GetDefinitions()
if err != nil {
return nil, err
}
res := &structs.TaskListResponse{
Definitions: defs,
}
return res, nil
}
// taskUpdate applies a task definition request to the raft logs which either
// adds a new or updates and existing task.
func (s *HTTPServer) tasksUpdate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if ok, err := s.forward(resp, req); ok {
return nil, err
}
// Decode and repackage
var out structs.TaskDefineRequest
dec := json.NewDecoder(req.Body)
if err := dec.Decode(&out); err != nil {
return nil, err
}
fsmErr, _, err := s.server.raftApply(structs.TaskDefineRequestType, out)
if err, ok := fsmErr.(error); ok && err != nil {
s.logger.Error("Failed to update FSM", "error", err, "fsm", true)
return nil, err
}
if err != nil {
s.logger.Error("Failed to update FSM", "error", err, "raft", true)
return nil, err
}
// TODO return more than the requested definition
res := structs.TaskDefineResponse{
Definition: out.Definition,
}
return res, nil
}
// taskRun initializes a new task instance given a task definition and applies
// the instance to the raft logs.
func (s *HTTPServer) taskRun(resp http.ResponseWriter, req *http.Request, defName string) (interface{}, error) {
if ok, err := s.forward(resp, req); ok {
return nil, err
}
// Check to see if definition exists
def, err := s.server.fsm.state.GetDefinitionByName(defName)
if err != nil {
return nil, err
}
if def == nil {
return nil, fmt.Errorf("No task definition with name %s", defName)
}
// Convert from api.FSM to fsm.FSM
fsm, err := api.Ftof(def.FSM)
if err != nil {
return nil, err
}
instance := &structs.Instance{
ID: fmt.Sprintf("%s-%s", def.Name, generateUUID()),
Definition: def,
FSM: fsm,
}
args := &structs.TaskRunRequest{
Instance: instance,
}
fsmErr, _, err := s.server.raftApply(structs.TaskRunRequestType, args)
if err, ok := fsmErr.(error); ok && err != nil {
s.logger.Error("Failed to update FSM", "error", err, "fsm", true)
return nil, err
}
if err != nil {
s.logger.Error("Failed to update FSM", "error", err, "raft", true)
return nil, err
}
res := structs.TaskRunResponse{
Instance: instance,
}
return res, nil
}
// taskPs returns the list of running task instances for a given task definition
// from the server's FSM.
func (s *HTTPServer) taskPs(resp http.ResponseWriter, req *http.Request, defName string) (interface{}, error) {
if ok, err := s.forward(resp, req); ok {
return nil, err
}
// Check to see if definition exists
def, err := s.server.fsm.state.GetDefinitionByName(defName)
if err != nil {
return nil, err
}
if def == nil {
return nil, fmt.Errorf("No task definition with name %s", defName)
}
allInstances, err := s.server.fsm.state.GetInstances()
if err != nil {
return nil, err
}
instances := make([]*structs.Instance, 0)
for _, instance := range allInstances {
if strings.HasPrefix(instance.ID, defName) {
instances = append(instances, instance)
}
}
res := &structs.TaskPsResponse{
Instances: instances,
}
return res, nil
}