Files
Pulse/internal/monitoring/platform_poller_shared.go
pulse-triage[bot] 32958688de Enforce truthful TrueNAS and VMware poller lifecycle and read safety
Preserve active tenant providers, cached inventory, cached changes, and
runtime health in TrueNASPoller.syncConnections and
VMwarePoller.syncConnections when GetPersistence or Load*Config fails
transiently, instead of treating the failed org as empty and pruning all
active connections.

Stop iterating and drop results on explicit context.Canceled in
TrueNASPoller.pollAll, VMwarePoller.pollAll/pollConnection, and
startPollerLoop so shutdown does not fabricate ConsecutiveFailures,
LastError, or PollMetrics failures while preserving genuine
context.DeadlineExceeded timeout reporting.

Fence out-of-lock TrueNAS and VMware poll completions against
p.providersByOrg ownership before mutating statusByOrg, PollMetrics,
recovery points, or VMware cachedRecordsByOrg/cachedChangesByOrg so
in-flight refreshes cannot resurrect deleted or disabled connections or
clobber a replacement provider.

Reset non-zero status.nextPollAt whenever TrueNASPoller.syncConnections
creates a provider so repaired failed-init or re-enabled connections
poll immediately rather than waiting out a stale backoff interval.

Contract-Neutral: Pure internal runtime lifecycle and poll-truthfulness bug fixes across existing TrueNAS and VMware pollers; no public API, schema, telemetry, or subsystem contract delta.
Change-source: pulse-maintainer
2026-11-26 22:48:48 +00:00

122 lines
2.6 KiB
Go

package monitoring
import (
"context"
"strings"
"sync"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
)
// startPollerLoop runs the scheduling scaffold shared by the periodic
// platform pollers (TrueNAS, VMware): guard against double-start under mu,
// then sync + poll immediately and again on every nextWaitDuration tick until
// the context is cancelled. cancelSlot and stoppedSlot point at the poller's
// lifecycle fields so Stop keeps working against the same state.
func startPollerLoop(
ctx context.Context,
mu *sync.Mutex,
cancelSlot *context.CancelFunc,
stoppedSlot *chan struct{},
syncConnections func(),
pollAll func(context.Context),
nextWaitDuration func(time.Time) time.Duration,
) {
if ctx == nil {
ctx = context.Background()
}
mu.Lock()
if *cancelSlot != nil {
mu.Unlock()
return
}
runCtx, cancel := context.WithCancel(ctx)
*cancelSlot = cancel
*stoppedSlot = make(chan struct{})
stopped := *stoppedSlot
mu.Unlock()
go func() {
defer close(stopped)
defer func() {
mu.Lock()
if *stoppedSlot == stopped {
*cancelSlot = nil
}
mu.Unlock()
}()
syncConnections()
if runCtx.Err() != nil {
return
}
pollAll(runCtx)
for {
wait := nextWaitDuration(time.Now())
timer := time.NewTimer(wait)
select {
case <-runCtx.Done():
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
return
case <-timer.C:
syncConnections()
if runCtx.Err() != nil {
return
}
pollAll(runCtx)
}
}
}()
}
// loadActiveInstanceConfigs resolves the active platform connection configs
// for an org: prefer the poller's cached map, otherwise load from per-org
// persistence and keep enabled instances keyed by trimmed connection ID with
// defaults applied. Shared by the TrueNAS and VMware pollers so the "what
// counts as an active connection" policy stays single-sourced.
func loadActiveInstanceConfigs[T any](
cached map[string]T,
multiTenant *config.MultiTenantPersistence,
orgID string,
load func(*config.ConfigPersistence) ([]T, error),
applyDefaults func(*T),
enabled func(T) bool,
connID func(T) string,
) map[string]T {
if len(cached) > 0 || multiTenant == nil {
return cached
}
persistence, err := multiTenant.GetPersistence(orgID)
if err != nil || persistence == nil {
return cached
}
instances, err := load(persistence)
if err != nil {
return cached
}
active := make(map[string]T)
for i := range instances {
instance := instances[i]
applyDefaults(&instance)
if !enabled(instance) {
continue
}
id := strings.TrimSpace(connID(instance))
if id == "" {
continue
}
active[id] = instance
}
return active
}