This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
task.go
423 lines (364 loc) · 11 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package worker
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
schematypes "github.com/taskcluster/go-schematypes"
"github.com/taskcluster/taskcluster-client-go"
"github.com/taskcluster/taskcluster-client-go/queue"
"github.com/taskcluster/taskcluster-worker/engines"
"github.com/taskcluster/taskcluster-worker/plugins"
"github.com/taskcluster/taskcluster-worker/runtime"
)
// TaskRun represents the task lifecycle once claimed. TaskRun contains information
// about the task as well as controllers for executing and resolving the task.
type TaskRun struct {
// Static properties
TaskID string
RunID int
definition *queue.TaskDefinitionResponse
plugin plugins.Plugin
monitor runtime.Monitor
// Internal state
m sync.Mutex
taskPlugin plugins.TaskPlugin
context *runtime.TaskContext
controller *runtime.TaskContextController
sandboxBuilder engines.SandboxBuilder
sandbox engines.Sandbox
resultSet engines.ResultSet
engine engines.Engine
success bool
shutdown bool
// Reclaim logic
queueURL string
stopReclaims chan struct{} // close to stop reclaiming task
reclaimsDone chan struct{} // closed when we have stopped reclaiming the task
}
func newTaskRun(
config *configType,
claim *taskClaim,
environment *runtime.Environment,
engine engines.Engine,
plugin plugins.Plugin,
monitor runtime.Monitor,
) (*TaskRun, error) {
tp := environment.TemporaryStorage.NewFilePath()
info := runtime.TaskInfo{
TaskID: claim.taskClaim.Status.TaskID,
RunID: claim.taskClaim.RunID,
Created: claim.taskClaim.Task.Created,
Deadline: claim.taskClaim.Task.Deadline,
Expires: claim.taskClaim.Task.Expires,
}
ctxt, ctxtctl, err := runtime.NewTaskContext(tp, info, environment.WebHookServer)
queueClient := queue.New(&tcclient.Credentials{
ClientID: claim.taskClaim.Credentials.ClientID,
AccessToken: claim.taskClaim.Credentials.AccessToken,
Certificate: claim.taskClaim.Credentials.Certificate,
})
if config.QueueBaseURL != "" {
queueClient.BaseURL = config.QueueBaseURL
}
ctxtctl.SetQueueClient(queueClient)
if err != nil {
return nil, err
}
t := &TaskRun{
TaskID: claim.taskID,
RunID: claim.runID,
definition: claim.definition,
monitor: monitor,
context: ctxt,
controller: ctxtctl,
engine: engine,
plugin: plugin,
queueURL: config.QueueBaseURL,
stopReclaims: make(chan struct{}),
reclaimsDone: make(chan struct{}),
}
go t.reclaim(time.Time(claim.taskClaim.TakenUntil))
return t, nil
}
func (t *TaskRun) reclaim(until time.Time) {
for {
duration := time.Until(until).Seconds()
// Using a reclaim divisor of 1.3 with the default reclaim deadline (20 minutes),
// means that a reclaim event will happen with a few minutes left of the origin claim.
nextReclaim := duration / 1.3
select {
case <-t.stopReclaims:
close(t.reclaimsDone)
return
case <-time.After(time.Duration(nextReclaim * 1e+9)):
client := t.controller.Queue()
claim, err := reclaimTask(client, t.TaskID, t.RunID, t.monitor)
if err != nil {
t.monitor.WithTag("error", err.Error()).Error("Error reclaiming task")
t.Abort()
close(t.reclaimsDone)
return
}
queueClient := queue.New(&tcclient.Credentials{
ClientID: claim.Credentials.ClientID,
AccessToken: claim.Credentials.AccessToken,
Certificate: claim.Credentials.Certificate,
})
queueClient.BaseURL = t.queueURL
t.controller.SetQueueClient(queueClient)
until = time.Time(claim.TakenUntil)
}
}
}
// Abort will set the status of the task to aborted and abort the task execution
// environment if one has been created.
func (t *TaskRun) Abort() {
t.m.Lock()
defer t.m.Unlock()
t.shutdown = true
if t.context != nil {
t.context.Abort()
}
if t.sandbox != nil {
t.sandbox.Abort()
}
}
// Cancel will set the status of the task to cancelled and abort the task execution
// environment if one has been created.
func (t *TaskRun) Cancel() {
t.monitor.Info("Cancelling task")
t.m.Lock()
defer t.m.Unlock()
if t.context != nil {
t.context.Cancel()
}
if t.sandbox != nil {
t.sandbox.Abort()
}
}
// Run is the entrypoint to executing a task run. The task payload will be parsed,
// plugins created, and the task will run through each of the stages of the task
// life cycle.
//
// Tasks that do not complete successfully will be reported as an exception during
// the exceptionStage.
func (t *TaskRun) Run() {
defer t.disposeStage()
stages := []func() error{
t.prepareStage,
t.buildStage,
t.startStage,
t.stopStage,
t.finishStage,
}
for _, stage := range stages {
err := stage()
if err != nil || t.context.IsAborted() || t.context.IsCancelled() {
t.exceptionStage(err)
return
}
}
err := t.resolveTask()
if err != nil || t.context.IsAborted() || t.context.IsCancelled() {
t.monitor.WithTag("error", err.Error()).Warn("Could not resolve task properly")
return
}
}
// prepareStage is where task plugins are prepared and a sandboxbuilder is created.
func (t *TaskRun) prepareStage() error {
t.monitor.Debug("Preparing task run")
// Parse payload
payload := map[string]interface{}{}
err := json.Unmarshal([]byte(t.definition.Payload), &payload)
if err != nil {
err = runtime.NewMalformedPayloadError(fmt.Sprintf("Invalid task payload. %s", err))
t.context.LogError(err.Error())
return err
}
// Construct payload schema
payloadSchema, err := schematypes.Merge(
t.engine.PayloadSchema(),
t.plugin.PayloadSchema(),
)
if err != nil {
panic(fmt.Sprintf(
"Conflicting plugin and engine payload properties, error: %s", err,
))
}
// Validate payload against schema
err = payloadSchema.Validate(payload)
if err != nil {
err = runtime.NewMalformedPayloadError("Schema violation: ", err)
t.context.LogError(err.Error())
return err
}
// Create TaskPlugin
t.taskPlugin, err = t.plugin.NewTaskPlugin(plugins.TaskPluginOptions{
TaskInfo: &runtime.TaskInfo{
TaskID: t.TaskID,
RunID: t.RunID,
},
TaskContext: t.context,
Payload: t.plugin.PayloadSchema().Filter(payload),
Monitor: t.monitor.WithPrefix("plugin").WithTag("plugin", "plugin-manager"),
})
if err != nil {
// TODO: We need to review all this... t.context.LogError is for task errors
// hence MalformedPayloadError only, not for internal-errors!!!
t.context.LogError(err.Error())
return err
}
sandboxBuilder, err := t.engine.NewSandboxBuilder(engines.SandboxOptions{
TaskContext: t.context,
Payload: t.engine.PayloadSchema().Filter(payload),
Monitor: t.monitor.WithPrefix("sandbox").WithTags(map[string]string{
"taskId": t.TaskID,
"runId": fmt.Sprintf("%d", t.RunID),
}),
})
t.m.Lock()
t.sandboxBuilder = sandboxBuilder
t.m.Unlock()
if err != nil {
t.context.LogError(fmt.Sprintf("Could not create task execution environment. %s", err))
return err
}
return nil
}
// buildStage is responsible for configuring the environment for building a sandbox (task execution environment).
func (t *TaskRun) buildStage() error {
t.monitor.Debug("Building task run")
err := t.taskPlugin.BuildSandbox(t.sandboxBuilder)
if err != nil {
t.context.LogError(fmt.Sprintf("Could not create task execution environment. %s", err))
return err
}
return nil
}
// startStage is responsible for starting the execution environment and waiting for a result.
func (t *TaskRun) startStage() error {
t.monitor.Debug("Running task")
sandbox, err := t.sandboxBuilder.StartSandbox()
if err != nil {
t.context.LogError(fmt.Sprintf("Could not start task execution environment. %s", err))
return err
}
t.m.Lock()
t.sandbox = sandbox
t.m.Unlock()
err = t.taskPlugin.Started(t.sandbox)
if err != nil {
t.context.LogError(fmt.Sprintf("Could not start task execution environment. %s", err))
return err
}
result, err := t.sandbox.WaitForResult()
if err != nil {
t.context.LogError(fmt.Sprintf("Task execution did not complete successfully. %s", err))
return err
}
t.m.Lock()
t.resultSet = result
t.m.Unlock()
return nil
}
// stopStage will run once the sandbox has terminated. This stage will be responsible
// for uploading artifacts, cleaning up of resources, etc.
func (t *TaskRun) stopStage() error {
t.monitor.Debug("Stopping task execution")
var err error
t.success, err = t.taskPlugin.Stopped(t.resultSet)
if err != nil {
t.context.LogError(fmt.Sprintf("Stopping execution environment failed. %s", err))
return err
}
return nil
}
// finishStage is responsible for finalizing the execution of a task, close and
// upload tasks logs, etc.
func (t *TaskRun) finishStage() error {
t.monitor.Debug("Finishing task run")
err := t.controller.CloseLog()
if err != nil {
t.monitor.WithTag("error", err.Error()).Warn("Could not properly close task log")
}
err = t.taskPlugin.Finished(t.success)
if err != nil {
t.monitor.Error(fmt.Sprintf("Could not finish cleaning up task execution. %s", err))
return err
}
return nil
}
// exceptionStage will report a task run as an exception with an appropriate reason.
// Tasks that have been cancelled will not be reported as an exception as the run
// has already been resolved.
func (t *TaskRun) exceptionStage(taskError error) {
close(t.stopReclaims)
<-t.reclaimsDone
var reason runtime.ExceptionReason
if t.shutdown {
reason = runtime.WorkerShutdown
} else {
switch taskError.(type) {
case runtime.MalformedPayloadError:
reason = runtime.MalformedPayload
default:
reason = runtime.InternalError
}
}
err := t.controller.CloseLog()
if err != nil {
t.monitor.WithTag("error", err.Error()).Warn("Could not properly close task log")
}
if t.taskPlugin != nil {
err = t.taskPlugin.Exception(reason)
if err != nil {
t.monitor.WithTag("error", err.Error()).Warn("Could not finalize task plugins as exception.")
}
}
if t.context.IsCancelled() {
return
}
e := reportException(t.context.Queue(), t, reason, t.monitor)
if e != nil {
t.monitor.WithTag("error", e.Error()).Warn("Could not resolve task as exception.")
}
return
}
// resolveTask will resolve the task as completed/failed depending on the outcome
// of executing the task and finalizing the task plugins.
func (t *TaskRun) resolveTask() error {
close(t.stopReclaims)
<-t.reclaimsDone
resolve := reportCompleted
if !t.resultSet.Success() {
resolve = reportFailed
}
err := resolve(t.context.Queue(), t, t.monitor)
if err != nil {
return errors.New(err.Error())
}
return nil
}
// disposeStage is responsible for cleaning up resources allocated for the task execution.
// This will involve closing all necessary files and disposing of contexts, plugins, and sandboxes.
func (t *TaskRun) disposeStage() {
if t.taskPlugin != nil {
err := t.taskPlugin.Dispose()
if err != nil {
t.monitor.WithTag("error", err.Error()).Warn("Could not dispose plugin")
}
}
if t.resultSet != nil {
err := t.resultSet.Dispose()
if err != nil {
t.monitor.WithTag("error", err.Error()).Warn("Could not dispose result set")
}
}
err := t.controller.Dispose()
if err != nil {
t.monitor.WithTag("error", err.Error()).Warn("Could not dispose of task context")
}
return
}