-
Notifications
You must be signed in to change notification settings - Fork 86
/
step_check_cancellation.go
61 lines (48 loc) · 1.87 KB
/
step_check_cancellation.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
package worker
import (
gocontext "context"
"errors"
"fmt"
"github.com/mitchellh/multistep"
"github.com/travis-ci/worker/context"
"go.opencensus.io/trace"
)
var JobCancelledError = errors.New("job cancelled")
type stepCheckCancellation struct{}
func (s *stepCheckCancellation) Run(state multistep.StateBag) multistep.StepAction {
cancelChan := state.Get("cancelChan").(<-chan CancellationCommand)
ctx := state.Get("ctx").(gocontext.Context)
_, span := trace.StartSpan(ctx, "CheckCancellation.Run")
defer span.End()
select {
case command := <-cancelChan:
ctx := state.Get("ctx").(gocontext.Context)
buildJob := state.Get("buildJob").(Job)
if _, ok := state.GetOk("logWriter"); ok {
logWriter := state.Get("logWriter").(LogWriter)
s.writeLogAndFinishWithState(ctx, logWriter, buildJob, FinishStateCancelled, fmt.Sprintf("\n\nDone: Job Cancelled\n\n%s", command.Reason))
} else {
err := buildJob.Finish(ctx, FinishStateCancelled)
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).WithField("state", FinishStateCancelled).Error("couldn't update job state")
}
}
state.Put("err", JobCancelledError)
return multistep.ActionHalt
default:
}
return multistep.ActionContinue
}
func (s *stepCheckCancellation) Cleanup(state multistep.StateBag) {}
func (s *stepCheckCancellation) writeLogAndFinishWithState(ctx gocontext.Context, logWriter LogWriter, buildJob Job, state FinishState, logMessage string) {
ctx, span := trace.StartSpan(ctx, "WriteLogAndFinishWithState.CheckCancellation")
defer span.End()
_, err := logWriter.WriteAndClose([]byte(logMessage))
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't write final log message")
}
err = buildJob.Finish(ctx, state)
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).WithField("state", state).Error("couldn't update job state")
}
}