-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathpipeline.go
267 lines (226 loc) · 5.97 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
263
264
265
266
267
package pipeline
import (
"context"
"fmt"
"reflect"
"strings"
"time"
"unicode"
"github.com/fatih/color"
)
// DefaultDrainTimeout time to wait for all readers to finish consuming output
const DefaultDrainTimeout = time.Second * 5
// DefaultBuffer channel buffer size of the output buffer
const DefaultBuffer = 1000
// Pipeline is a sequence of stages
type Pipeline struct {
Name string `json:"name"`
Stages []*Stage `json:"stages"`
DrainTimeout time.Duration
expectedDuration time.Duration
duration time.Duration
outsubscribed bool
outbufferlen int
tick time.Duration
cancelDrain context.CancelFunc
cancelProgress context.CancelFunc
}
// New returns a new pipeline
// name of the pipeline
// outBufferLen is the size of the output buffered channel
func New(name string, outBufferLen int) *Pipeline {
return newPipeline(name, outBufferLen)
}
// NewProgress returns a new pipeline which returns progress updates
// name of the pipeline
// outBufferLen is the size of the output buffered channel
//
// expectedDurationInMs is the expected time for the job to finish in milliseconds
// If set, you can get the current time spent from GetDuration()int64 and
// listen on the channel returned by GetProgress() <-chan float64 to get current progress
func NewProgress(name string, outBufferLen int, expectedDuration time.Duration) *Pipeline {
p := newPipeline(name, outBufferLen)
p.expectedDuration = expectedDuration
p.tick = time.Millisecond * 250
return p
}
func newPipeline(name string, outBufferLen int) *Pipeline {
if outBufferLen < 0 {
outBufferLen = 1
}
if buffersMap == nil {
buffersMap = &buffers{bufferMap: make(map[string]*buffer)}
}
p := &Pipeline{Name: spaceMap(name)}
p.outbufferlen = outBufferLen
if p.DrainTimeout == 0 {
p.DrainTimeout = DefaultDrainTimeout
}
buf := buffer{in: make(chan string, outBufferLen), out: []chan string{}, progress: []chan int64{}}
buffersMap.set(p.Name, &buf)
return p
}
// SetDrainTimeout sets DrainTimeout
func (p *Pipeline) SetDrainTimeout(timeout time.Duration) {
p.DrainTimeout = timeout
}
// AddStage adds a new stage to the pipeline
func (p *Pipeline) AddStage(stage ...*Stage) {
for i := range stage {
for j := range stage[i].Steps {
ctx := &stepContextVal{
name: p.Name + "." + stage[i].Name + "." + reflect.TypeOf(stage[i].Steps[j]).String(),
pipelineKey: p.Name,
concurrent: stage[i].Concurrent,
index: j,
}
stage[i].Steps[j].setCtx(ctx)
}
stage[i].pipelineKey = p.Name
}
p.Stages = append(p.Stages, stage...)
}
// Run the pipeline. The stages are executed in sequence while steps may be concurrent or sequential.
func (p *Pipeline) Run() *Result {
if len(p.Stages) == 0 {
return &Result{Error: fmt.Errorf("No stages to be executed")}
}
var ticker *time.Ticker
if p.expectedDuration != 0 && p.tick != 0 {
// start progress update ticker
ticker = time.NewTicker(p.tick)
ctx, cancelProgress := context.WithCancel(context.Background())
p.cancelProgress = cancelProgress
go p.updateProgress(ticker, ctx)
}
buf, ok := buffersMap.get(p.Name)
if !ok {
return &Result{Error: fmt.Errorf("error creating output %s", p.Name)}
}
ctx, cancelDrain := context.WithCancel(context.Background())
p.cancelDrain = cancelDrain
go buf.drainBuffer(ctx)
defer buffersMap.remove(p.Name)
defer p.waitForDrain()
if p.expectedDuration != 0 && p.tick != 0 {
defer ticker.Stop()
}
defer p.status("end")
p.status("begin")
request := &Request{}
result := &Result{}
for i, stage := range p.Stages {
stage.index = i
result = stage.run(request)
if result.Error != nil {
p.status("stage: " + stage.Name + " failed !!! ")
return result
}
request.Data = result.Data
request.KeyVal = result.KeyVal
}
return result
}
// Out collects the status output from the stages and steps
func (p *Pipeline) Out() (<-chan string, error) {
// add a new listener
out := make(chan string, p.outbufferlen)
err := buffersMap.appendOutBuffer(p.Name, out)
if err != nil {
return nil, err
}
return out, nil
}
// GetDuration returns the current time spent by the pipleline
func (p *Pipeline) GetDuration() time.Duration {
return p.duration
}
// GetProgressPercent of the pipeline
func (p *Pipeline) GetProgressPercent() (<-chan int64, error) {
pg := make(chan int64, 1)
err := buffersMap.appendProgressBuffer(p.Name, pg)
if err != nil {
return nil, err
}
return pg, nil
}
// started as a goroutine
func (p *Pipeline) updateProgress(ticker *time.Ticker, ctx context.Context) {
start := time.Now()
for range ticker.C {
p.duration = time.Since(start)
percentDone := int64((p.duration.Seconds() / p.expectedDuration.Seconds()) * 100)
// if estimate is incorrect don't overflow progress end
if percentDone > 100 {
percentDone = 99
}
buf, ok := buffersMap.get(p.Name)
if !ok {
return
}
loop:
for _, pg := range buf.progress {
select {
case <-ctx.Done():
break loop
case pg <- percentDone:
default:
<-pg
pg <- percentDone
}
}
}
}
func (p *Pipeline) waitForDrain() {
buf, ok := buffersMap.get(p.Name)
if !ok {
return
}
var empty = func() chan bool {
emptyChan := make(chan bool)
go func() {
if len(buf.out) == 0 {
return
}
pending := 0
for _, o := range buf.out {
pending += len(o)
}
if pending == 0 && len(buf.in) == 0 {
emptyChan <- true
return
}
emptyChan <- false
}()
return emptyChan
}
loop:
for {
select {
case empty := <-empty():
if empty {
break loop
}
case <-time.After(p.DrainTimeout):
break loop
}
}
p.cancelDrain()
if p.cancelProgress != nil {
p.cancelProgress()
}
}
// status writes a line to the out channel
func (p *Pipeline) status(line string) {
red := color.New(color.FgRed).SprintFunc()
line = red("[pipeline]") + "[" + p.Name + "]: " + line
send(p.Name, line)
}
func spaceMap(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, str)
}