/
scheduler.go
263 lines (242 loc) · 5.51 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
package scheduler
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"msg"
"os"
"time"
)
type Scheduler struct {
Stamp string // string formatting for printing time
events []chan Event
queueLock chan []int // index into events list, sorted by next event time
nextEvent chan Event
stopWaiting chan bool
}
func New() *Scheduler {
s := new(Scheduler)
s.Stamp = "Jan 2 2006 at 3:04:05PM"
s.nextEvent = make(chan Event)
s.queueLock = make(chan []int, 1)
s.queueLock <- []int{}
s.stopWaiting = make(chan bool)
return s
}
func (s *Scheduler) Pop() (Event, error) {
queue := <-s.queueLock
if len(queue) == 0 {
s.queueLock <- queue
return Event{}, errors.New("The events queue is empty")
}
e := <-s.events[queue[0]]
if len(queue) > 1 {
queue = queue[1:]
} else {
queue = []int{}
}
s.queueLock <- queue
return e, nil
}
func (s *Scheduler) Push(e Event) (int, error) {
if e.index != nil && *e.index >= 0 && *e.index < len(s.events) {
s.events[*e.index] <- e
} else {
e.index = new(int)
*e.index = len(s.events)
evnt := make(chan Event, 1)
evnt <- e
s.events = append(s.events, evnt)
}
queue := <-s.queueLock
queue = append(queue, *e.index)
s.queueLock <- queue
return *e.index, nil
}
func (s *Scheduler) InsertInOrder(e Event) (int, error) {
index, err := s.Push(e)
if err != nil {
fmt.Println(err)
return -1, nil
}
queue := <-s.queueLock
for i := len(queue) - 2; i >= 0; i-- {
evnt := <-s.events[queue[i]]
nextTime := evnt.NextTime
s.events[queue[i]] <- evnt
if e.NextTime.After(nextTime) {
s.queueLock <- queue
return index, nil
}
tmp := queue[i]
queue[i] = queue[i+1]
queue[i+1] = tmp
}
s.queueLock <- queue
return index, nil
}
func (s *Scheduler) GetNextTime() (t time.Time, err error) {
q := <-s.queueLock
if len(q) == 0 {
err = errors.New("No events in the queue")
} else {
e := <-s.events[q[0]]
t = e.NextTime
s.events[q[0]] <- e
}
s.queueLock <- q
return
}
func (s *Scheduler) feedNextEvent() {
for {
nextTime, err := s.GetNextTime()
if err != nil {
msg.Logln(" feeder) Didn't find an event. Sleeping.")
time.Sleep(time.Second)
continue
}
msg.Logf(" feeder) Next event on %v\n", nextTime.Format(s.Stamp))
timer := time.AfterFunc(nextTime.Sub(time.Now()), func() {
e, err := s.Pop()
if err == nil {
s.nextEvent <- e
s.stopWaiting <- true
}
})
<-s.stopWaiting
if timer.Stop() {
msg.Logln(" feeder) Interrupted while waiting for current event.")
} else {
msg.Logln(" feeder) Current event completed.")
}
}
}
func (s *Scheduler) ManageEventQueue() {
go s.feedNextEvent()
var err error
// main loop
for {
// wait for the next event
msg.Logln("\nmanager) Waiting for an event...")
event := <-s.nextEvent
msg.Logln("manager) Got one an event.")
// Run the scheduled event
if event.Action != nil {
err = event.Action()
if err != nil {
fmt.Println(err)
break
}
} else {
msg.Logln("Event had no action")
}
// Update the next time for this event
err = event.UpdateNextTime()
if err != nil {
fmt.Println(err)
break
}
// Put this event back on the queue
_, err = s.InsertInOrder(event)
if err != nil {
fmt.Println(err)
break
}
}
}
// Generate num random events. All events will fire at least once in num * 2
// seconds after starting the program, then they will have random days and weeks
// in the future where they will fire again. These can be used for anything from
// filling in a schedule as a template to testing the output system.
func (s *Scheduler) GenerateRandomEvents(num int, aux func(Event)) {
for i := 0; i < num; i++ {
// up to num*2 seconds in the future
dur := time.Second * time.Duration(rand.Int()%(num*2)+1)
nextT := time.Now().Add(dur)
// choose the days of the week to be applied
var days []bool
for j := 0; j < 7; j++ {
r := false
if rand.Int()%2 == 0 {
r = true
}
days = append(days, r)
}
// choose the weeks of the year to be applied
var weeks []bool
for j := 0; j < 53; j++ {
r := false
if rand.Int()%2 == 0 {
r = true
}
weeks = append(weeks, r)
}
e := Event{
NextTime: nextT,
RepeatDays: days,
RepeatWeeks: weeks,
}
index, err := s.InsertInOrder(e)
if err != nil {
fmt.Println(err)
break
}
e.index = new(int)
*e.index = index
if aux != nil {
aux(e)
}
}
}
// Save the current in-memory schedule to file as a json encoded object
// according to schema.json
func (s *Scheduler) SaveSchedule(file string, aux func(Event)) error {
var events []Event
for _, e := range s.events {
event := <-e
events = append(events, event)
e <- event
}
//bytes, err := json.Marshal(events)
bytes, err := json.MarshalIndent(events, "", "\t")
if err != nil {
fmt.Println("Marshal:", err)
return err
}
err = ioutil.WriteFile(file, bytes, os.FileMode(0664))
if err != nil {
fmt.Println("WriteOut:", err)
return err
}
return nil
}
// Load a schedule file into memory. file should be a json encoded file
// according to schema.json
func (s *Scheduler) LoadSchedule(file string, aux func(Event)) error {
fp, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println("ReadFile:", err)
return err
}
//log("%s\n", string(fp))
var events []Event
err = json.Unmarshal(fp, &events)
if err != nil {
fmt.Println("Unmarshal:", err)
return err
}
// Put all the events in
for _, e := range events {
*e.index, err = s.InsertInOrder(e)
if err != nil {
fmt.Println("Insert:", err)
return err
}
if aux != nil {
aux(e)
}
}
return nil
}