This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_client_request.go
165 lines (125 loc) · 3.85 KB
/
task_client_request.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
package director
import (
"fmt"
"net/http"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type TaskClientRequest struct {
clientRequest ClientRequest
taskReporter TaskReporter
taskCheckStepDuration time.Duration
}
func NewTaskClientRequest(
clientRequest ClientRequest,
taskReporter TaskReporter,
taskCheckStepDuration time.Duration,
) TaskClientRequest {
return TaskClientRequest{
clientRequest: clientRequest,
taskReporter: taskReporter,
taskCheckStepDuration: taskCheckStepDuration,
}
}
type taskShortResp struct {
ID int // 165
State string // e.g. "queued", "processing", "done", "error", "cancelled"
}
func (r taskShortResp) IsRunning() bool {
return r.State == "queued" || r.State == "processing" || r.State == "cancelling"
}
func (r taskShortResp) IsSuccessfullyDone() bool {
return r.State == "done"
}
func (r TaskClientRequest) GetResult(path string) (int, []byte, error) {
var taskResp taskShortResp
err := r.clientRequest.Get(path, &taskResp)
if err != nil {
return 0, nil, err
}
respBody, err := r.waitForResult(taskResp)
return taskResp.ID, respBody, err
}
func (r TaskClientRequest) PostResult(path string, payload []byte, f func(*http.Request)) ([]byte, error) {
var taskResp taskShortResp
err := r.clientRequest.Post(path, payload, f, &taskResp)
if err != nil {
return nil, err
}
return r.waitForResult(taskResp)
}
func (r TaskClientRequest) PutResult(path string, payload []byte, f func(*http.Request)) ([]byte, error) {
var taskResp taskShortResp
err := r.clientRequest.Put(path, payload, f, &taskResp)
if err != nil {
return nil, err
}
return r.waitForResult(taskResp)
}
func (r TaskClientRequest) DeleteResult(path string) ([]byte, error) {
var taskResp taskShortResp
err := r.clientRequest.Delete(path, &taskResp)
if err != nil {
return nil, err
}
return r.waitForResult(taskResp)
}
func (r TaskClientRequest) WaitForCompletion(id int, type_ string, taskReporter TaskReporter) error {
taskReporter.TaskStarted(id)
var taskResp taskShortResp
var outputOffset int
defer func() {
taskReporter.TaskFinished(id, taskResp.State)
}()
taskPath := fmt.Sprintf("/tasks/%d", id)
for {
err := r.clientRequest.Get(taskPath, &taskResp)
if err != nil {
return bosherr.WrapError(err, "Getting task state")
}
// retrieve output *after* getting state to make sure
// it's complete in case of task being finished
outputOffset, err = r.reportOutputChunk(taskResp.ID, outputOffset, type_, taskReporter)
if err != nil {
return bosherr.WrapError(err, "Getting task output")
}
if taskResp.IsRunning() {
time.Sleep(r.taskCheckStepDuration)
continue
}
if taskResp.IsSuccessfullyDone() {
return nil
}
msgFmt := "Expected task '%d' to succeed but was state is '%s'"
return bosherr.Errorf(msgFmt, taskResp.ID, taskResp.State)
}
}
func (r TaskClientRequest) waitForResult(taskResp taskShortResp) ([]byte, error) {
err := r.WaitForCompletion(taskResp.ID, "event", r.taskReporter)
if err != nil {
return nil, err
}
resultPath := fmt.Sprintf("/tasks/%d/output?type=result", taskResp.ID)
respBody, _, err := r.clientRequest.RawGet(resultPath, nil, nil)
if err != nil {
return nil, err
}
return respBody, nil
}
func (r TaskClientRequest) reportOutputChunk(id, offset int, type_ string, taskReporter TaskReporter) (int, error) {
outputPath := fmt.Sprintf("/tasks/%d/output?type=%s", id, type_)
setHeaders := func(req *http.Request) {
req.Header.Add("Range", fmt.Sprintf("bytes=%d-", offset))
}
respBodyChunk, resp, err := r.clientRequest.RawGet(outputPath, nil, setHeaders)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusRequestedRangeNotSatisfiable {
return offset, nil
}
return 0, err
}
if len(respBodyChunk) > 0 {
taskReporter.TaskOutputChunk(id, respBodyChunk)
}
return offset + len(respBodyChunk), nil
}