/
selina.go
167 lines (153 loc) · 3.77 KB
/
selina.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
package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/licaonfee/selina"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)
type PipeDefinition struct {
NodeDefs []GeneralOptions `yaml:"nodes"`
nodes []*selina.Node `yaml:"-"`
}
func layout(def *PipeDefinition) (selina.Pipeliner, error) {
nodes := make(map[string]*selina.Node, len(def.nodes))
var usefetch bool
for i := 0; i < len(def.nodes); i++ {
nodes[def.nodes[i].Name()] = def.nodes[i]
if len(def.NodeDefs[i].Fetch) > 0 {
usefetch = true
}
}
if !usefetch {
return selina.LinealPipeline(def.nodes...), nil
}
chained := make(map[string]struct{})
for _, d := range def.NodeDefs {
me := nodes[d.Name]
for _, f := range d.Fetch {
prev, ok := nodes[f]
if !ok {
return nil, errors.New("missing node")
}
prev.Chain(me)
chained[me.Name()] = struct{}{}
chained[prev.Name()] = struct{}{}
}
}
for _, d := range def.NodeDefs {
_, ok := chained[d.Name]
if !ok {
return nil, errors.New("not chained")
}
}
return selina.FreePipeline(def.nodes...), nil
}
func loadDefinition(definition io.Reader, availableNodes map[string]NewFacility) (*PipeDefinition, error) {
dec := yaml.NewDecoder(definition)
dec.SetStrict(true)
var defined PipeDefinition
if err := dec.Decode(&defined); err != nil {
return nil, fmt.Errorf("decode YAML %w", err)
}
pipeNodes := make([]*selina.Node, 0, len(defined.NodeDefs))
for _, n := range defined.NodeDefs {
facFunc, ok := availableNodes[n.Type]
if !ok {
return nil, errors.New("unavailable type")
}
facility := facFunc()
n.Args["read_format"] = n.ReadFormat
n.Args["write_format"] = n.WriteFormat
if err := mapstructure.Decode(n.Args, &facility); err != nil {
fmt.Printf("%v\n", n.Args)
return nil, fmt.Errorf("decode struct %w", err)
}
node, err := facility.Make(n.Name)
if err != nil {
return nil, err
}
pipeNodes = append(pipeNodes, node)
}
defined.nodes = pipeNodes
return &defined, nil
}
func createPipeline(defined *PipeDefinition) (selina.Pipeliner, error) {
p, err := layout(defined)
if err != nil {
return nil, err
}
return p, nil
}
func main() {
filename := flag.String("file", "", "pipeline definition file use - to stdin ")
timeout := flag.Duration("timeout", time.Duration(0), "maximum time to run, default limitless")
printSchema := flag.Bool("schema", false, "print jsonschema for yaml LSP")
log.SetFlags(log.Llongfile | log.LstdFlags)
flag.Parse()
var availableNodes = map[string]NewFacility{
"read_file": NewReadFile,
"write_file": NewWriteFile,
"sql_query": NewSQLQuery,
"sql_insert": NewSQLInsert,
"regex": NewRegexp,
"csv": NewCSV,
"cron": NewCron,
"remote": NewRemote,
"random": NewRandom,
"time_serie": NewTimeSerie,
}
if *printSchema {
fmt.Println(schema(availableNodes))
return
}
if *filename == "" {
log.Fatal("file is mandatory")
}
var fileStream io.Reader = os.Stdin
if *filename != "-" {
data, err := os.ReadFile(*filename)
if err != nil {
log.Fatal(err)
}
fileStream = bytes.NewBuffer(data)
}
def, err := loadDefinition(fileStream, availableNodes)
if err != nil {
log.Fatal(err)
}
p, err := createPipeline(def)
if err != nil {
log.Fatal(err)
}
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt, syscall.SIGTERM)
var ctx context.Context
var cancel context.CancelFunc
if *timeout == time.Duration(0) {
ctx, cancel = context.WithCancel(context.Background())
} else {
ctx, cancel = context.WithTimeout(context.Background(), *timeout)
}
go func() {
<-s
cancel()
}()
err = p.Run(ctx)
switch {
case err == nil:
case errors.Is(err, context.DeadlineExceeded):
log.Fatalf("timeout after %v", *timeout)
default:
log.Fatal(err)
}
}