-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogQueueJob.go
239 lines (195 loc) · 5.12 KB
/
logQueueJob.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
// Copyright (c) PavedRoad. All rights reserved.
// Licensed under the Apache2. See LICENSE file in the project root
// for full license information.
//
package main
import (
"encoding/json"
"fmt"
"log"
"net/url"
"time"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/pavedroad-io/eventbridge/s3"
"github.com/pavedroad-io/go-core/logger"
)
const (
//LogQueueJobType is type of Job from scheduler
LogQueueJobType string = "io.pavedraod.eventbridge.logQueueJob"
)
type logQueueJob struct {
JobID uuid.UUID `json:"jobID"`
Payload []byte `json:"payload"`
JobType string `json:"jobType"`
customers s3.Customer `json:"customers"`
s3Client *minio.Client `json:"s3Client"`
schedulerJobChan chan Job `json:"schedulerJobChan"`
// TODO: FIX to errors or custom errors
jobErrors []string `json:"jobErrors"`
Stats logQueueStats `json:"stats"`
}
type logQueueStats struct {
RequestTimedOut bool
RequestStartTime time.Time
RequestTime time.Duration
}
// Process methods
func (j *logQueueJob) ID() string {
return j.JobID.String()
}
func (j *logQueueJob) Type() string {
return LogQueueJobType
}
func (j *logQueueJob) InitWithJobChan(job chan Job) error {
j.schedulerJobChan = job
return j.Init()
}
func (j *logQueueJob) Init() error {
// Generate UUID
j.JobID = uuid.New()
// Set job type
j.JobType = LogQueueJobType
return nil
}
func (j *logQueueJob) Run() (result Result, err error) {
c := s3.Customer{}
var customers []s3.Customer
var eConf Environment
eConf.get()
if eConf.LoadFrom == "disk" {
customers, err = c.LoadFromDisk("customer.yaml")
if err != nil {
log.Fatalf("fail loading customer.yaml: %v\n", err)
}
} else {
// Load from network
customers, err = c.LoadFromNetwork(eConf.EventBridgeConfigURL)
if err != nil {
log.Fatalf("fail loading customer.yaml: %v\n", err)
}
fmt.Printf("Found %d customers\n", len(customers))
}
opts := minio.ListObjectsOptions{
Recursive: true,
Prefix: "",
}
var logQueue []s3.LogQueueItem
var plogs s3.ProcessedLogs
for _, c := range customers {
// Load a list of previously processed logs
// For now ignore error if not found
pconf := s3.LogConfig{
LoadFrom: eConf.LoadFrom,
LoadURL: eConf.EventBridgePlogsURL,
CustID: c.ID.String(),
PlogConfigID: c.Configuration.PlogConfigID,
}
if err := plogs.Load(pconf); err != nil {
log.Printf("Failed to load past processed logs: %v\n", err)
fmt.Printf("Failed to load past processed logs: %v\n", err)
}
// Build a list of providers the customer
// uses
plist := c.Providers
// Actually buckets not logs
for i, l := range c.Logs {
p, err := plist.Lookup(l.Provider)
if err != nil {
log.Printf("Provider not found: %v\n", err)
continue
}
s3Client, err := s3.NewClient(p)
if err != nil {
log.Fatalln(err)
}
objects, err := s3.ListBucketObjects(s3Client, l.Name, opts)
if err != nil {
log.Fatalln(err)
}
for _, o := range objects {
// See it is already processed
//
if plogs.Processed(l.Name, o.Key) {
continue
}
/// create new stats object
j.Stats.RequestStartTime = time.Now()
f, err := s3.GetObject(s3Client, l.Name, o.Key, minio.GetObjectOptions{})
if err != nil {
j.Stats.RequestTimedOut = true
log.Fatalln(err)
}
c.Configuration.Hook.Host = eConf.EventBridgePostHost
item := s3.LogQueueItem{
ID: c.ID.String(),
Bucket: l.Name,
Webhook: c.Configuration.Hook,
Filter: l.FilterEvents,
Name: o.Key,
Created: time.Now(),
Location: f,
LogFormat: c.Logs[i].LogFormat,
Processed: false,
PlogConfigID: c.Configuration.PlogConfigID,
Prune: c.Logs[i].PruneAfterProcessing,
}
// send item to kafka
jitem, _ := json.Marshal(item)
logger.Printf(string(jitem))
// Write new Job to dispatcher Job
// Channel
nj := &logProcessorJob{}
nj.Init()
nj.Log = item
j.Stats.RequestTime = time.Now().Sub(j.Stats.RequestStartTime)
j.schedulerJobChan <- nj
logQueue = append(logQueue, item)
}
}
/*
if eConf.LoadFrom == s3.NETWORK {
fmt.Println("plots save: ", plogs)
if err := plogs.SaveToNetwork(pconf); err != nil {
return nil, err
}
}
*/
}
payload, err := json.Marshal(logQueue)
if err != nil {
fmt.Errorf("Error %v\n", err)
return nil, err
}
// metaData: md,
jd, err := json.Marshal(j)
if err != nil {
fmt.Println(err)
}
jrsp := &logResult{job: jd,
jobType: j.Type(),
payload: payload}
return jrsp, nil
}
func (j *logQueueJob) newJob(url url.URL) logQueueJob {
newJob := logQueueJob{}
// Set type and ID and http.Client
newJob.Init()
return newJob
}
func (j *logQueueJob) Pause() (status string, err error) {
return "paused", nil
}
func (j *logQueueJob) Shutdown() error {
return nil
}
func (j *logQueueJob) Errors() []error {
return nil
}
func (j *logQueueJob) Metrics() []byte {
jblob, err := json.Marshal(j.Stats)
if err != nil {
return []byte("Marshal metrics failed")
}
return jblob
}