From c45fa13206245945602824acf2801348ab0598b2 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 17 Aug 2025 21:15:28 +0200 Subject: [PATCH] Initialize last request time on instance start and update timeout handling logic --- pkg/instance/lifecycle.go | 3 +++ pkg/instance/timeout.go | 7 +++++-- pkg/manager/timeout.go | 21 ++++++++++++++------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/pkg/instance/lifecycle.go b/pkg/instance/lifecycle.go index 1442b6e..abd6cc2 100644 --- a/pkg/instance/lifecycle.go +++ b/pkg/instance/lifecycle.go @@ -30,6 +30,9 @@ func (i *Process) Start() error { i.restarts = 0 } + // Initialize last request time to current time when starting + i.lastRequestTime.Store(time.Now().Unix()) + // Create log files if err := i.logger.Create(); err != nil { return fmt.Errorf("failed to create log files: %w", err) diff --git a/pkg/instance/timeout.go b/pkg/instance/timeout.go index 4b7a8c8..e17da4d 100644 --- a/pkg/instance/timeout.go +++ b/pkg/instance/timeout.go @@ -21,7 +21,10 @@ func (i *Process) ShouldTimeout() bool { // Check if the last request time exceeds the idle timeout lastRequest := i.lastRequestTime.Load() - idleTimeout := *i.options.IdleTimeout + idleTimeoutMinutes := *i.options.IdleTimeout - return (time.Now().Unix() - lastRequest) > int64(idleTimeout) + // Convert timeout from minutes to seconds for comparison + idleTimeoutSeconds := int64(idleTimeoutMinutes * 60) + + return (time.Now().Unix() - lastRequest) > idleTimeoutSeconds } diff --git a/pkg/manager/timeout.go b/pkg/manager/timeout.go index 090311b..6b67fa2 100644 --- a/pkg/manager/timeout.go +++ b/pkg/manager/timeout.go @@ -4,16 +4,23 @@ import "log" func (im *instanceManager) checkAllTimeouts() { im.mu.RLock() - defer im.mu.RUnlock() + var timeoutInstances []string + // Identify instances that should timeout for _, inst := range im.instances { if inst.ShouldTimeout() { - log.Printf("Instance %s has timed out, stopping it", inst.Name) - if proc, err := im.StopInstance(inst.Name); err != nil { - log.Printf("Error stopping instance %s: %v", inst.Name, err) - } else { - log.Printf("Instance %s stopped successfully, process: %v", inst.Name, proc) - } + timeoutInstances = append(timeoutInstances, inst.Name) + } + } + im.mu.RUnlock() // Release read lock before calling StopInstance + + // Stop the timed-out instances + for _, name := range timeoutInstances { + log.Printf("Instance %s has timed out, stopping it", name) + if proc, err := im.StopInstance(name); err != nil { + log.Printf("Error stopping instance %s: %v", name, err) + } else { + log.Printf("Instance %s stopped successfully, process: %v", name, proc) } } }