Skip to content
This repository was archived by the owner on Jul 7, 2026. It is now read-only.

Commit e8dd8f4

Browse files
committed
feat: move (re)dial inside monitor loop, to cover server bounces
1 parent a0ff553 commit e8dd8f4

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

cmd/monitor.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import (
55
"time"
66

77
"cloud.google.com/go/logging"
8-
rcon "github.com/gtaylor/factorio-rcon"
98
)
109

11-
func monitor(r *rcon.RCON, logger *logging.Logger, cfg configMonitor) {
10+
func monitor(logger *logging.Logger, cfg config) {
1211
// Keep track of how long the server has been empty for
1312
emptySince := time.Now().UTC()
1413

1514
// Main monitoring loop
1615
for {
17-
time.Sleep(cfg.MonitorInterval)
16+
time.Sleep(cfg.Monitor.MonitorInterval)
17+
18+
r := dialAndAuth(logger, cfg.RCON)
1819

1920
players, errCP := r.CmdPlayers()
2021
if errCP != nil {
@@ -26,6 +27,8 @@ func monitor(r *rcon.RCON, logger *logging.Logger, cfg configMonitor) {
2627
continue
2728
}
2829

30+
r.Close()
31+
2932
logger.Log(logging.Entry{
3033
Payload: fmt.Sprintf("%+v", players),
3134
Severity: logging.Info,
@@ -49,9 +52,9 @@ func monitor(r *rcon.RCON, logger *logging.Logger, cfg configMonitor) {
4952
})
5053
}
5154

52-
if emptySince.Add(cfg.MonitorLimit).Before(time.Now().UTC()) {
55+
if emptySince.Add(cfg.Monitor.MonitorLimit).Before(time.Now().UTC()) {
5356
logger.Log(logging.Entry{
54-
Payload: fmt.Sprintf("Threshold reached; %s elapsed without any online players", cfg.MonitorLimit),
57+
Payload: fmt.Sprintf("Threshold reached; %s elapsed without any online players", cfg.Monitor.MonitorLimit),
5558
Severity: logging.Notice,
5659
})
5760

cmd/password.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"cloud.google.com/go/logging"
1010
)
1111

12-
// mustGetPassword prepares the RCON password.
12+
// mustGetPassword fetches the RCON password.
1313
func mustGetPassword(l *logging.Logger) string {
1414
bPassword, errRF := ioutil.ReadFile("/opt/factorio/config/rconpw")
1515
if errRF != nil {

cmd/rcon.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ func dialAndAuth(logger *logging.Logger, cfg configRcon) *rcon.RCON {
2424

2525
var r *rcon.RCON
2626

27+
logger.Log(logging.Entry{
28+
Payload: fmt.Sprintf("Dialling '%s' and authing...", rconAddress),
29+
Severity: logging.Notice,
30+
})
31+
2732
// Set placeholder errors before going into loop
2833
errDial, errAuth := errPlaceholder, errPlaceholder
2934

@@ -50,5 +55,10 @@ func dialAndAuth(logger *logging.Logger, cfg configRcon) *rcon.RCON {
5055
}
5156
}
5257

58+
logger.Log(logging.Entry{
59+
Payload: "Online!",
60+
Severity: logging.Notice,
61+
})
62+
5363
return r
5464
}

cmd/root.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,9 @@ func Run(_ []string, _ io.Writer) error {
7575
}
7676

7777
notice.Printf("Configuration loaded:\n%s", spew.Sdump(cfg))
78-
notice.Printf("Dialling '%s' and authing...", rconAddress)
7978

8079
// Main loop
81-
r := dialAndAuth(logger, cfg.RCON)
82-
defer r.Close()
83-
notice.Print("Online!")
84-
monitor(r, logger, cfg.Monitor)
80+
monitor(logger, cfg)
8581

8682
// Server seppuku
8783
cmd := exec.Command("shutdown", "--poweroff", "now")

0 commit comments

Comments
 (0)