-
Notifications
You must be signed in to change notification settings - Fork 927
/
monitor.go
113 lines (93 loc) · 3.08 KB
/
monitor.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
package premium
import (
"context"
"strconv"
"sync"
"time"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/backgroundworkers"
"github.com/jonas747/yagpdb/premium/models"
"github.com/pkg/errors"
"github.com/volatiletech/sqlboiler/queries/qm"
)
var _ backgroundworkers.BackgroundWorkerPlugin = (*Plugin)(nil)
func (p *Plugin) RunBackgroundWorker() {
go runMonitor()
}
func (p *Plugin) StopBackgroundWorker(wg *sync.WaitGroup) {
wg.Done()
}
func runMonitor() {
ticker := time.NewTicker(time.Second * 30)
time.Sleep(time.Second * 3)
logger.Info("started premium server monitor")
err := checkExpiredSlots(context.Background())
if err != nil {
logger.WithError(err).Error("Failed checking for expired premium slots")
}
checkedExpiredSlots := false
for {
<-ticker.C
if checkedExpiredSlots {
err := updatePremiumServers(context.Background())
if err != nil {
logger.WithError(err).Error("Failed updating premium servers")
}
checkedExpiredSlots = false
} else {
err := checkExpiredSlots(context.Background())
if err != nil {
logger.WithError(err).Error("Failed checking for expired premium slots")
}
checkedExpiredSlots = true
}
}
}
// Updates ALL premiun slots from ALL sources
func checkExpiredSlots(ctx context.Context) error {
timedSlots, err := models.PremiumSlots(qm.Where("permanent = false"), qm.Where("guild_id IS NOT NULL")).AllG(ctx)
if err != nil {
return errors.WithMessage(err, "models.PremiumSlots")
}
for _, v := range timedSlots {
if SlotDurationLeft(v) <= 0 {
err := SlotExpired(ctx, v)
if err != nil {
logger.WithError(err).WithField("slot", v.ID).Error("Failed expiring premium slot")
}
}
}
return nil
}
func updatePremiumServers(ctx context.Context) error {
slots, err := models.PremiumSlots(qm.Where("guild_id IS NOT NULL")).AllG(ctx)
if err != nil {
return errors.WithMessage(err, "models.PremiumSlots")
}
if len(slots) < 1 {
// Fast path
err = common.RedisPool.Do(retryableredis.Cmd(nil, "DEL", RedisKeyPremiumGuilds))
return errors.WithMessage(err, "do.Del")
}
rCmd := []string{RedisKeyPremiumGuildsTmp}
rCmdLastTimesPremium := []string{RedisKeyPremiumGuildLastActive}
now := strconv.FormatInt(time.Now().Unix(), 10)
for _, slot := range slots {
strGID := strconv.FormatInt(slot.GuildID.Int64, 10)
strUserID := strconv.FormatInt(slot.UserID, 10)
rCmd = append(rCmd, strGID, strUserID)
rCmdLastTimesPremium = append(rCmdLastTimesPremium, now, strGID)
}
if err = common.RedisPool.Do(retryableredis.Cmd(nil, "DEL", RedisKeyPremiumGuildsTmp)); err != nil {
return errors.WithMessage(err, "del tmp")
}
if err = common.RedisPool.Do(retryableredis.Cmd(nil, "HMSET", rCmd...)); err != nil {
return errors.WithMessage(err, "hmset")
}
if err = common.RedisPool.Do(retryableredis.Cmd(nil, "RENAME", RedisKeyPremiumGuildsTmp, RedisKeyPremiumGuilds)); err != nil {
return errors.WithMessage(err, "rename")
}
err = common.RedisPool.Do(retryableredis.Cmd(nil, "ZADD", rCmdLastTimesPremium...))
return errors.WithMessage(err, "last_premium_times")
}