-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.go
218 lines (176 loc) · 4.65 KB
/
runner.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
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"github.com/hibiken/asynq"
"github.com/rverton/pipers/db"
"github.com/rverton/pipers/pipe"
"github.com/rverton/pipers/queue"
log "github.com/sirupsen/logrus"
)
var SCHEDULER_SLEEP = time.Minute * 1
func runAsFile(p pipe.Pipe, client *asynq.Client, ds db.DataService) error {
logger := log.WithField("pipe", p.Name)
targets, err := ds.RetrieveTargets()
if err != nil {
return fmt.Errorf("retrieving targets failed")
}
interval, _ := p.Interval()
for _, target := range targets {
if !ds.ShouldRun(p.Name, target, interval) {
continue
}
rows, err := ds.RetrieveByTarget(p.Input.Table, p.Input.Filter, target)
if err != nil {
return fmt.Errorf("could not retrieve input: %v", err)
}
var data db.Data
tmpInputFile, err := ioutil.TempFile(os.TempDir(), "pipers-tmp-")
if err != nil {
return fmt.Errorf("could not create tmp file: %v", err)
}
count := 0
for rows.Next() {
err := rows.Scan(&data.Id, &data.Asset, &data.Target, &data.Data)
if err != nil {
return fmt.Errorf("retrieving pipe input data failed: %v", err)
}
tpl, err := pipe.Tpl(p.Input.AsFile, map[string]interface{}{
"input": pipe.MapInput(data),
})
if err != nil {
logger.WithField("error", err).Error("template for as_file failed")
continue
}
tmpInputFile.WriteString(tpl + "\n")
count++
}
tmpInputFile.Close()
if count <= 0 {
continue
}
// take last data object and put input filename in
newData := db.Data{
Target: data.Target,
Data: map[string]interface{}{
"as_file": tmpInputFile.Name(),
},
}
// enqueue task
if err := queue.EnqueuePipe(p, newData, client); err != nil {
if !errors.Is(err, asynq.ErrDuplicateTask) {
return fmt.Errorf("enqueuing failed: %v", err)
}
} else {
logger.WithFields(log.Fields{
"inputId": data.Id,
"target": target,
"lines": count,
}).Info("enqueued as_file")
// add task now to prevent this resource intensive operation
// to be run too often
// TODO: move to retriever and add a redis check for this task
ds.AddTask(db.Task{
Pipe: p.Name,
Ident: target,
})
}
}
return nil
}
func runSingle(p pipe.Pipe, client *asynq.Client, ds db.DataService) error {
interval, _ := p.Interval()
count := 0
countAdded := 0
logger := log.WithFields(log.Fields{"pipe": p.Name})
// retrieve data from file?
if p.Input.File != "" {
file, err := os.Open(p.Input.File)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if !ds.ShouldRun(p.Name, line, interval) {
continue
}
// enqueue task
data := db.Data{
Id: line,
Asset: line,
Target: filepath.Base(p.Input.File),
}
if err := queue.EnqueuePipe(p, data, client); err != nil {
if !errors.Is(err, asynq.ErrDuplicateTask) {
return fmt.Errorf("enqueueing failed: %v", err)
}
} else {
logger.WithField("ident", data.Id).Info("enqueued")
countAdded += 1
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
} else {
rows, err := ds.Retrieve(p.Input.Table, p.Name, p.Input.Filter, p.Input.Threshold, interval)
if err != nil {
return fmt.Errorf("could not retrieve input: %v", err)
}
for rows.Next() {
count++
data := db.Data{}
err := rows.Scan(&data.Id, &data.Asset, &data.Target, &data.Data)
if err != nil {
return fmt.Errorf("scanning pipe input failed: %v", err)
}
// enqueue task
if err := queue.EnqueuePipe(p, data, client); err != nil {
if !errors.Is(err, asynq.ErrDuplicateTask) {
return fmt.Errorf("enqueueing failed: %v", err)
}
} else {
logger.WithField("ident", data.Id).Info("enqueued")
countAdded += 1
}
}
}
logger.WithFields(log.Fields{
"enqueued": countAdded,
"skipped": count - countAdded,
}).Info("scheduler run")
return nil
}
// run will be executed for each pipe and is reponsible for
// scheduling tasks periodically
func run(p pipe.Pipe, client *asynq.Client, ds db.DataService, wg *sync.WaitGroup) {
defer wg.Done()
for {
// based on as_file, create a task with all results at once
// or a single task for each returned record
if p.Input.AsFile != "" {
if err := runAsFile(p, client, ds); err != nil {
log.WithFields(log.Fields{
"pipe": p.Name,
"error": err,
}).Error("running as file failed")
}
} else {
if err := runSingle(p, client, ds); err != nil {
log.WithFields(log.Fields{
"pipe": p.Name,
"error": err,
}).Error("run single failed")
}
}
time.Sleep(SCHEDULER_SLEEP)
}
}