-
Notifications
You must be signed in to change notification settings - Fork 86
/
step_update_state.go
107 lines (85 loc) · 2.85 KB
/
step_update_state.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
package worker
import (
gocontext "context"
"time"
"github.com/mitchellh/multistep"
"github.com/sirupsen/logrus"
"github.com/travis-ci/worker/backend"
"github.com/travis-ci/worker/context"
"go.opencensus.io/trace"
)
type stepUpdateState struct{}
func (s *stepUpdateState) Run(state multistep.StateBag) multistep.StepAction {
ctx := state.Get("ctx").(gocontext.Context)
buildJob := state.Get("buildJob").(Job)
instance := state.Get("instance").(backend.Instance)
processedAt := state.Get("processedAt").(time.Time)
logWriter := state.Get("logWriter").(LogWriter)
logger := context.LoggerFromContext(ctx).WithField("self", "step_update_state")
instanceID := instance.ID()
if instanceID != "" {
ctx = context.FromInstanceID(ctx, instanceID)
state.Put("ctx", ctx)
}
defer context.TimeSince(ctx, "step_update_state_run", time.Now())
ctx, span := trace.StartSpan(ctx, "UpdateState.Run")
defer span.End()
logWriter.SetJobStarted(&JobStartedMeta{
QueuedAt: buildJob.Payload().Job.QueuedAt,
Repo: buildJob.Payload().Repository.Slug,
Queue: buildJob.Payload().Queue,
Infra: state.Get("infra").(string),
})
err := buildJob.Started(ctx)
if err != nil {
context.LoggerFromContext(ctx).WithFields(logrus.Fields{
"err": err,
"self": "step_update_state",
"instance_id": instanceID,
}).Error("couldn't mark job as started")
}
logger.WithFields(logrus.Fields{
"since_processed_ms": time.Since(processedAt).Seconds() * 1e3,
"action": "run",
}).Info("marked job as started")
return multistep.ActionContinue
}
func (s *stepUpdateState) Cleanup(state multistep.StateBag) {
buildJob := state.Get("buildJob").(Job)
ctx := state.Get("ctx").(gocontext.Context)
processedAt := state.Get("processedAt").(time.Time)
instance := state.Get("instance").(backend.Instance)
instanceID := instance.ID()
if instanceID != "" {
ctx = context.FromInstanceID(ctx, instanceID)
state.Put("ctx", ctx)
}
defer context.TimeSince(ctx, "step_update_state_cleanup", time.Now())
ctx, span := trace.StartSpan(ctx, "UpdateState.Cleanup")
defer span.End()
logger := context.LoggerFromContext(ctx).WithField("self", "step_update_state")
logger.WithFields(logrus.Fields{
"since_processed_ms": time.Since(processedAt).Seconds() * 1e3,
"action": "cleanup",
}).Info("cleaning up")
mresult, ok := state.GetOk("scriptResult")
if ok {
result := mresult.(*backend.RunResult)
var err error
switch result.ExitCode {
case 0:
err = buildJob.Finish(ctx, FinishStatePassed)
case 1:
err = buildJob.Finish(ctx, FinishStateFailed)
default:
err = buildJob.Finish(ctx, FinishStateErrored)
}
if err != nil {
context.LoggerFromContext(ctx).WithFields(logrus.Fields{
"err": err,
"self": "step_update_state",
"instance_id": instanceID,
}).Error("couldn't mark job as finished")
}
}
}