/
loader.go
99 lines (78 loc) · 1.93 KB
/
loader.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
package api
import (
"context"
"strings"
"time"
"github.com/YaleSpinup/minion/jobs"
log "github.com/sirupsen/logrus"
)
func (l *loader) start(ctx context.Context) error {
log.Infof("%s: loader starting", l.id)
// run the load for the first time which also blocks return until the cache is fresh
if err := l.run(ctx); err != nil {
return nil
}
go l.loop(ctx)
log.Infof("%s: loader started", l.id)
return nil
}
func (l *loader) loop(ctx context.Context) {
ticker := time.NewTicker(l.refreshInterval)
for {
log.Debug("starting loader loop")
select {
case <-ticker.C:
err := l.run(ctx)
if err != nil {
log.Errorf("error executing job refresh: %s", err)
}
case <-ctx.Done():
log.Debug("shutting down loader timer")
ticker.Stop()
return
}
log.Debug("ending loader loop")
}
}
func (l *loader) run(ctx context.Context) error {
defer timeTrack("loader.run()", time.Now())
log.Infof("%s running jobs loader", l.id)
l.jobsCache.Mux.Lock()
defer l.jobsCache.Mux.Unlock()
cache := make(map[string]*jobs.Job)
for name := range l.accounts {
jobs, err := l.jobsRepository.List(ctx, name, "")
if err != nil {
return err
}
log.Debugf("list of jobs: %+v", jobs)
for _, j := range jobs {
var id, group string
if split := strings.SplitN(j, "/", 2); len(split) == 1 {
id = split[0]
} else {
group = split[0]
id = split[1]
}
job, err := l.jobsRepository.Get(ctx, name, group, id)
if err != nil {
log.Errorf("error getting details about job '%s': %s", j, err)
continue
}
if !job.Enabled {
log.Infof("job '%s' is disabled, not caching", id)
continue
}
log.Debugf("caching job id %s with details: %+v", j, job)
l.jobsCache.Cache[j] = job
cache[j] = job
}
}
for k := range l.jobsCache.Cache {
if _, ok := cache[k]; !ok {
delete(l.jobsCache.Cache, k)
}
}
log.Infof("%s done loading %d jobs", l.id, len(l.jobsCache.Cache))
return nil
}