forked from vmware-archive/atc
/
on_abort.go
46 lines (39 loc) · 1.09 KB
/
on_abort.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 exec
import (
"context"
)
// OnAbortStep will run one step, and then a second step if the first step
// aborts (but not errors).
type OnAbortStep struct {
step Step
hook Step
}
// OnAbort constructs an OnAbortStep factory.
func OnAbort(step Step, hook Step) OnAbortStep {
return OnAbortStep{
step: step,
hook: hook,
}
}
// Run will call Run on the first step and wait for it to complete. If the
// first step errors, Run returns the error. OnAbortStep is ready as soon as
// the first step is ready.
//
// If the first step aborts (that is, it gets interrupted), the second
// step is executed. If the second step errors, its error is returned.
func (o OnAbortStep) Run(ctx context.Context, state RunState) error {
stepRunErr := o.step.Run(ctx, state)
if stepRunErr == nil {
return nil
}
if stepRunErr == context.Canceled {
// run only on abort, not timeout
o.hook.Run(context.Background(), state)
}
return stepRunErr
}
// Succeeded is true if the first step doesn't exist, or if it
// completed successfully.
func (o OnAbortStep) Succeeded() bool {
return o.step.Succeeded()
}