-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflektor.go
97 lines (77 loc) · 1.88 KB
/
reflektor.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
package reflektor
import (
"compress/flate"
"fmt"
"github.com/mholt/archiver"
log "github.com/sirupsen/logrus"
"gopkg.in/robfig/cron.v2"
"time"
)
// Reflektor holds an instance of the cron scheduler and the collection jobs
type Reflektor struct {
Cron *cron.Cron
Jobs []*Job
}
// ScheduleJobs adds each job to the cron scheduler and starts the cron service
func (r *Reflektor) ScheduleJobs() {
r.Cron = cron.New()
for _, job := range r.Jobs {
j := job
id, err := r.Cron.AddFunc(job.Schedule, func() {
r.RunJob(j)
})
if err != nil {
log.WithFields(log.Fields{"job": j.Name, "error": err}).Fatal("unable to schedule job")
continue
}
job.ID = id
}
r.Cron.Start()
for _, job := range r.Jobs {
entity := r.Cron.Entry(job.ID)
if !job.ArchiveExists() {
go r.RunJob(job)
continue
}
log.WithFields(log.Fields{
"job": job.Name,
"next_run": entity.Next,
}).Info("job scheduled")
}
}
// RunJob archives the given job's source directory
func (r *Reflektor) RunJob(job *Job) {
if !job.SourceExists() {
log.WithFields(log.Fields{
"job": job.Name,
"source": job.SourcePath,
}).Error("unable to find job source path")
return
}
log.WithField("job", job.Name).Info("job running")
start := time.Now()
z := archiver.TarGz{
CompressionLevel: flate.BestCompression,
Tar: &archiver.Tar{
OverwriteExisting: true,
ImplicitTopLevelFolder: true,
MkdirAll: true,
ContinueOnError: true,
},
}
err := z.Archive([]string{job.SourcePath}, job.ArchivePath())
if err != nil {
log.WithFields(log.Fields{
"job": job.Name,
"error": err,
}).Info("job failed")
return
}
elapsed := time.Since(start).Seconds()
cronEntity := r.Cron.Entry(job.ID)
log.WithFields(log.Fields{
"job": job.Name,
"elapsed": fmt.Sprintf("%.2fs", elapsed),
"next_run": cronEntity.Next,
}).Info("job finished")
}