forked from vmware-archive/atc
/
ensure_step.go
65 lines (51 loc) · 1.51 KB
/
ensure_step.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
package exec
import (
"os"
"github.com/concourse/atc/worker"
"github.com/hashicorp/go-multierror"
)
// EnsureStep will run one step, and then a second step regardless of whether
// the first step fails or errors.
type EnsureStep struct {
stepFactory StepFactory
ensureFactory StepFactory
repo *worker.ArtifactRepository
step Step
ensure Step
}
// Ensure constructs an EnsureStep factory.
func Ensure(firstStep StepFactory, secondStep StepFactory) EnsureStep {
return EnsureStep{
stepFactory: firstStep,
ensureFactory: secondStep,
}
}
// Using constructs an *EnsureStep.
func (o EnsureStep) Using(repo *worker.ArtifactRepository) Step {
o.repo = repo
o.step = o.stepFactory.Using(o.repo)
return &o
}
// Run will call Run on the first step, wait for it to complete, and then call
// Run on the second step, regardless of whether the first step failed or
// errored.
//
// If the first step or the second step errors, an aggregate of their errors is
// returned.
func (o *EnsureStep) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
var errors error
originalErr := o.step.Run(signals, ready)
if originalErr != nil {
errors = multierror.Append(errors, originalErr)
}
o.ensure = o.ensureFactory.Using(o.repo)
hookErr := o.ensure.Run(signals, make(chan struct{}))
if hookErr != nil {
errors = multierror.Append(errors, hookErr)
}
return errors
}
// Succeeded is true if both of its steps are true
func (o *EnsureStep) Succeeded() bool {
return o.step.Succeeded() && o.ensure.Succeeded()
}