-
Notifications
You must be signed in to change notification settings - Fork 927
/
monitor.go
113 lines (93 loc) · 3.05 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"
"emperror.dev/errors"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/backgroundworkers"
"github.com/botlabs-gg/yagpdb/v2/premium/models"
"github.com/mediocregopher/radix/v3"
"github.com/volatiletech/sqlboiler/v4/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(radix.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(radix.Cmd(nil, "DEL", RedisKeyPremiumGuildsTmp)); err != nil {
return errors.WithMessage(err, "del tmp")
}
if err = common.RedisPool.Do(radix.Cmd(nil, "HMSET", rCmd...)); err != nil {
return errors.WithMessage(err, "hmset")
}
if err = common.RedisPool.Do(radix.Cmd(nil, "RENAME", RedisKeyPremiumGuildsTmp, RedisKeyPremiumGuilds)); err != nil {
return errors.WithMessage(err, "rename")
}
err = common.RedisPool.Do(radix.Cmd(nil, "ZADD", rCmdLastTimesPremium...))
return errors.WithMessage(err, "last_premium_times")
}