-
Notifications
You must be signed in to change notification settings - Fork 86
/
step_subscribe_cancellation.go
46 lines (34 loc) · 1.17 KB
/
step_subscribe_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
package worker
import (
gocontext "context"
"github.com/mitchellh/multistep"
"go.opencensus.io/trace"
)
type stepSubscribeCancellation struct {
cancellationBroadcaster *CancellationBroadcaster
}
func (s *stepSubscribeCancellation) Run(state multistep.StateBag) multistep.StepAction {
ctx := state.Get("ctx").(gocontext.Context)
_, span := trace.StartSpan(ctx, "SubscribeCancellation.Run")
defer span.End()
if s.cancellationBroadcaster == nil {
ch := make(chan CancellationCommand)
state.Put("cancelChan", (<-chan CancellationCommand)(ch))
return multistep.ActionContinue
}
buildJob := state.Get("buildJob").(Job)
ch := s.cancellationBroadcaster.Subscribe(buildJob.Payload().Job.ID)
state.Put("cancelChan", ch)
return multistep.ActionContinue
}
func (s *stepSubscribeCancellation) Cleanup(state multistep.StateBag) {
if s.cancellationBroadcaster == nil {
return
}
ctx := state.Get("ctx").(gocontext.Context)
_, span := trace.StartSpan(ctx, "SubscribeCancellation.Cleanup")
defer span.End()
buildJob := state.Get("buildJob").(Job)
ch := state.Get("cancelChan").(<-chan CancellationCommand)
s.cancellationBroadcaster.Unsubscribe(buildJob.Payload().Job.ID, ch)
}