-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (96 loc) · 2.75 KB
/
main.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
114
115
116
117
118
package main
import (
"context"
"fmt"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"github.com/SevenTV/ServerGo/src/configure"
"github.com/SevenTV/ServerGo/src/discord"
"github.com/SevenTV/ServerGo/src/mongo"
"github.com/SevenTV/ServerGo/src/mongo/cache"
"github.com/SevenTV/ServerGo/src/mongo/datastructure"
_ "github.com/SevenTV/ServerGo/src/redis"
"github.com/SevenTV/ServerGo/src/server"
api_websocket "github.com/SevenTV/ServerGo/src/server/api/v2/websocket"
)
func init() {
log.Infoln("Application Starting...")
}
func main() {
configCode := configure.Config.GetInt("exit_code")
if configCode > 125 || configCode < 0 {
log.Warnf("Invalid exit code specified in config (%v), using 0 as new exit code.", configCode)
configCode = 0
}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
s := server.New()
go func() {
sig := <-c
log.Infof("sig=%v, gracefully shutting down...", sig)
start := time.Now().UnixNano()
// Run pre-shutdown cleanup
Cleanup()
wg := sync.WaitGroup{}
wg.Wait()
wg.Add(1)
go func() {
defer wg.Done()
if err := s.Shutdown(); err != nil {
log.Errorf("failed to shutdown server, err=%v", err)
}
}()
wg.Wait()
log.Infof("Shutdown took, %.2fms", float64(time.Now().UnixNano()-start)/10e5)
os.Exit(configCode)
}()
go func() {
for {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Debugf("Alloc = %vM\tTotalAlloc = %vM\tSys = %vM\tNumGC = %v", m.Alloc/1024/1024, m.TotalAlloc/1024/1024, m.Sys/1024/1024, m.NumGC)
time.Sleep(5 * time.Second)
}
}()
log.Infoln("Application Started.")
// Get and cache roles
roles, err := GetAllRoles(context.Background())
if err != nil {
log.Errorf("could not get roles, %s", err)
}
log.Infof("Retrieved %s roles", fmt.Sprint(len(roles)))
select {}
}
func Cleanup() {
// Remove websocket connections from Redis
log.Infof("<WebSocket> Closing %d connections", len(api_websocket.Connections))
for _, conn := range api_websocket.Connections {
conn.Unregister(context.Background())
}
// Logout from discord
_ = discord.Discord.CloseWithCode(1000)
}
// Get all roles available and cache into the mongo context
func GetAllRoles(ctx context.Context) ([]datastructure.Role, error) {
roles := []datastructure.Role{}
cur, err := mongo.Database.Collection("roles").Find(ctx, bson.M{})
if err != nil {
return nil, err
}
roles = append(roles, *datastructure.DefaultRole) // Add default role
if err := cur.All(ctx, &roles); err != nil { // Fetch roles
if err == mongo.ErrNoDocuments {
return roles, nil
}
return nil, err
}
// Set "AllRoles" value to mongo context
cache.CachedRoles = roles
return roles, nil
}