Commit a9db9b9

bryfry <bryon@fryer.io>
2025-05-01 15:25:23
cleanup
1 parent b6bb454
Changed files (1)
internal
internal/aod/main.go
@@ -8,13 +8,13 @@ import (
 	"github.com/bryfry/mm/internal/minecraft"
 )
 
-func Watch(domain string, die bool) error {
+func Watch(domain string, timeout bool) error {
 
 	const (
-		_targetPort     uint16        = 25565
-		_checkInterval  time.Duration = time.Second * 5
-		_logInterval    time.Duration = time.Minute * 5
-		_defaultTimeout time.Duration = time.Minute * 55
+		_targetPort      uint16        = 25565
+		_checkInterval   time.Duration = time.Second * 5
+		_logInterval     time.Duration = time.Minute * 5
+		_inactiveTimeout time.Duration = time.Minute * 55
 	)
 
 	ticker := time.NewTicker(_checkInterval)
@@ -23,13 +23,12 @@ func Watch(domain string, die bool) error {
 	slog.Info("active or die: starting",
 		slog.String("domain", domain),
 		slog.Int("port", int(_targetPort)),
-		slog.Bool("poweroff_on_timeout", die))
+		slog.Bool("poweroff_on_timeout", timeout))
 
 	// loop variables and timers
 	lastActive := time.Now()
 	lastLog := time.Now()
 	connections := -1 // log first active connection check
-	inactiveTimeout := _defaultTimeout
 	for {
 		select {
 		case <-ticker.C:
@@ -48,14 +47,17 @@ func Watch(domain string, die bool) error {
 			msg := "inactive"
 			details := []slog.Attr{
 				slog.String("duration", inactiveDuration.String()),
-				slog.String("timeout", inactiveTimeout.String()),
+			}
+			if timeout {
+				details = append(details,
+					slog.String("timeout", _inactiveTimeout.String()),
+				)
 			}
 
 			// reset if active
 			if connections != 0 {
 				lastActive = time.Now()
 				inactiveDuration = 0
-				inactiveTimeout = _defaultTimeout
 				msg = "active"
 				details = []slog.Attr{
 					slog.Int("connections", connections),
@@ -68,15 +70,12 @@ func Watch(domain string, die bool) error {
 				lastLog = time.Now()
 			}
 
-			if inactiveDuration > inactiveTimeout {
-				if die {
+			if inactiveDuration > _inactiveTimeout {
+				if timeout {
 					msg = "inactive timeout: powering off"
 					slog.LogAttrs(context.Background(), slog.LevelInfo, msg, details...)
 					return poweroff()
 				}
-				msg = "inactive timeout"
-				slog.LogAttrs(context.Background(), slog.LevelInfo, msg, details...)
-				inactiveTimeout = inactiveTimeout * 2
 			}
 		}
 	}