-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
262 lines (230 loc) · 5.47 KB
/
pipeline.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package exec
import (
"errors"
"strings"
"time"
)
// Pipeline commands pipeline
type Pipeline struct {
// pipeline started
Started bool
// pipeline done
Done bool
// pipeline failed
Failed bool
// pipeline canceld
Canceled bool
// pipline Timedout
Timedout bool
// Cmd pointer slice
PipeCmds []*PipeCmd
// current index of PipeCmd
CurrentIndex int
// EventHandler
EventHandler *EventHandler
// TimeoutDur duration
TimeoutDur time.Duration
// FailureMsg custom error message when failed at handler
FailureMsg string
// current Under
currentDir string
// current Env
currentEnv []string
// current Event Handler
currentEventHandler *EventHandler
// chan to check pipeline is running
runningChan chan bool
}
// NewPipeline new pipeline.
func NewPipeline() *Pipeline {
pipe := Pipeline{
currentDir: "",
currentEnv: []string{},
EventHandler: DefaultEventHandler,
}
return &pipe
}
// SetPipelineEventHandler set event handler of pipeline
func (pipe *Pipeline) SetPipelineEventHandler(eh *EventHandler) *Pipeline {
pipe.EventHandler = eh
pipe.currentEventHandler = eh
return pipe
}
// SetTimeout set timeout
func (pipe *Pipeline) SetTimeout(dur time.Duration) *Pipeline {
pipe.TimeoutDur = dur
return pipe
}
// Under under which dir.
func (pipe *Pipeline) Under(dir string) *Pipeline {
if pipe.Started {
return pipe
}
pipe.currentDir = dir
return pipe
}
// SetEnv replace whole Env for cmds. KeyValue should be like 'Key=Value'.
func (pipe *Pipeline) SetEnv(keyValue string, more ...string) *Pipeline {
if pipe.Started {
return pipe
}
pipe.currentEnv = append(more, keyValue)
return pipe
}
// SetCmdEventHandler set Event Handler for cmds. It inheirt from pipeline's by default.
func (pipe *Pipeline) SetCmdEventHandler(eh *EventHandler) *Pipeline {
pipe.currentEventHandler = eh
return pipe
}
// AddEnv Add Env.
func (pipe *Pipeline) AddEnv(name, value string) *Pipeline {
if pipe.Started {
return pipe
}
pipe.currentEnv = append(pipe.currentEnv, name+"="+value)
return pipe
}
// RemoveEnv Remove Env by name.
func (pipe *Pipeline) RemoveEnv(name string) *Pipeline {
if pipe.Started {
return pipe
}
newEnv := []string{}
for _, Env := range pipe.currentEnv {
if !strings.HasPrefix(Env, name+"=") {
newEnv = append(newEnv, Env)
}
}
pipe.currentEnv = newEnv
return pipe
}
// Do add cmd to pipeline. It use latest Dir/Env you set before.
func (pipe *Pipeline) Do(name string, arg ...string) *PipeCmd {
if pipe.Started {
return nil
}
cmd := NewCmd(pipe.currentDir, name, arg...).SetEnv(pipe.currentEnv)
if pipe.currentEventHandler != nil {
cmd.SetEventHandler(pipe.currentEventHandler)
} else if pipe.EventHandler != nil {
cmd.SetEventHandler(pipe.EventHandler)
}
pcmd := &PipeCmd{
Cmd: cmd,
HandleFunc: pipeStopAtErr,
pipe: pipe,
}
pipe.PipeCmds = append(pipe.PipeCmds, pcmd)
return pcmd
}
// Finished pipeline lifecycle is finished or not
func (pipe *Pipeline) Finished() bool {
return pipe.Done || pipe.Failed || pipe.Canceled
}
// Start start pipeline
func (pipe *Pipeline) Start() error {
if pipe.Started {
return errors.New("Pipeline already started")
}
pipe.Started = true
pipe.runningChan = make(chan bool, 1)
go pipe.runCmds()
if pipe.TimeoutDur != 0 {
go pipe.checkTimeout()
}
if pipe.EventHandler.PipelineStarted != nil {
pipe.EventHandler.PipelineStarted(pipe)
}
return nil
}
// do timeout checking
func (pipe *Pipeline) checkTimeout() {
timer := time.NewTimer(pipe.TimeoutDur)
<-timer.C
if !(pipe.Failed || pipe.Done) && !pipe.Canceled {
pipe.Timedout = true
pipe.Cancel()
}
}
func (pipe *Pipeline) runCmds() {
for i := 0; i < len(pipe.PipeCmds); i++ {
if pipe.Finished() {
break
}
c := pipe.PipeCmds[i]
time.Sleep(c.DelayDur)
if pipe.Finished() {
break
}
pipe.CurrentIndex = i
err := c.Cmd.Start()
if err == nil {
err = c.Cmd.Wait()
}
if pipe.Canceled {
break
} else {
success, msg := c.HandleFunc(c.Cmd.Output(), err)
c.HandleMsg = msg
if !success {
pipe.Failed = true
pipe.FailureMsg = msg
break
}
}
}
if !pipe.Canceled && !pipe.Failed {
pipe.Done = true
}
pipe.runningChan <- false
}
// Wait block until Done/Failed/Canceled
func (pipe *Pipeline) Wait() error {
if !pipe.Started {
return errors.New("Pipeline cant Wait() before Start()")
}
if pipe.runningChan == nil {
return errors.New("Pipeline cant Wait() after lifecycle is finished")
}
<-pipe.runningChan
close(pipe.runningChan)
pipe.runningChan = nil
if pipe.Failed && pipe.EventHandler.PipelineFailed != nil {
pipe.EventHandler.PipelineFailed(pipe)
}
if pipe.Done && pipe.EventHandler.PipelineDone != nil {
pipe.EventHandler.PipelineDone(pipe)
}
return nil
}
// Run run pipeline and wait for result
func (pipe *Pipeline) Run() error {
err := pipe.Start()
if err != nil {
return err
}
return pipe.Wait()
}
// Cancel cancel pipeline
func (pipe *Pipeline) Cancel() {
if pipe.Canceled {
return
}
pipe.Canceled = true
pipe.PipeCmds[pipe.CurrentIndex].Cmd.Cancel()
if pipe.EventHandler.PipelineCanceled != nil {
pipe.EventHandler.PipelineCanceled(pipe)
}
}
// GetCmdsOutput get output of all Cmds concat into one string
func (pipe *Pipeline) GetCmdsOutput() (string, error) {
if !pipe.Finished() {
return "", errors.New("Pipeline cant GetCmdsOutput() before lifecycle is finished")
}
strs := []string{}
for i := 0; i <= pipe.CurrentIndex; i++ {
c := pipe.PipeCmds[i].Cmd
strs = append(strs, c.Output()+c.Error())
}
return strings.Join(strs, ""), nil
}