Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Quiesce discovery backfills during shutdown

rcourtmanfb75ac6a056b
diff --git a/internal/servicediscovery/service.go b/internal/servicediscovery/service.go
index 908d95817a..801635bcbb 100644
--- a/internal/servicediscovery/service.go
+++ b/internal/servicediscovery/service.go
@@ -261,6 +261,8 @@ type Service struct {
stopCh chan struct{}
loopDone chan struct{}
runCancel context.CancelFunc
+ backfillCancel context.CancelFunc
+ backfillDone chan struct{}
intervalCh chan time.Duration // Channel for live interval updates
interval time.Duration
initialDelay time.Duration
@@ -410,10 +412,30 @@ func (s *Service) SetAIAnalyzer(analyzer AIAnalyzer) {
// SetReadState sets the typed ReadState provider for the discovery service.
// When set, getSnapshot() uses ReadState to build infrastructure snapshots.
func (s *Service) SetReadState(rs unifiedresources.ReadState) {
+ ctx, cancel := context.WithCancel(context.Background())
+ done := make(chan struct{})
+
s.mu.Lock()
+ previousCancel := s.backfillCancel
+ previousDone := s.backfillDone
s.readState = rs
+ s.backfillCancel = cancel
+ s.backfillDone = done
s.mu.Unlock()
- go s.backfillAvailabilitySuggestions(context.Background())
+
+ if previousCancel != nil {
+ previousCancel()
+ }
+ go func() {
+ defer close(done)
+ if previousDone != nil {
+ <-previousDone
+ }
+ if ctx.Err() != nil {
+ return
+ }
+ s.backfillAvailabilitySuggestions(ctx)
+ }()
}
// Start begins the background discovery service.
@@ -447,19 +469,22 @@ func (s *Service) Start(ctx context.Context) {
// Stop stops the background discovery service.
func (s *Service) Stop() {
s.mu.Lock()
- if !s.running {
- s.mu.Unlock()
- return
- }
s.running = false
stopCh := s.stopCh
loopDone := s.loopDone
runCancel := s.runCancel
+ backfillCancel := s.backfillCancel
+ backfillDone := s.backfillDone
s.stopCh = nil
s.loopDone = nil
s.runCancel = nil
+ s.backfillCancel = nil
+ s.backfillDone = nil
s.mu.Unlock()
+ if backfillCancel != nil {
+ backfillCancel()
+ }
if runCancel != nil {
runCancel()
}
@@ -469,6 +494,9 @@ func (s *Service) Stop() {
if loopDone != nil {
<-loopDone
}
+ if backfillDone != nil {
+ <-backfillDone
+ }
}
// SetInterval updates the scan interval. Takes effect immediately if running.
diff --git a/internal/servicediscovery/service_test.go b/internal/servicediscovery/service_test.go
index 1e26773221..85c3a7b37c 100644
--- a/internal/servicediscovery/service_test.go
+++ b/internal/servicediscovery/service_test.go
@@ -1176,6 +1176,7 @@ func TestService_CleanupPreservesLiveHostDiscoveries(t *testing.T) {
service := NewService(store, nil, DefaultConfig())
service.SetReadState(readStateFromSnapshot(state))
+ t.Cleanup(service.Stop)
service.cleanupOrphanedData(state)
if got, err := store.Get(live.ID); err != nil || got == nil {
@@ -1188,6 +1189,44 @@ func TestService_CleanupPreservesLiveHostDiscoveries(t *testing.T) {
}
}
+func TestService_StopWaitsForReadStateBackfill(t *testing.T) {
+ store, err := NewStore(t.TempDir())
+ if err != nil {
+ t.Fatalf("NewStore error: %v", err)
+ }
+ store.crypto = nil
+ if err := store.Save(&ResourceDiscovery{
+ ID: "agent:agent-uuid-1:agent-uuid-1",
+ ResourceType: ResourceTypeAgent,
+ TargetID: "agent-uuid-1",
+ ResourceID: "agent-uuid-1",
+ }); err != nil {
+ t.Fatalf("Save discovery: %v", err)
+ }
+
+ service := NewService(store, nil, DefaultConfig())
+ service.SetReadState(readStateFromSnapshot(StateSnapshot{}))
+
+ service.mu.RLock()
+ done := service.backfillDone
+ service.mu.RUnlock()
+ if done == nil {
+ t.Fatal("expected read-state backfill lifecycle channel")
+ }
+ select {
+ case <-done:
+ t.Fatal("expected empty-state backfill to wait for monitor data")
+ case <-time.After(100 * time.Millisecond):
+ }
+
+ service.Stop()
+ select {
+ case <-done:
+ default:
+ t.Fatal("Stop returned before read-state backfill exited")
+ }
+}
+
func TestService_PromptsAndDiscoveryLoop(t *testing.T) {
service := NewService(nil, nil, DefaultConfig())