-
Notifications
You must be signed in to change notification settings - Fork 2
/
ticker.go
69 lines (58 loc) · 1.19 KB
/
ticker.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
package main
import (
"log"
"time"
"github.com/wallix/awless-scheduler/model"
)
type ticker struct {
frequency time.Duration
store store
tick *time.Ticker
}
func newTicker(store store, dur time.Duration) *ticker {
t := &ticker{frequency: dur, store: store}
t.tick = time.NewTicker(t.frequency)
return t
}
func (t *ticker) start() {
for {
select {
case <-t.tick.C:
if *debug {
log.Println("tick")
}
executables := t.retrieveExecutableTasks()
for _, s := range executables {
d, err := driversFunc(s.Region)
if err != nil {
log.Println(err)
continue
}
evt := &event{tk: s}
evt.tpl, evt.err = executeTask(s, d, defaultCompileEnv)
eventc <- evt
}
}
}
}
func (t *ticker) stop() {
t.tick.Stop()
}
func (t *ticker) retrieveExecutableTasks() []*model.Task {
tasks, err := t.store.GetTasks()
if err != nil {
log.Println(err)
}
var executables []*model.Task
for _, tk := range tasks {
if isExecutable(tk) {
executables = append(executables, tk)
}
}
return executables
}
func isExecutable(tk *model.Task) bool {
now := time.Now().UTC()
limit := now.Add(stillExecutable)
return tk.RunAt.After(limit) && now.After(tk.RunAt)
}