-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.go
173 lines (143 loc) · 3.46 KB
/
plan.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package gsd
import (
"container/list"
"context"
"sync"
"time"
)
// PlanOpt represents a Plan creation option.
type PlanOpt func(*Plan) error
// PlanOptContinueOnError instructs the plan to continue its execution when
// one or multiple steps execution fail, whereas by default it stops at the
// first error encountered.
func PlanOptContinueOnError() PlanOpt {
return func(p *Plan) error {
p.continueOnError = true
return nil
}
}
// PlanOptLimitDuration instructs the plan to time out if its execution
// exceeds the duration d.
func PlanOptLimitDuration(d time.Duration) PlanOpt {
return func(p *Plan) error {
p.maxDuration = d
return nil
}
}
// Plan represents a plan instance.
type Plan struct {
state *State
steps *list.List
continueOnError bool
maxDuration time.Duration
}
// NewPlan returns a new plan.
func NewPlan(opts ...PlanOpt) (*Plan, error) {
plan := Plan{
state: &State{sync.Map{}},
steps: list.New(),
}
for _, opt := range opts {
if err := opt(&plan); err != nil {
return nil, err
}
}
return &plan, nil
}
// AddStep adds a new step to the plan.
func (p *Plan) AddStep(step Step) *Plan {
p.steps.PushBack(step)
return p
}
// AddPause injects a pause of duration d after the latest step added to the
// plan. Note: pauses are ignored during the cleanup phase.
func (p *Plan) AddPause(d time.Duration) *Plan {
return p.AddStep(&pauseStep{d: d})
}
// Execute executes the plan's steps sequentially until completion, or
// stops and returns a non-nil error if a step failed (unless the
// PlanOptContinueOnError option has been specified during plan creation).
func (p *Plan) Execute(ctx context.Context) error {
var cancel context.CancelFunc
if p.maxDuration > 0 {
ctx, cancel = context.WithTimeout(ctx, p.maxDuration)
} else {
ctx, cancel = context.WithCancel(ctx)
}
errCh := make(chan error)
go func(cancelFunc context.CancelFunc) {
var (
lastOK *list.Element
err error
)
defer cancelFunc()
for s := p.steps.Front(); s != nil; s = s.Next() {
step := s.Value.(Step)
for attempt := 0; attempt <= step.Retries(); attempt++ {
if err = ctx.Err(); err != nil {
errCh <- err
return
}
if err = step.PreExec(ctx, p.state); err != nil {
if !p.continueOnError {
goto stop
}
if step.Retries() > attempt {
continue
}
}
if err = step.Exec(ctx, p.state); err != nil {
if !p.continueOnError {
goto stop
}
if step.Retries() > attempt {
continue
}
}
if err = step.PostExec(ctx, p.state); err != nil {
if !p.continueOnError {
goto stop
}
if step.Retries() > attempt {
continue
}
}
}
// Save last successful step as starting point of the cleanup phase.
lastOK = s
}
stop:
for step := lastOK; step != nil; step = step.Prev() {
if ctx.Err() != nil {
errCh <- ctx.Err()
return
}
// Skip pause steps during cleanup phase.
if _, ok := step.Value.(*pauseStep); ok {
continue
}
step.Value.(Step).Cleanup(ctx, p.state)
}
errCh <- err
}(cancel)
select {
// Plan finished execution.
case err := <-errCh:
cancel()
close(errCh)
return err
// Parent context aborted.
case <-ctx.Done():
switch ctx.Err() {
case context.Canceled:
return ErrCancelled
case context.DeadlineExceeded:
return ErrTimeout
}
}
return nil
}
// State returns the plan's current state shared between steps.
func (p *Plan) State() *State {
return p.state
}