-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscheduler.go
400 lines (339 loc) · 10.6 KB
/
scheduler.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package main
import (
"context"
"fmt"
"github.com/go-co-op/gocron"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
)
const DEFAULT_REPORT_PREFIX = "repeat-"
const DEFAULT_ANY_EXIT_CODE = "any"
var ExecCommand = exec.Command
var ExecCommandContext = exec.CommandContext
type Scheduler struct {
Config *Config
DBStorage *DBStorage
GoCronScheduler *gocron.Scheduler
Pgid int
Timeout *time.Duration
DBDir, BaseDir, ResultsDir string
Tasks map[string]*SchedulerTask
DBOpsQueue *chan *InsertRecord
Stopped bool
}
type SchedulerTask struct {
Name string
RunEvery, Timeout time.Duration
Config Collection
Pgid int
Command string
Job *gocron.Job
BaseDir string
DBStorage *DBStorage
DBOpsQueue *chan *InsertRecord
Scheduler *Scheduler
}
var Tempdir = ioutil.TempDir
const DefaultOpsQueueSize = 100000000
func NewScheduler(configFilename string, timeout *time.Duration, baseDir, resultsDir, dbDir string) (*Scheduler, error) {
var scheduler Scheduler
var t time.Location
log.Infof("Loading collectors from configuration file: %s", configFilename)
config, err := NewConfigFromFile(configFilename)
if err != nil {
return nil, err
}
tempDir, err := Tempdir(baseDir, DEFAULT_REPORT_PREFIX)
if err != nil {
return nil, err
}
pgid, err := syscall.Getpgid(os.Getpid())
if err != nil {
return nil, err
}
opsQueue := make(chan *InsertRecord, DefaultOpsQueueSize)
scheduler.BaseDir = tempDir
scheduler.Pgid = pgid
scheduler.Config = config
scheduler.GoCronScheduler = gocron.NewScheduler(&t)
scheduler.ResultsDir = resultsDir
scheduler.Tasks = make(map[string]*SchedulerTask)
scheduler.DBDir = dbDir
scheduler.DBOpsQueue = &opsQueue
storage, err := NewDBStorage(scheduler.BaseDir)
if err != nil {
return nil, err
}
scheduler.DBStorage = storage
if timeout != nil {
log.Infof("Scheduler timeout set to: %f seconds", timeout.Seconds())
scheduler.Timeout = timeout
}
if err = scheduler.LoadTasks(); err != nil {
return nil, err
}
return &scheduler, nil
}
func (scheduler *Scheduler) LoadTasks() error {
var task *SchedulerTask
var err error
for name, collection := range scheduler.Config.Collections {
if task, err = NewSchedulerTask(name, collection, scheduler); err != nil {
return err
}
scheduler.Tasks[name] = task
}
return nil
}
func (scheduler *Scheduler) TarballReport() error {
reportFileName := filepath.Join(scheduler.ResultsDir,
fmt.Sprintf("%sreport-%s.tar.gz", DEFAULT_REPORT_PREFIX, time.Now().Format("2006-01-02-15-04")))
files, err := filepath.Glob(filepath.Join(scheduler.BaseDir, "/*"))
if err != nil {
return err
}
var filesToAppend []string
for _, file := range files {
if !strings.Contains(file, "db-journal") {
filesToAppend = append(filesToAppend, file)
}
}
log.Infof("Creating report tarball at: %s", reportFileName)
if err = CreateTarball(reportFileName, filesToAppend); err != nil {
return err
}
return nil
}
func (scheduler *Scheduler) Cleanup() error {
log.Info("Cleaning up resources")
scheduler.GoCronScheduler.Clear()
scheduler.GoCronScheduler.Stop()
scheduler.Stopped = true
close(*scheduler.DBOpsQueue)
if err := scheduler.TarballReport(); err != nil {
return err
}
log.Debugf("Removing base directory: %s", scheduler.BaseDir)
if err := os.RemoveAll(scheduler.BaseDir); err != nil {
return err
}
return nil
}
func (scheduler *Scheduler) HandleTimeout() {
timer := time.NewTimer(*scheduler.Timeout)
defer timer.Stop()
for range timer.C {
log.Infof("Scheduler timeout (%f) reached, cleaning up and killing process", scheduler.Timeout.Seconds())
if err := scheduler.Cleanup(); err != nil {
log.Error(err)
}
if err := syscall.Kill(-scheduler.Pgid, syscall.SIGKILL); err != nil {
log.Errorf("Error killing process group: %d", scheduler.Pgid)
os.Exit(-1)
}
}
}
func (scheduler *Scheduler) RemoveTask(name string) {
if task, ok := scheduler.Tasks[name]; !ok {
log.Debugf("Not found available task with name: %s in scheduler", name)
} else {
scheduler.GoCronScheduler.RemoveByReference(task.Job)
log.Debugf("Removed task name: %s from scheduler", name)
}
}
var WriteFile = ioutil.WriteFile
func (scheduler *Scheduler) RunTask(task *SchedulerTask) error {
var output []byte
var err error
if task.Config.RunOnce {
scheduler.RemoveTask(task.Name)
}
if task.Timeout > 0 {
output, err = RunWithTimeout(task)
} else {
output, err = RunWithoutTimeout(task)
}
if err != nil && !task.IsValidExitCode(err) {
errMsg := fmt.Errorf("Command for collector %s exited with exit code: %s - (not allowed by exit-codes config)",
task.Name, err.Error())
log.Error(errMsg)
return errMsg
}
switch task.Config.Store {
case "database":
{
return task.StoreResultsToDB(output)
}
case "file":
{
return task.StoreResultsToFile(output)
}
}
return nil
}
func (scheduler *Scheduler) WaitForRecordsToInsert(ch *chan *InsertRecord) {
var RecordsMap = make(map[string][]*InsertRecord)
for {
record := <-*ch
if record == nil {
continue
}
RecordsMap[record.TableName] = append(RecordsMap[record.TableName], record)
batchSize := scheduler.Config.Collections[record.TableName].BatchSize
log.Tracef("Records on table %s -- records: %d - batchsize: %d", record.TableName, len(RecordsMap[record.TableName]), batchSize)
if len(RecordsMap[record.TableName]) >= batchSize || scheduler.Stopped {
var dst strings.Builder
dst.WriteString("INSERT INTO ")
dst.WriteString("main." + record.TableName)
dst.WriteString(" (")
dst.WriteString(strings.Join(record.FieldNames, ", "))
dst.WriteString(") VALUES ")
for i, r := range RecordsMap[record.TableName] {
dst.WriteString("(" + strings.Join(r.Values, ", ") + ")")
if i == len(RecordsMap[record.TableName])-1 {
dst.WriteString(";")
} else {
dst.WriteString(",")
}
}
if err := scheduler.DBStorage.Exec(dst.String()).Error; err != nil {
log.Errorf("Error executing database query: %s", err)
}
log.Debugf("Remaining elements on channel to be processed: %d", len(*ch))
log.Tracef("Executed query: %s", dst.String())
RecordsMap[record.TableName] = make([]*InsertRecord, 0)
}
}
}
func (scheduler *Scheduler) Start() error {
for name, task := range scheduler.Tasks {
log.Infof("Scheduling run of %s collector every %f secs", name, task.RunEvery.Seconds())
job, err := scheduler.GoCronScheduler.Every(uint64(task.RunEvery.Seconds())).Seconds().StartImmediately().Do(scheduler.RunTask, task)
if err != nil {
return err
}
scheduler.Tasks[name].Job = job
}
scheduler.GoCronScheduler.StartAsync()
if *scheduler.Timeout > 0 {
go scheduler.HandleTimeout()
}
go scheduler.WaitForRecordsToInsert(scheduler.DBOpsQueue)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
for range c {
if err := scheduler.Cleanup(); err != nil {
log.Errorf("Error during the cleanup phase: %s", err)
}
os.Exit(1)
}
return nil
}
var TempFile = ioutil.TempFile
func NewSchedulerTask(name string, collection Collection, scheduler *Scheduler) (*SchedulerTask, error) {
var task SchedulerTask
var command string
runEvery, err := time.ParseDuration(collection.RunEvery)
if err != nil {
return nil, err
}
taskTimeout, err := time.ParseDuration(collection.Timeout)
if err != nil {
return nil, err
}
if collection.RunOnce && runEvery > 0 {
return nil, fmt.Errorf("task: %s must be defined as run-once or run-every, not both", name)
}
if collection.Script != "" {
fd, err := TempFile(scheduler.BaseDir, "run-script-")
if err != nil {
return nil, err
}
if err = fd.Chmod(0700); err != nil {
return nil, err
}
if _, err = fd.WriteString(collection.Script); err != nil {
return nil, err
}
fd.Close()
command = fd.Name()
} else {
command = collection.Command
}
task.BaseDir = scheduler.BaseDir
task.Command = command
task.Timeout = taskTimeout
task.RunEvery = runEvery
task.Config = collection
task.Name = name
task.Pgid = scheduler.Pgid
task.DBStorage = scheduler.DBStorage
task.DBOpsQueue = scheduler.DBOpsQueue
task.Scheduler = scheduler
return &task, nil
}
func (task *SchedulerTask) IsValidExitCode(err error) bool {
if task.Config.ExitCodes == DEFAULT_ANY_EXIT_CODE {
return true
}
if exitError, ok := err.(*exec.ExitError); ok {
for _, exitCode := range strings.Split(task.Config.ExitCodes, " ") {
code, _ := strconv.Atoi(exitCode)
if code == exitError.ExitCode() {
return true
}
}
}
return false
}
func (task *SchedulerTask) StoreResultsToDB(results []byte) error {
tableName := strings.ToLower(task.Name)
for _, line := range strings.Split(string(results), "\n") {
values := strings.Split(line, task.Config.Database.MapValues.Separator)
fields := task.Config.Database.MapValues.Fields
task.DBStorage.CreateTable(tableName, fields)
if err := task.DBStorage.CreateRecord(task, tableName, fields, values); err != nil {
return err
}
}
log.Infof("Command for collector %s, successfully ran, stored results into database, table: %s", task.Name, tableName)
return nil
}
func (task *SchedulerTask) StoreResultsToFile(results []byte) error {
outputFileName := filepath.Join(task.BaseDir, fmt.Sprintf("%s-%s", task.Name, time.Now().Format("2006-01-02-15:04:05")))
if err := WriteFile(outputFileName, results, 0750); err != nil {
log.Errorf("Error storing collection results for %s, on file: %s", task.Name, outputFileName)
return err
}
log.Infof("Command for collector %s, successfully ran, stored results into file: %s", task.Name, outputFileName)
return nil
}
func RunWithTimeout(task *SchedulerTask) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), task.Timeout)
defer cancel()
cmd := ExecCommandContext(ctx, "bash", "-c", task.Command)
cmd.Dir = task.BaseDir
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: task.Pgid}
log.Infof("Running command for collector %s", task.Name)
output, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
log.Warnf("Collector: %s, timed out after %f secs (cancelled)", task.Name, task.Timeout.Seconds())
return nil, nil
}
return output, err
}
func RunWithoutTimeout(task *SchedulerTask) ([]byte, error) {
cmd := ExecCommand("bash", "-c", task.Command)
cmd.Dir = task.BaseDir
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: task.Pgid}
log.Infof("Running command for collector %s", task.Name)
return cmd.CombinedOutput()
}