-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
103 lines (78 loc) · 1.92 KB
/
timer.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
package main
import (
"fmt"
"log"
"oengus-timers/sql"
"oengus-timers/structs"
"oengus-timers/utils"
"os"
"os/signal"
"strconv"
"time"
)
func StartTimers() {
// Run everything now!
timerCallback()
timerInterval, err := strconv.Atoi(utils.GetEnv("TIMER_INTERVAL_MINUTES", "5"))
if err != nil {
panic(err)
}
// tick every 5 minutes to check for updates
ticker := time.NewTicker(time.Duration(timerInterval) * time.Minute)
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
func() {
for {
select {
case <-ticker.C:
// Run everything on timer tick
timerCallback()
case <-sigint:
ticker.Stop()
return
}
}
}()
}
func timerCallback() {
go CheckSubmissionOpenClose()
}
func CheckSubmissionOpenClose() {
marathonsToOpen, err1 := sql.FindMarathonsToOpenSubmissions()
if err1 != nil {
fmt.Fprintf(os.Stderr, "(FindMarathonsToOpenSubmissions) QueryRow failed: %v\n", err1)
return
}
openMarathonSubmissions(marathonsToOpen)
marathonsToClose, err2 := sql.FindMarathonsToCloseSubmissions()
if err2 != nil {
fmt.Fprintf(os.Stderr, "(FindMarathonsToCloseSubmissions) QueryRow failed: %v\n", err2)
return
}
closeMarathonSubmissions(marathonsToClose)
}
func extractIds(marathons []structs.Marathon) []string {
v := make([]string, 0, len(marathons))
for _, marathon := range marathons {
v = append(v, marathon.Id)
}
return v
}
func openMarathonSubmissions(marathons []structs.Marathon) {
if len(marathons) == 0 {
log.Println("No submissions to open!")
return
}
marathonIds := extractIds(marathons)
log.Println("Opening submissions for", marathonIds)
sql.OpenSubmission(marathonIds)
}
func closeMarathonSubmissions(marathons []structs.Marathon) {
if len(marathons) == 0 {
log.Println("No submissions to close!")
return
}
marathonIds := extractIds(marathons)
log.Println("Closing submissions for", marathonIds)
sql.CloseSubmission(marathonIds)
}