/
shutdown.go
233 lines (197 loc) · 6.2 KB
/
shutdown.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"context"
"runtime"
"strconv"
"time"
"github.com/tevino/abool"
)
var (
shuttingDown = abool.New()
blockAllIncomingMessages = abool.New()
datetimeShutdownInit time.Time
)
func shutdown(ctx context.Context) {
shuttingDown.Set()
datetimeShutdownInit = time.Now()
// Since this is a function that changes a user's relationship to tables,
// we must acquires the tables lock to prevent race conditions
tables.Lock(ctx)
defer tables.Unlock(ctx)
numGames := countActiveTables(ctx)
logger.Info("Initiating a graceful server shutdown (with " + strconv.Itoa(numGames) +
" active games).")
if numGames == 0 {
shutdownImmediate(ctx)
} else {
// Notify the lobby and all ongoing tables
notifyAllShutdown()
numMinutes := strconv.Itoa(int(ShutdownTimeout.Minutes()))
chatServerSendAll(ctx, "The server will shutdown in "+numMinutes+" minutes.")
go shutdownXMinutesLeft(ctx, 5)
go shutdownXMinutesLeft(ctx, 10)
go shutdownWait(ctx)
}
}
func shutdownXMinutesLeft(ctx context.Context, minutesLeft int) {
time.Sleep(ShutdownTimeout - time.Duration(minutesLeft)*time.Minute)
// Do nothing if the shutdown was canceled
if shuttingDown.IsNotSet() {
return
}
// Since this is a function that changes a user's relationship to tables,
// we must acquires the tables lock to prevent race conditions
tables.Lock(ctx)
defer tables.Unlock(ctx)
if minutesLeft == 5 {
// Automatically end all unstarted tables,
// since they will almost certainly not have time to finish
terminateAllUnstartedTables(ctx)
}
// Send a warning message to the lobby
msg := "The server will shutdown in " + strconv.Itoa(minutesLeft) + " minutes."
chatServerSend(ctx, msg, "lobby", false)
// Send a warning message to the people still playing
tableList := tables.GetList(false)
roomNames := make([]string, 0)
for _, t := range tableList {
t.Lock(ctx)
roomNames = append(roomNames, t.GetRoomName())
t.Unlock(ctx)
}
msg += " Finish your game soon or it will be automatically terminated!"
for _, roomName := range roomNames {
chatServerSend(ctx, msg, roomName, false)
}
}
func terminateAllUnstartedTables(ctx context.Context) {
// It is assumed that the tables mutex is locked when calling this function
for _, t := range tables.GetList(false) {
t.Lock(ctx)
if !t.Running {
s := t.GetOwnerSession()
commandTableLeave(ctx, s, &CommandData{ // nolint: exhaustivestruct
TableID: t.ID,
NoTableLock: true,
NoTablesLock: true,
})
}
t.Unlock(ctx)
}
}
func shutdownWait(ctx context.Context) {
for {
if shutdownWaitSub(ctx) {
break
}
time.Sleep(time.Second)
}
}
// shutdownWaitSub runs at an interval while the server is waiting to shutdown
// It returns whether or not to break out of the infinite loop
func shutdownWaitSub(ctx context.Context) bool {
if shuttingDown.IsNotSet() {
logger.Info("The shutdown was aborted.")
return true
}
// Since this is a function that changes a user's relationship to tables,
// we must acquires the tables lock to prevent race conditions
tables.Lock(ctx)
defer tables.Unlock(ctx)
numActiveTables := countActiveTables(ctx)
if numActiveTables == 0 {
// Wait 10 seconds so that the players are not immediately booted upon finishing
time.Sleep(time.Second * 10)
logger.Info("There are 0 active tables left.")
shutdownImmediate(ctx)
return true
}
if numActiveTables > 0 && time.Since(datetimeShutdownInit) >= ShutdownTimeout {
// It has been a long time since the server shutdown/restart was initiated,
// so automatically terminate any remaining ongoing games
terminateAllStartedTables(ctx)
}
return false
}
func terminateAllStartedTables(ctx context.Context) {
// It is assumed that the tables mutex is locked when calling this function
for _, t := range tables.GetList(false) {
t.Lock(ctx)
if t.Running && !t.Replay {
s := t.GetOwnerSession()
commandAction(ctx, s, &CommandData{ // nolint: exhaustivestruct
TableID: t.ID,
Type: ActionTypeEndGame,
Target: -1,
Value: EndConditionTerminated,
NoTableLock: true,
NoTablesLock: true,
})
}
t.Unlock(ctx)
}
}
func countActiveTables(ctx context.Context) int {
// It is assumed that the tables mutex is locked when calling this function
tableList := tables.GetList(false)
numTables := 0
for _, t := range tableList {
t.Lock(ctx)
if t.Running && !t.Replay {
numTables++
}
t.Unlock(ctx)
}
return numTables
}
func shutdownImmediate(ctx context.Context) {
// It is assumed that the tables mutex is locked when calling this function
logger.Info("Initiating an immediate server shutdown.")
waitForAllWebSocketCommandsToFinish()
sessionList := sessions.GetList()
for _, s := range sessionList {
s.Error("The server is going down for scheduled maintenance.<br />" +
"The server might be down for a while; " +
"please see the Discord server for more specific updates.")
s.NotifySoundLobby("shutdown")
}
msg := "The server successfully shut down at: " + getCurrentTimestamp()
chatServerSend(ctx, msg, "lobby", false)
if runtime.GOOS == "windows" {
logger.Info("Manually kill the server now.")
} else if err := executeScript("stop.sh"); err != nil {
logger.Error("Failed to execute the \"stop.sh\" script: " + err.Error())
}
}
func cancel(ctx context.Context) {
shuttingDown.UnSet()
notifyAllShutdown()
chatServerSendAll(ctx, "Server shutdown has been canceled.")
}
func checkImminentShutdown(s *Session) bool {
if shuttingDown.IsNotSet() {
return false
}
timeLeft := ShutdownTimeout - time.Since(datetimeShutdownInit)
minutesLeft := int(timeLeft.Minutes())
if minutesLeft <= 5 {
msg := "The server is shutting down "
if minutesLeft == 0 {
msg += "momentarily"
} else if minutesLeft == 1 {
msg += "in 1 minute"
} else {
msg += "in " + strconv.Itoa(minutesLeft) + " minutes"
}
msg += ". You cannot start any new games for the time being."
s.Warning(msg)
return true
}
return false
}
func waitForAllWebSocketCommandsToFinish() {
logger.Info("Waiting for all ongoing WebSocket commands to finish execution...")
blockAllIncomingMessages.Set()
commandWaitGroup.Wait() // Will block until it the counter becomes 0
logger.Info("All WebSocket commands have completed.")
}