This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
/
job.go
302 lines (251 loc) · 7.27 KB
/
job.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package ccv2
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/url"
"time"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
//go:generate counterfeiter . Reader
// Reader is an io.Reader.
type Reader interface {
io.Reader
}
// JobStatus is the current state of a job.
type JobStatus string
const (
// JobStatusFailed is when the job is no longer running due to a failure.
JobStatusFailed JobStatus = "failed"
// JobStatusFinished is when the job is no longer and it was successful.
JobStatusFinished JobStatus = "finished"
// JobStatusQueued is when the job is waiting to be run.
JobStatusQueued JobStatus = "queued"
// JobStatusRunning is when the job is running.
JobStatusRunning JobStatus = "running"
)
// Job represents a Cloud Controller Job.
type Job struct {
Error string
ErrorDetails struct {
Description string
}
GUID string
Status JobStatus
}
// UnmarshalJSON helps unmarshal a Cloud Controller Job response.
func (job *Job) UnmarshalJSON(data []byte) error {
var ccJob struct {
Entity struct {
Error string `json:"error"`
ErrorDetails struct {
Description string `json:"description"`
} `json:"error_details"`
GUID string `json:"guid"`
Status string `json:"status"`
} `json:"entity"`
Metadata internal.Metadata `json:"metadata"`
}
if err := json.Unmarshal(data, &ccJob); err != nil {
return err
}
job.Error = ccJob.Entity.Error
job.ErrorDetails.Description = ccJob.Entity.ErrorDetails.Description
job.GUID = ccJob.Entity.GUID
job.Status = JobStatus(ccJob.Entity.Status)
return nil
}
// Finished returns true when the job has completed successfully.
func (job Job) Finished() bool {
return job.Status == JobStatusFinished
}
// Failed returns true when the job has completed with an error/failure.
func (job Job) Failed() bool {
return job.Status == JobStatusFailed
}
// GetJob returns a job for the provided GUID.
func (client *Client) GetJob(jobGUID string) (Job, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetJobRequest,
URIParams: Params{"job_guid": jobGUID},
})
if err != nil {
return Job{}, nil, err
}
var job Job
response := cloudcontroller.Response{
Result: &job,
}
err = client.connection.Make(request, &response)
return job, response.Warnings, err
}
// PollJob will keep polling the given job until the job has terminated, an
// error is encountered, or config.OverallPollingTimeout is reached. In the
// last case, a JobTimeoutError is returned.
func (client *Client) PollJob(job Job) (Warnings, error) {
originalJobGUID := job.GUID
var (
err error
warnings Warnings
allWarnings Warnings
)
startTime := time.Now()
for time.Now().Sub(startTime) < client.jobPollingTimeout {
job, warnings, err = client.GetJob(job.GUID)
allWarnings = append(allWarnings, Warnings(warnings)...)
if err != nil {
return allWarnings, err
}
if job.Failed() {
return allWarnings, ccerror.JobFailedError{
JobGUID: originalJobGUID,
Message: job.ErrorDetails.Description,
}
}
if job.Finished() {
return allWarnings, nil
}
time.Sleep(client.jobPollingInterval)
}
return allWarnings, ccerror.JobTimeoutError{
JobGUID: originalJobGUID,
Timeout: client.jobPollingTimeout,
}
}
// UploadApplicationPackage uploads the newResources and a list of existing
// resources to the cloud controller. A job that combines the requested/newly
// uploaded bits is returned. If passed an io.Reader, this request will return
// a PipeSeekError on retry.
func (client *Client) UploadApplicationPackage(appGUID string, existingResources []Resource, newResources Reader, newResourcesLength int64) (Job, Warnings, error) {
if existingResources == nil {
return Job{}, nil, ccerror.NilObjectError{Object: "existingResources"}
}
if newResources == nil {
return Job{}, nil, ccerror.NilObjectError{Object: "newResources"}
}
contentLength, err := client.overallRequestSize(existingResources, newResourcesLength)
if err != nil {
return Job{}, nil, err
}
contentType, body, writeErrors := client.createMultipartBodyAndHeaderForAppBits(existingResources, newResources, newResourcesLength)
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PutAppBitsRequest,
URIParams: Params{"app_guid": appGUID},
Query: url.Values{
"async": {"true"},
},
Body: body,
})
if err != nil {
return Job{}, nil, err
}
request.Header.Set("Content-Type", contentType)
request.ContentLength = contentLength
var job Job
response := cloudcontroller.Response{
Result: &job,
}
httpErrors := client.uploadBits(request, &response)
// The following section makes the following assumptions:
// 1) If an error occurs during file reading, an EOF is sent to the request
// object. Thus ending the request transfer.
// 2) If an error occurs during request transfer, an EOF is sent to the pipe.
// Thus ending the writing routine.
var firstError error
var writeClosed, httpClosed bool
for {
select {
case writeErr, ok := <-writeErrors:
if !ok {
writeClosed = true
break
}
if firstError == nil {
firstError = writeErr
}
case httpErr, ok := <-httpErrors:
if !ok {
httpClosed = true
break
}
if firstError == nil {
firstError = httpErr
}
}
if writeClosed && httpClosed {
break
}
}
return job, response.Warnings, firstError
}
func (*Client) createMultipartBodyAndHeaderForAppBits(existingResources []Resource, newResources io.Reader, newResourcesLength int64) (string, io.ReadSeeker, <-chan error) {
writerOutput, writerInput := cloudcontroller.NewPipeBomb()
form := multipart.NewWriter(writerInput)
writeErrors := make(chan error)
go func() {
defer close(writeErrors)
defer writerInput.Close()
jsonResources, err := json.Marshal(existingResources)
if err != nil {
writeErrors <- err
return
}
err = form.WriteField("resources", string(jsonResources))
if err != nil {
writeErrors <- err
return
}
writer, err := form.CreateFormFile("application", "application.zip")
if err != nil {
writeErrors <- err
return
}
if newResourcesLength != 0 {
_, err = io.Copy(writer, newResources)
if err != nil {
writeErrors <- err
return
}
}
err = form.Close()
if err != nil {
writeErrors <- err
}
}()
return form.FormDataContentType(), writerOutput, writeErrors
}
func (*Client) overallRequestSize(existingResources []Resource, newResourcesLength int64) (int64, error) {
body := &bytes.Buffer{}
form := multipart.NewWriter(body)
jsonResources, err := json.Marshal(existingResources)
if err != nil {
return 0, err
}
err = form.WriteField("resources", string(jsonResources))
if err != nil {
return 0, err
}
_, err = form.CreateFormFile("application", "application.zip")
if err != nil {
return 0, err
}
err = form.Close()
if err != nil {
return 0, err
}
return int64(body.Len()) + newResourcesLength, nil
}
func (client *Client) uploadBits(request *cloudcontroller.Request, response *cloudcontroller.Response) <-chan error {
httpErrors := make(chan error)
go func() {
defer close(httpErrors)
err := client.connection.Make(request, response)
if err != nil {
httpErrors <- err
}
}()
return httpErrors
}