This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
111 lines (98 loc) · 2.16 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
package spotmc
import (
"fmt"
log "github.com/Sirupsen/logrus"
"os"
"os/signal"
"syscall"
)
func Main() {
// Initialize
smc, err := NewSpotMC()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// Update DDNS
smc.updateDDNS()
// Get game server jar file
log.Info("retrieving game server jar file")
_, err = smc.getJarFile()
if err != nil {
log.Fatal(err)
return
}
log.WithFields(log.Fields{
"path": smc.serverPath,
}).Info("game server jar file retrieved")
// Get the data dir from S3
log.Info("retrieving data directory")
_, err = smc.getDataDir()
if err != nil {
log.Fatal(err)
return
}
log.WithFields(log.Fields{
"path": smc.dataDirPath,
}).Info("data directory archive file retrieved")
// Run game server
log.Printf("starting the game server")
cmd, err := smc.startServer()
if err != nil {
err = fmt.Errorf("game server did not start: %s", err)
}
// Spawn watch proc which waits for the game server to end
go func() {
err = cmd.Wait()
log.Info("game server process exited")
smc.msgs <- msgGameServerDown
}()
// Spawn a SIGTERM handler
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGTERM)
go func() {
<-sigchan
smc.msgs <- msgInstanceTerminating
}()
// Spawn other watchers
go smc.idleWatcher()
go smc.uptimeWatcher()
go smc.terminationNotificationWatcher()
// Start the main loop
for {
msg := <-smc.msgs
if msg == msgInstanceTerminating {
log.Info("killing the game server")
cmd.Process.Kill()
}
if msg == msgShutdownCluster {
log.Info("shutting down the cluster")
err := smc.shutdownCluster()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("cluster shutdown failed!")
}
go func() {
smc.msgs <- msgInstanceTerminating
}()
}
if msg == msgGameServerDown {
// If the game server ends, the instance dies
// Save data to S3
log.Info("saving data to S3 started")
err := smc.putDataDir()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("saving data to S3 failed")
} else {
log.Info("saving data to S3 done")
}
// Kill instance
smc.killInstance()
// Escape the loop
break
}
}
}