Skip to content

Commit

Permalink
feat: move (re)dial inside monitor loop, to cover server bounces
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucktay committed Nov 12, 2020
1 parent a0ff553 commit e8dd8f4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
13 changes: 8 additions & 5 deletions cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"time"

"cloud.google.com/go/logging"
rcon "github.com/gtaylor/factorio-rcon"
)

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

// Main monitoring loop
for {
time.Sleep(cfg.MonitorInterval)
time.Sleep(cfg.Monitor.MonitorInterval)

r := dialAndAuth(logger, cfg.RCON)

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

r.Close()

logger.Log(logging.Entry{
Payload: fmt.Sprintf("%+v", players),
Severity: logging.Info,
Expand All @@ -49,9 +52,9 @@ func monitor(r *rcon.RCON, logger *logging.Logger, cfg configMonitor) {
})
}

if emptySince.Add(cfg.MonitorLimit).Before(time.Now().UTC()) {
if emptySince.Add(cfg.Monitor.MonitorLimit).Before(time.Now().UTC()) {
logger.Log(logging.Entry{
Payload: fmt.Sprintf("Threshold reached; %s elapsed without any online players", cfg.MonitorLimit),
Payload: fmt.Sprintf("Threshold reached; %s elapsed without any online players", cfg.Monitor.MonitorLimit),
Severity: logging.Notice,
})

Expand Down
2 changes: 1 addition & 1 deletion cmd/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"cloud.google.com/go/logging"
)

// mustGetPassword prepares the RCON password.
// mustGetPassword fetches the RCON password.
func mustGetPassword(l *logging.Logger) string {
bPassword, errRF := ioutil.ReadFile("/opt/factorio/config/rconpw")
if errRF != nil {
Expand Down
10 changes: 10 additions & 0 deletions cmd/rcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func dialAndAuth(logger *logging.Logger, cfg configRcon) *rcon.RCON {

var r *rcon.RCON

logger.Log(logging.Entry{
Payload: fmt.Sprintf("Dialling '%s' and authing...", rconAddress),
Severity: logging.Notice,
})

// Set placeholder errors before going into loop
errDial, errAuth := errPlaceholder, errPlaceholder

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

logger.Log(logging.Entry{
Payload: "Online!",
Severity: logging.Notice,
})

return r
}
6 changes: 1 addition & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,9 @@ func Run(_ []string, _ io.Writer) error {
}

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

// Main loop
r := dialAndAuth(logger, cfg.RCON)
defer r.Close()
notice.Print("Online!")
monitor(r, logger, cfg.Monitor)
monitor(logger, cfg)

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

0 comments on commit e8dd8f4

Please sign in to comment.