-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
115 lines (94 loc) · 2.11 KB
/
main.go
File metadata and controls
115 lines (94 loc) · 2.11 KB
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
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"sync"
"time"
_ "net/http/pprof"
discordwebhook "github.com/bensch777/discord-webhook-golang"
_ "github.com/joho/godotenv/autoload"
)
var includeFailed = 0
var statsHook = NewWebhookWorker(os.Getenv("STATS_WEBHOOK"))
var restrictHook = NewWebhookWorker(os.Getenv("RESTRICTED_WEBHOOK"))
func main() {
var wg sync.WaitGroup
rpsStr := os.Getenv("REQUESTS_PER_SECOND")
rps := 5
if rpsStr != "" {
parsed, err := strconv.Atoi(rpsStr)
if err == nil && parsed != 0 {
rps = parsed
}
}
if os.Getenv("INCLUDE_FAILED") == "true" {
includeFailed = 1
}
client = NewLimitedClient(rps)
InitDB(os.Getenv("POSTGRES_URL"))
defer DB.Close()
initCursor()
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
if os.Getenv("ENABLE_WEBHOOK") != "true" {
statsHook.Link = ""
restrictHook.Link = ""
}
statsHook.Start()
restrictHook.Start()
userUpdater.Workers(20)
userUpdater.Start()
loadUsers()
loadQueue()
fetchScores() // 4 * 1 Ratelimit -> 4 -> 604
go func() {
defer wg.Done()
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
for range ticker.C {
fetchScores()
}
}()
go func() {
now := time.Now()
nextHour := now.Truncate(time.Hour).Add(time.Hour)
time.Sleep(time.Until(nextHour))
ticker := time.NewTicker(time.Hour)
defer ticker.Stop()
for {
embed := discordwebhook.Embed{
Title: "Update Stats",
Color: 0x86DC3D,
Timestamp: time.Now(),
Footer: discordwebhook.Footer{
Text: fmt.Sprintf("Users tracked: %d", userCount),
},
Fields: []discordwebhook.Field{
{
Name: "Scores Stored",
Value: fmt.Sprintf("%d", scoreCount),
Inline: true,
},
{
Name: "Stats Updated",
Value: fmt.Sprintf("%d", statsCount),
Inline: true,
},
},
}
hook := discordwebhook.Hook{
Username: "Advance",
Avatar_url: "https://a.ppy.sh/9527931",
Embeds: []discordwebhook.Embed{embed},
}
statsHook.Queue(hook)
scoreCount = 0
statsCount = 0
<-ticker.C
}
}()
select {} // block forever (or start server)
}