hub: reset smart interval on agent reconnect if agent hasn't successfully saved smart devices

this is so people trying to get smart working can see the config changes immediately. not need to wait for the smart interval.
This commit is contained in:
henrygd
2026-03-28 20:47:16 -04:00
parent afdc3f7779
commit 5bd43ed461
4 changed files with 121 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"strings"
"time"
"github.com/henrygd/beszel/internal/entities/smart"
"github.com/pocketbase/pocketbase/core"
@@ -12,10 +13,39 @@ import (
// FetchAndSaveSmartDevices fetches SMART data from the agent and saves it to the database
func (sys *System) FetchAndSaveSmartDevices() error {
smartData, err := sys.FetchSmartDataFromAgent()
if err != nil || len(smartData) == 0 {
if err != nil {
sys.recordSmartFetchResult(err, 0)
return err
}
return sys.saveSmartDevices(smartData)
err = sys.saveSmartDevices(smartData)
sys.recordSmartFetchResult(err, len(smartData))
return err
}
// recordSmartFetchResult stores a cooldown entry for the SMART interval and marks
// whether the last fetch produced any devices, so failed setup can retry on reconnect.
func (sys *System) recordSmartFetchResult(err error, deviceCount int) {
if sys.manager == nil {
return
}
sys.manager.smartFetchMap.Set(sys.Id, err == nil && deviceCount > 0, sys.smartFetchInterval()+time.Minute)
}
// shouldFetchSmart returns true when there is no active SMART cooldown entry for this system.
func (sys *System) shouldFetchSmart() bool {
if sys.manager == nil {
return true
}
_, ok := sys.manager.smartFetchMap.GetOk(sys.Id)
return !ok
}
// smartFetchInterval returns the agent-provided SMART interval or the default when unset.
func (sys *System) smartFetchInterval() time.Duration {
if sys.smartInterval > 0 {
return sys.smartInterval
}
return time.Hour
}
// saveSmartDevices saves SMART device data to the smart_devices collection