/
ReadFile.go
executable file
·144 lines (123 loc) · 3.91 KB
/
ReadFile.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
// Package files is generated by Handlergenerator tooling
// Make sure to insert real Description here
package files
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/percybolmer/go4data/handlers"
"github.com/percybolmer/go4data/metric"
"github.com/percybolmer/go4data/payload"
"github.com/percybolmer/go4data/property"
"github.com/percybolmer/go4data/pubsub"
"github.com/percybolmer/go4data/register"
)
// ReadFile is used to ReadFiles data
type ReadFile struct {
// Cfg is values needed to properly run the Handle func
Cfg *property.Configuration `json:"configs" yaml:"configs"`
Name string `json:"handler" yaml:"handler_name"`
remove bool
subscriptionless bool
errChan chan error
metrics metric.Provider
metricPrefix string
// MetricPayloadOut is how many payloads the processor has outputted
MetricPayloadOut string
// MetricPayloadIn is how many payloads the processor has inputted
MetricPayloadIn string
}
func init() {
register.Register("ReadFile", NewReadFileHandler)
}
// NewReadFileHandler generates a new ReadFile Handler
func NewReadFileHandler() handlers.Handler {
act := &ReadFile{
Cfg: &property.Configuration{
Properties: make([]*property.Property, 0),
},
Name: "ReadFile",
errChan: make(chan error, 1000),
metrics: metric.NewPrometheusProvider(),
}
act.Cfg.AddProperty("remove_after", "This property is used to configure if files that are read should be removed after", true)
return act
}
// GetHandlerName is used to retrun a unqiue string name
func (a *ReadFile) GetHandlerName() string {
return a.Name
}
// Handle is used to Read the content of a file from the former payload
// Expects a filepath in the input payload
func (a *ReadFile) Handle(ctx context.Context, input payload.Payload, topics ...string) error {
a.metrics.IncrementMetric(a.MetricPayloadIn, 1)
path := string(input.GetPayload())
file, err := os.Open(path)
if err != nil {
return err
}
defer func() {
file.Close()
if a.remove {
os.Remove(path)
}
}()
data, err := ioutil.ReadAll(file)
if err != nil {
return err
}
a.metrics.IncrementMetric(a.MetricPayloadOut, 1)
errs := pubsub.PublishTopics(topics, payload.NewBasePayload(data, file.Name(), input.GetMetaData()))
for _, err := range errs {
a.errChan <- err
}
return nil
}
// ValidateConfiguration is used to see that all needed configurations are assigned before starting
func (a *ReadFile) ValidateConfiguration() (bool, []string) {
// Check if Cfgs are there as needed
removeProp := a.Cfg.GetProperty("remove_after")
missing := make([]string, 0)
if removeProp == nil || removeProp.Value == nil {
missing = append(missing, "remove_after")
return false, missing
}
remove, err := removeProp.Bool()
if err != nil {
return false, nil
}
a.remove = remove
return true, nil
}
// GetConfiguration will return the CFG for the Handler
func (a *ReadFile) GetConfiguration() *property.Configuration {
return a.Cfg
}
// Subscriptionless will return true/false if the Handler is genereating payloads itself
func (a *ReadFile) Subscriptionless() bool {
return a.subscriptionless
}
// GetErrorChannel will return a channel that the Handler can output eventual errors onto
func (a *ReadFile) GetErrorChannel() chan error {
return a.errChan
}
// SetMetricProvider is used to change what metrics provider is used by the handler
func (a *ReadFile) SetMetricProvider(p metric.Provider, prefix string) error {
a.metrics = p
a.metricPrefix = prefix
a.MetricPayloadIn = fmt.Sprintf("%s_payloads_in", prefix)
a.MetricPayloadOut = fmt.Sprintf("%s_payloads_out", prefix)
err := a.metrics.AddMetric(&metric.Metric{
Name: a.MetricPayloadOut,
Description: "keeps track of how many payloads the handler has outputted",
})
if err != nil {
return err
}
err = a.metrics.AddMetric(&metric.Metric{
Name: a.MetricPayloadIn,
Description: "keeps track of how many payloads the handler has ingested",
})
return err
}