-
Notifications
You must be signed in to change notification settings - Fork 86
/
http_job.go
268 lines (212 loc) · 6.59 KB
/
http_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
package worker
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"time"
gocontext "context"
"github.com/bitly/go-simplejson"
"github.com/jtacoma/uritemplates"
"github.com/pkg/errors"
"github.com/travis-ci/worker/backend"
"github.com/travis-ci/worker/context"
"github.com/travis-ci/worker/metrics"
)
type httpJob struct {
payload *httpJobPayload
rawPayload *simplejson.Json
startAttributes *backend.StartAttributes
received time.Time
started time.Time
finished time.Time
finishState FinishState
requeued bool
stateCount uint
refreshClaim func(gocontext.Context)
deleteSelf func(gocontext.Context) error
cancelSelf func(gocontext.Context)
}
type jobScriptPayload struct {
Name string `json:"name"`
Encoding string `json:"encoding"`
Content string `json:"content"`
}
type httpJobPayload struct {
Data *JobPayload `json:"data"`
JobScript jobScriptPayload `json:"job_script"`
JobStateURL string `json:"job_state_url"`
JobPartsURL string `json:"log_parts_url"`
JWT string `json:"jwt"`
ImageName string `json:"image_name"`
}
func (j *httpJob) GoString() string {
return fmt.Sprintf("&httpJob{payload: %#v, startAttributes: %#v}",
j.payload, j.startAttributes)
}
func (j *httpJob) Payload() *JobPayload {
return j.payload.Data
}
func (j *httpJob) RawPayload() *simplejson.Json {
return j.rawPayload
}
func (j *httpJob) StartAttributes() *backend.StartAttributes {
return j.startAttributes
}
func (j *httpJob) FinishState() FinishState {
return j.finishState
}
func (j *httpJob) Requeued() bool {
return j.requeued
}
func (j *httpJob) Error(ctx gocontext.Context, errMessage string) error {
log, err := j.LogWriter(ctx, time.Minute)
if err != nil {
return err
}
_, err = log.WriteAndClose([]byte(errMessage))
if err != nil {
return err
}
return j.Finish(ctx, FinishStateErrored)
}
func (j *httpJob) Requeue(ctx gocontext.Context) error {
context.LoggerFromContext(ctx).WithField("self", "http_job").Info("requeueing job")
metrics.Mark("worker.job.requeue")
j.requeued = true
j.received = time.Time{}
j.started = time.Time{}
return j.sendStateUpdate(ctx, j.currentState(), "created")
}
func (j *httpJob) Received(ctx gocontext.Context) error {
j.received = time.Now()
if j.refreshClaim != nil {
context.LoggerFromContext(ctx).WithField("self", "http_job").Debug("starting claim refresh goroutine")
go j.refreshClaim(context.FromJWT(ctx, j.payload.JWT))
}
return j.sendStateUpdate(ctx, "queued", "received")
}
func (j *httpJob) Started(ctx gocontext.Context) error {
j.started = time.Now()
metrics.TimeSince("travis.worker.job.start_time", j.received)
return j.sendStateUpdate(ctx, "received", "started")
}
func (j *httpJob) currentState() string {
currentState := "queued"
if !j.received.IsZero() {
currentState = "received"
}
if !j.started.IsZero() {
currentState = "started"
}
return currentState
}
func (j *httpJob) Finish(ctx gocontext.Context, state FinishState) error {
err := j.deleteSelf(ctx)
if err != nil {
return err
}
j.finished = time.Now()
if j.received.IsZero() {
j.received = j.finished
}
if j.started.IsZero() {
j.started = j.finished
}
return j.sendStateUpdate(ctx, j.currentState(), string(state))
}
func (j *httpJob) LogWriter(ctx gocontext.Context, defaultLogTimeout time.Duration) (LogWriter, error) {
logTimeout := time.Duration(j.payload.Data.Timeouts.LogSilence) * time.Second
if logTimeout == 0 {
logTimeout = defaultLogTimeout
}
return newHTTPLogWriter(ctx, j.payload.JobPartsURL, j.payload.JWT, j.payload.Data.Job.ID, logTimeout)
}
func (j *httpJob) Generate(ctx gocontext.Context, job Job) ([]byte, error) {
if j.payload.JobScript.Encoding != "base64" {
return nil, errors.Errorf("unknown job script encoding: %s", j.payload.JobScript.Encoding)
}
script, err := base64.StdEncoding.DecodeString(j.payload.JobScript.Content)
if err != nil {
return nil, errors.Wrap(err, "couldn't base64 decode job script")
}
return script, nil
}
func (j *httpJob) createStateUpdateBody(curState, newState string) map[string]interface{} {
body := map[string]interface{}{
"id": j.Payload().Job.ID,
"state": newState,
"cur": curState,
"new": newState,
"meta": map[string]interface{}{
"state_update_count": j.stateCount,
},
}
if j.Payload().Job.QueuedAt != nil {
body["queued_at"] = j.Payload().Job.QueuedAt.UTC().Format(time.RFC3339)
}
if !j.received.IsZero() {
body["received_at"] = j.received.UTC().Format(time.RFC3339)
}
if !j.started.IsZero() {
body["started_at"] = j.started.UTC().Format(time.RFC3339)
}
if !j.finished.IsZero() {
body["finished_at"] = j.finished.UTC().Format(time.RFC3339)
}
if j.Payload().Trace {
body["trace"] = true
}
return body
}
func (j *httpJob) sendStateUpdate(ctx gocontext.Context, curState, newState string) error {
j.stateCount++
payload := j.createStateUpdateBody(curState, newState)
encodedPayload, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "error encoding json")
}
template, err := uritemplates.Parse(j.payload.JobStateURL)
if err != nil {
return errors.Wrap(err, "couldn't parse base URL template")
}
u, err := template.Expand(map[string]interface{}{
"job_id": j.payload.Data.Job.ID,
})
if err != nil {
return errors.Wrap(err, "couldn't expand base URL template")
}
req, err := http.NewRequest("PATCH", u, bytes.NewReader(encodedPayload))
if err != nil {
return errors.Wrap(err, "couldn't create request")
}
req = req.WithContext(ctx)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", j.payload.JWT))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "error making state update request")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
j.handleStateUpdateError(ctx, resp.StatusCode, newState)
return errors.Errorf("expected %d, but got %d", http.StatusOK, resp.StatusCode)
}
return nil
}
func (j *httpJob) handleStateUpdateError(ctx gocontext.Context, status int, newState string) {
if status != http.StatusConflict {
return
}
if newState == "received" || newState == "started" {
// NOTE: receiving a conflict response when attempting to transition to
// 'received' or 'started' means that the job is potentially being run
// by multiple workers. Assume the worst and cancel self.
j.cancelSelf(ctx)
}
}
func (j *httpJob) SetupContext(ctx gocontext.Context) gocontext.Context {
return context.FromJWT(ctx, j.payload.JWT)
}
func (j *httpJob) Name() string { return "http" }