forked from jheuel/pollrBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollservices.go
91 lines (81 loc) · 1.97 KB
/
pollservices.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
package main
import (
"time"
"k8s.io/klog"
_ "github.com/semog/go-sqldb"
)
func checkAndUpdatePolls(st Store) {
autoClosePolls(st)
autoResetPolls(st)
autoOpenPolls(st)
}
func autoClosePolls(st Store) {
now := time.Now().Unix()
polls, err := st.GetPollsWithCloseAtBefore(now)
if err != nil {
klog.Errorf("could not get polls with close at: %v", err)
return
}
for _, p := range polls {
klog.Infof("Closing poll %d automatically", p.ID)
p.Inactive = inactive
if p.CloseEvery != "" {
// Calculate the new CloseAt and update the poll
p.CloseAt = calcNextServiceTime(now, p.CloseEvery)
}
_, err = st.SavePoll(p)
if err != nil {
klog.Errorf("could not auto close poll: %v", err)
continue
}
pollsToUpdate.enqueue(p.ID)
}
}
func autoResetPolls(st Store) {
now := time.Now().Unix()
polls, err := st.GetPollsWithResetAtBefore(now)
if err != nil {
klog.Errorf("could not get polls with reset at: %v", err)
return
}
for _, p := range polls {
klog.Infof("Resetting poll %d automatically", p.ID)
err = st.ResetPoll(p.UserID, p.ID)
if err != nil {
klog.Errorf("could not reset poll: %v", err)
continue
}
if p.ResetEvery != "" {
// Calculate the new ResetAt and update the poll
p.ResetAt = calcNextServiceTime(now, p.ResetEvery)
} else {
p.ResetAt = 0
}
_, err = st.SavePoll(p)
if err != nil {
klog.Errorf("could not save poll: %v", err)
}
pollsToUpdate.enqueue(p.ID)
}
}
func autoOpenPolls(st Store) {
now := time.Now().Unix()
polls, err := st.GetPollsWithOpenAtBefore(now)
if err != nil {
klog.Errorf("could not get polls with open at: %v", err)
return
}
for _, p := range polls {
klog.Infof("Opening poll %d automatically", p.ID)
p.Inactive = open
if p.OpenEvery != "" {
// Calculate the new OpenAt and update the poll
p.OpenAt = calcNextServiceTime(now, p.OpenEvery)
}
_, err = st.SavePoll(p)
if err != nil {
klog.Errorf("could not auto open poll: %v", err)
}
pollsToUpdate.enqueue(p.ID)
}
}