This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
scribe_multi.go
255 lines (203 loc) · 6.51 KB
/
scribe_multi.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
package scribe
import (
"context"
"fmt"
"github.com/grafana/scribe/plumbing"
"github.com/grafana/scribe/plumbing/pipeline"
"github.com/sirupsen/logrus"
)
type ScribeMulti struct {
Client pipeline.Client
Collection *pipeline.Collection
// Opts are the options that are provided to the pipeline from outside sources. This includes mostly command-line arguments and environment variables
Opts pipeline.CommonOpts
Log logrus.FieldLogger
Version string
n *counter
pipeline int64
prev []pipeline.Pipeline
}
func (s *ScribeMulti) serial() int64 {
return s.n.Next()
}
// runPipeliens adds the list of pipelines to the collection. Pipelines are essentially branches in the graph.
// The pipelines provided run one after another.
func (s *ScribeMulti) runPipelines(pipelines ...pipeline.Pipeline) error {
prev := s.prev
for _, v := range pipelines {
v.Dependencies = prev
if err := s.Collection.AddPipelines(v); err != nil {
return fmt.Errorf("error adding pipeline '%d' to collection. error: %w", v.ID, err)
}
prev = []pipeline.Pipeline{v}
}
s.prev = prev
return nil
}
func (s *ScribeMulti) Run(steps ...pipeline.Pipeline) {
if err := s.runPipelines(steps...); err != nil {
s.Log.Fatalln(err)
}
}
func (s *ScribeMulti) parallelPipelines(pipelines ...pipeline.Pipeline) error {
for i := range pipelines {
pipelines[i].Dependencies = s.prev
}
if err := s.Collection.AddPipelines(pipelines...); err != nil {
return fmt.Errorf("error adding '%d' parallel pipelines to collection. error: %w", len(pipelines), err)
}
s.prev = pipelines
return nil
}
func (s *ScribeMulti) Parallel(steps ...pipeline.Pipeline) {
if err := s.parallelPipelines(steps...); err != nil {
s.Log.Fatalln(err)
}
}
func (s *ScribeMulti) subMulti(sub *ScribeMulti) error {
prev := s.prev
for i, v := range sub.Collection.Graph.Nodes {
if v.ID == 0 || v.ID == DefaultPipelineID {
continue
}
sub.Collection.Graph.Nodes[i].Value.Type = pipeline.PipelineTypeSub
if len(v.Value.Dependencies) == 0 {
sub.Collection.Graph.Nodes[i].Value.Dependencies = prev
}
if err := s.Collection.AddPipelines(sub.Collection.Graph.Nodes[i].Value); err != nil {
return err
}
s.Log.Debugln("Appended pipeline", v.ID, v.Value.Name)
}
return nil
}
func (s *ScribeMulti) newSub() *ScribeMulti {
serial := s.n.Next()
opts := s.Opts
opts.Name = fmt.Sprintf("sub-pipeline-%d", serial)
collection := NewDefaultCollection(opts)
return &ScribeMulti{
Client: s.Client,
Opts: opts,
Log: s.Log.WithField("sub-pipeline", opts.Name),
Version: s.Version,
n: s.n,
Collection: collection,
pipeline: DefaultPipelineID,
}
}
type MultiSubFunc func(*ScribeMulti)
func (s *ScribeMulti) Sub(sf MultiSubFunc) {
sub := s.newSub()
sf(sub)
if err := s.subMulti(sub); err != nil {
s.Log.WithError(err).Fatalln("failed to add sub-pipeline")
}
}
// Execute is the equivalent of Done, but returns an error.
// Done should be preferred in Scribe pipelines as it includes sub-process handling and logging.
func (s *ScribeMulti) Execute(ctx context.Context, collection *pipeline.Collection) error {
if err := s.Client.Done(ctx, collection); err != nil {
return err
}
return nil
}
func (s *ScribeMulti) Done() {
ctx := context.Background()
if err := execute(ctx, s.Collection, nameOrDefault(s.Opts.Name), s.Opts, s.n, s.Execute); err != nil {
s.Log.WithError(err).Fatal("error in execution")
}
}
// NewMulti is the equivalent of `scribe.New`, but for building a pipeline made of multiple pipelines.
// Pipelines can behave in the same way that a step does. They can be ran in parallel using the Parallel function, or ran in a series using the Run function.
// To add new pipelines to execution, use the `(*scribe.ScribeMulti).New(...)` function.
func NewMulti() *ScribeMulti {
opts, err := parseOpts()
if err != nil {
panic(fmt.Sprintf("failed to parse arguments: %s", err.Error()))
}
sw := NewClient(opts, NewMultiCollection())
return &ScribeMulti{
Client: sw.Client,
Collection: sw.Collection,
Opts: opts,
Log: sw.Log,
// Ensure that no matter the behavior of the initializer, we still set the version on the scribe object.
Version: opts.Args.Version,
n: &counter{1},
}
}
func NewMultiWithClient(opts pipeline.CommonOpts, client pipeline.Client) *ScribeMulti {
if opts.Args == nil {
opts.Args = &plumbing.PipelineArgs{}
}
return &ScribeMulti{
Client: client,
Opts: opts,
Log: opts.Log,
Collection: NewMultiCollection(),
n: &counter{1},
}
}
type MultiFunc func(*Scribe)
func MultiFuncWithLogging(logger logrus.FieldLogger, mf MultiFunc) MultiFunc {
return func(sw *Scribe) {
log := logger.WithFields(logrus.Fields{
"n": sw.n,
"pipeline": sw.pipeline,
})
log.Debugln("Populating the sub pipeline...")
mf(sw)
log.Debugln("Done populating sub pipeline")
}
}
// New creates a new Pipeline step that executes the provided MultiFunc onto a new `*Scribe` type, creating a DAG.
// Because this function returns a pipeline.Step[T], it can be used with the normal Scribe functions like `Run` and `Parallel`.
func (s *ScribeMulti) New(name string, mf MultiFunc) pipeline.Pipeline {
log := s.Log.WithFields(logrus.Fields{
"pipeline": name,
})
sw, err := s.newMulti(name)
if err != nil {
log.WithError(err).Fatalln("Failed to clone pipeline for use in multi-pipeline")
}
sw.Opts.Name = name
// This function adds the pipeline the way the user specified. It should look exactly like a normal scribe pipeline.
// This collection will be populated with a collection of Steps with actions.
wrapped := MultiFuncWithLogging(log, mf)
wrapped(sw)
// Update our counter with the new value of the sub-pipeline counter
s.n = sw.n
node, err := sw.Collection.Graph.Node(DefaultPipelineID)
if err != nil {
log.Fatal(err)
}
graph := node.Value.Graph
log.WithFields(logrus.Fields{
"nodes": len(graph.Nodes),
"edges": len(graph.Edges),
}).Debugln("Graph populated")
return pipeline.Pipeline{
Name: name,
Events: node.Value.Events,
ID: s.serial(),
Graph: node.Value.Graph,
}
}
func (s *ScribeMulti) newMulti(name string) (*Scribe, error) {
log := s.Log.WithField("pipeline", name)
collection := NewMultiCollection()
if err := collection.AddPipelines(pipeline.New(name, DefaultPipelineID)); err != nil {
return nil, err
}
sw := &Scribe{
Client: s.Client,
Opts: s.Opts,
Log: log,
Version: s.Version,
n: s.n,
Collection: collection,
pipeline: DefaultPipelineID,
}
return sw, nil
}