-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
192 lines (164 loc) · 4.49 KB
/
stream.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
package ingest
import "fmt"
import "reflect"
// StreamForEachFn is a function that operates over each record in a stream
type StreamForEachFn func(rec interface{}) error
// A Streamer is used to manipulate input in and out of channels and slices
type Streamer struct {
Opts StreamOpts
Log Logger
In <-chan interface{}
Out chan interface{}
depGroup *DependencyGroup
forEachs []StreamForEachFn
}
// StreamOpts are the options used to configure a Streamer
type StreamOpts struct {
Progress chan struct{}
}
// NewStream builds a new Streamer. Generally you will want to use
// StreamArray or Stream instead
func NewStream() *Streamer {
return &Streamer{
depGroup: NewDependencyGroup(),
}
}
// Stream builds a new Streamer that will read from the input channel
func Stream(input <-chan interface{}) *Streamer {
stream := NewStream()
stream.Log = DefaultLogger.WithField("task", "stream")
stream.In = input
return stream
}
// StreamArray builds a Streamer which will ready from the provided array
func StreamArray(array interface{}) *Streamer {
arrValue := reflect.ValueOf(array)
if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array {
panic(fmt.Sprintf("ingest.StreamArray called on non-array type: %s", arrValue.Type().String()))
}
size := arrValue.Len()
input := make(chan interface{}, size)
for i := 0; i < size; i++ {
input <- arrValue.Index(i).Interface()
}
close(input)
stream := NewStream()
stream.Log = DefaultLogger.WithField("task", "stream-array")
stream.In = input
return stream
}
// WriteTo is a chainable configuration method that sets
// where the Streamer will output records to
func (s *Streamer) WriteTo(output chan interface{}) *Streamer {
s.Out = output
return s
}
// ReportProgressTo is a chainable configuration method that sets
// where the Streamer will report progress events
func (s *Streamer) ReportProgressTo(progress chan struct{}) *Streamer {
s.Opts.Progress = progress
return s
}
// DependOn is a chainable configuration method that will not proceed until all
// specified controllers have resolved
func (s *Streamer) DependOn(ctrls ...*Controller) *Streamer {
s.depGroup.SetCtrls(ctrls...)
return s
}
// ForEach is a chainable configuration method that will
// execute a function on the specified stream.
//
// If an error is returned, it will be reported to the controller
// and the record will not be transmitted
//
// For each can be called multiple times, in which case the functions will
// be executed in the order that they were added to the stream
func (s *Streamer) ForEach(fn StreamForEachFn) *Streamer {
s.forEachs = append(s.forEachs, fn)
return s
}
// Start starts running the Stream task under the control of the specified controller
func (s *Streamer) Start(ctrl *Controller) <-chan interface{} {
ctrl = ctrl.Child()
defer ctrl.ChildBuilt()
s.depGroup.Wait()
out := s.Out
if out == nil {
out = make(chan interface{})
go func() {
ctrl.Wait()
close(out)
}()
}
ctrl.WorkerStart()
s.Log.Debug("Starting worker")
go func() {
defer ctrl.WorkerEnd()
defer s.Log.Debug("Exiting worker")
for {
select {
case <-ctrl.Quit:
return
case rec, ok := <-s.In:
if !ok {
return
}
if err := s.runForEachs(rec); err != nil {
ctrl.Err <- err
continue
}
select {
case <-ctrl.Quit:
return
case out <- rec:
s.reportProgress()
continue
}
}
}
}()
return out
}
// Collect reads the results of the Input into an array, under the control of the specified controller.
//
// If the Controller is aborted, ErrAborted will be returned
func (s *Streamer) Collect(ctrl *Controller) ([]interface{}, error) {
resultChan := s.Start(ctrl)
results := []interface{}{}
for {
select {
case rec, ok := <-resultChan:
if !ok {
return results, nil
}
results = append(results, rec)
case err := <-ctrl.Err:
return nil, err
case <-ctrl.Quit:
return nil, ErrAborted
}
}
}
// reportProgress will emit a progress event if there is a configured listener
func (s *Streamer) reportProgress() {
if s.Opts.Progress != nil {
go func() {
s.Opts.Progress <- struct{}{}
}()
}
}
// runForEachs will run all of the forEach functions on the specified record, or
// return the first error encountered.
//
// If there are no forEachs, it is essentially a no-op
func (s *Streamer) runForEachs(rec interface{}) error {
if s.forEachs == nil || len(s.forEachs) == 0 {
return nil
}
for _, fn := range s.forEachs {
if err := fn(rec); err != nil {
return err
}
}
return nil
}