From 176dfac0c1c38554e3501b5a400427e1fd4b766a Mon Sep 17 00:00:00 2001 From: headlessdev Date: Tue, 29 Apr 2025 20:40:56 +0200 Subject: [PATCH] Refactor notification handling to track server status changes instead of notification timestamps --- agent/internal/server/monitor.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/agent/internal/server/monitor.go b/agent/internal/server/monitor.go index 88dc1f5..2403594 100644 --- a/agent/internal/server/monitor.go +++ b/agent/internal/server/monitor.go @@ -15,12 +15,12 @@ import ( "github.com/corecontrol/agent/internal/notifications" ) -// notificationState tracks the last notification time for each server +// notificationState tracks the last known status for each server var notificationState = struct { sync.RWMutex - lastNotification map[int]time.Time + lastStatus map[int]bool }{ - lastNotification: make(map[int]time.Time), + lastStatus: make(map[int]bool), } // MonitorServers checks and updates the status of all servers @@ -108,17 +108,16 @@ func MonitorServers(db *sql.DB, client *http.Client, servers []models.Server, no } } -// shouldSendNotification checks if a notification should be sent based on cooldown period +// shouldSendNotification checks if a notification should be sent based on status change func shouldSendNotification(serverID int, online bool) bool { notificationState.Lock() defer notificationState.Unlock() - lastNotif, exists := notificationState.lastNotification[serverID] - now := time.Now() + lastStatus, exists := notificationState.lastStatus[serverID] - // If no previous notification or more than 5 minutes have passed - if !exists || now.Sub(lastNotif) > 5*time.Minute { - notificationState.lastNotification[serverID] = now + // If this is the first check or status has changed + if !exists || lastStatus != online { + notificationState.lastStatus[serverID] = online return true }