-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
176 lines (155 loc) · 3.57 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
package main
import (
"container/heap"
"sync"
"time"
"github.com/sirupsen/logrus"
)
const (
defaultSchedulingCapacity = 10
periodicStats = false
periodicStatsDuration = time.Second * time.Duration(60)
activationDuration = time.Millisecond * time.Duration(1)
)
type Scheduler struct {
log *logrus.Entry
allitems map[Scheduable]*SchedulerData
dsched ScheduleHeap
updates chan SchedUpdate
shutdown chan bool
stats chan bool
wg sync.WaitGroup
}
func NewScheduler(log *logrus.Logger) *Scheduler {
sched := new(Scheduler)
sched.log = log.WithField("module", "scheduler")
sched.allitems = make(map[Scheduable]*SchedulerData)
sched.dsched = make(ScheduleHeap, 0, defaultSchedulingCapacity)
sched.updates = make(chan SchedUpdate)
sched.shutdown = make(chan bool)
sched.stats = make(chan bool)
heap.Init(&sched.dsched)
return sched
}
func (s *Scheduler) firstTime() time.Time {
if s.dsched.Len() == 0 {
return time.Now()
}
return s.dsched[0].when
}
func (s *Scheduler) add(d Scheduable, when time.Time) {
s.log.Debugf("Request to add event for %v (%v)", when, when.Sub(time.Now()))
if _, ok := s.allitems[d]; !ok {
sdata := new(SchedulerData)
sdata.when = when
sdata.object = d
heap.Push(&s.dsched, sdata)
s.allitems[d] = sdata
}
}
func (s *Scheduler) remove(d Scheduable) {
s.log.Debugf("Request to remove event")
if sdata, ok := s.allitems[d]; ok {
heap.Remove(&s.dsched, sdata.index)
delete(s.allitems, d)
}
}
func (s *Scheduler) printStats() {
if periodicStats {
s.log.Infof("Heap Length is %d", len(s.dsched))
s.log.Infof("Map Length is %d", len(s.allitems))
} else {
s.log.Debugf("Heap Length is %d", len(s.dsched))
s.log.Debugf("Map Length is %d", len(s.allitems))
}
}
func (s *Scheduler) runtime() {
defer s.wg.Done()
s.log.Info("Runtime started")
for {
s.printStats()
if s.dsched.Len() > 0 {
// Wait on current items
s.log.Debug("Waiting for event times")
select {
case update := <-s.updates:
switch update.event {
case SchedUpdateTypeAdd:
s.add(update.d, update.when)
case SchedUpdateTypeRem:
s.remove(update.d)
}
case <-time.After(time.Until(s.firstTime())):
if time.Now().Sub(s.firstTime()) < activationDuration {
s.log.Debug("Acting on scheduler event")
sdata := heap.Pop(&s.dsched).(*SchedulerData)
delete(s.allitems, sdata.object)
sdata.object.SchedulerAction()
}
case <-s.shutdown:
return
case <-s.stats:
}
} else {
// No items currently
s.log.Debug("Waiting for add or remove event")
select {
case update := <-s.updates:
switch update.event {
case SchedUpdateTypeAdd:
s.add(update.d, update.when)
case SchedUpdateTypeRem:
s.remove(update.d)
}
case <-s.shutdown:
return
case <-s.stats:
}
}
}
}
func (s *Scheduler) monitor() {
defer s.wg.Done()
s.log.Info("Monitor started")
if periodicStats {
for {
select {
case <-s.shutdown:
return
case <-time.After(periodicStatsDuration):
s.stats <- true
}
}
} else {
select {
case <-s.shutdown:
return
}
}
}
func (s *Scheduler) Start() {
s.wg.Add(2)
go s.runtime()
go s.monitor()
}
func (s *Scheduler) Stop() {
s.shutdown <- true
s.shutdown <- true
s.wg.Wait()
}
func (s *Scheduler) Add(d Scheduable, when time.Duration) {
s.updates <- SchedUpdate{
event: SchedUpdateTypeAdd,
d: d,
when: time.Now().Add(when),
}
}
func (s *Scheduler) Cancel(d Scheduable) {
s.updates <- SchedUpdate{
event: SchedUpdateTypeRem,
d: d,
}
}
func (s *Scheduler) QueueLen() int {
return len(s.dsched)
}