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
200 lines (173 loc) · 5.69 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
package scribe
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/grafana/scribe/args"
"github.com/grafana/scribe/pipeline"
"github.com/grafana/scribe/pipeline/clients"
"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 clients.CommonOpts
Log logrus.FieldLogger
Version string
n *counter
pipeline int64
}
func (s *ScribeMulti) serial() int64 {
return s.n.Next()
}
// Add adds new pipelines to the Scribe DAG to be processed by the Client.
func (s *ScribeMulti) Add(pipelines ...pipeline.Pipeline) {
for _, v := range pipelines {
s.Log.WithFields(logrus.Fields{
"name": v.Name,
"requires": v.RequiredArgs,
"provides": v.ProvidedArgs,
}).Debugln("adding pipeline")
}
if err := s.Collection.AddPipelines(pipelines...); err != nil {
s.Log.WithError(err).Fatalln("error adding pipelines")
}
}
// 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 {
// Only worry about building an entire graph if we're not running a specific step.
if step := s.Opts.Args.Step; step == nil || (*step) == 0 {
rootArgs := pipeline.ClientProvidedArguments
if err := collection.BuildEdges(s.Opts.Log, rootArgs...); err != nil {
return err
}
}
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 {
rand.Seed(time.Now().Unix())
ctx := context.Background()
opts, err := parseOpts()
if err != nil {
panic(fmt.Sprintf("failed to parse arguments: %s", err.Error()))
}
sw := NewClient(ctx, 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 clients.CommonOpts, client pipeline.Client) *ScribeMulti {
rand.Seed(time.Now().Unix())
if opts.Args == nil {
opts.Args = &args.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.
wrappedMultiFunc := MultiFuncWithLogging(log, mf)
wrappedMultiFunc(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)
}
id := s.serial()
log.WithFields(logrus.Fields{
"nodes": len(node.Value.Graph.Nodes),
"requires": node.Value.RequiredArgs,
"provides": node.Value.RequiredArgs,
"name": name,
"id": id,
}).Debugln("Sub-pipeline created")
return pipeline.Pipeline{
ID: id,
Name: name,
Events: node.Value.Events,
Graph: node.Value.Graph,
Providers: node.Value.Providers,
Root: node.Value.Root,
RequiredArgs: node.Value.RequiredArgs,
ProvidedArgs: node.Value.ProvidedArgs,
}
}
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
}
func (s *ScribeMulti) PrintGraph(msg string) {
for _, v := range s.Collection.Graph.Nodes {
s.Log.WithFields(logrus.Fields{
"id": v.ID,
"name": v.Value.Name,
"steps": len(v.Value.Graph.Nodes),
"edges": len(v.Value.Graph.Edges),
"requires": v.Value.RequiredArgs,
"provides": v.Value.ProvidedArgs,
}).Debugln(msg)
}
}