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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Keep the pending planned poll slot instead of compounding it
rcourtman5632ee10e89b
diff --git a/internal/monitoring/scheduler.go b/internal/monitoring/scheduler.goindex e8ecd2fd98..a715eb1be0 100644--- a/internal/monitoring/scheduler.go+++ b/internal/monitoring/scheduler.go@@ -204,7 +204,21 @@ func (s *AdaptiveScheduler) BuildPlan(now time.Time, inventory []InstanceDescripnextRun := nowif !lastScheduled.IsZero() {- nextRun = lastScheduled.Add(nextInterval)+ if lastScheduled.After(now) {+ // The previously planned run has not elapsed yet. Planning+ // passes run every poll tick, so re-adding an interval here+ // compounds NextRun ahead of wall-clock faster than time+ // passes, and once the adaptive interval stretches beyond the+ // tick cadence the instance is never due again (#1437). Keep+ // the pending slot, tightening it only when the freshly+ // selected interval now justifies an earlier run.+ nextRun = lastScheduled+ if candidate := now.Add(nextInterval); candidate.Before(nextRun) {+ nextRun = candidate+ }+ } else {+ nextRun = lastScheduled.Add(nextInterval)+ }} else if !inst.LastSuccess.IsZero() {nextRun = inst.LastSuccess.Add(nextInterval)}diff --git a/internal/monitoring/scheduler_test.go b/internal/monitoring/scheduler_test.goindex 8aee3417bd..8d37a4fbcd 100644--- a/internal/monitoring/scheduler_test.go+++ b/internal/monitoring/scheduler_test.go@@ -2360,3 +2360,92 @@ func TestLastScheduled_Found(t *testing.T) {t.Errorf("expected instance name pbs1, got %s", task.InstanceName)}}++// TestBuildPlanRepeatedPassesKeepPendingSlot reproduces the monitor's planning+// feedback loop: every poll tick rebuilds the plan from LastScheduled, which+// reports the previously planned NextRun. Re-adding an interval to a pending+// slot that has not elapsed compounds NextRun ahead of wall-clock faster than+// time passes, so once the adaptive interval stretches beyond the tick cadence+// the instance is never due again and API polling starves permanently (#1437).+func TestBuildPlanRepeatedPassesKeepPendingSlot(t *testing.T) {+ selector := mockIntervalSelector{interval: 60 * time.Second}+ scheduler := NewAdaptiveScheduler(DefaultSchedulerConfig(), mockStalenessSource{}, selector, &mockTaskEnqueuer{})++ start := time.Now()+ descriptorAt := func(now time.Time) []InstanceDescriptor {+ desc := InstanceDescriptor{Name: "pve-1", Type: InstanceTypePVE}+ if last, ok := scheduler.LastScheduled(InstanceTypePVE, "pve-1"); ok {+ desc.LastScheduled = last.NextRun+ desc.LastInterval = last.Interval+ }+ return []InstanceDescriptor{desc}+ }++ // First pass schedules an immediate run, planning the next slot 60s out.+ first := scheduler.BuildPlan(start, descriptorAt(start), 0)+ if len(first) != 1 {+ t.Fatalf("expected one task, got %d", len(first))+ }+ pending := start.Add(60 * time.Second)++ // Re-plan every 10 seconds while the pending slot has not elapsed, exactly+ // like the monitor's poll tick does. The slot must hold still.+ for tick := 1; tick <= 5; tick++ {+ now := start.Add(time.Duration(tick) * 10 * time.Second)+ tasks := scheduler.BuildPlan(now, descriptorAt(now), 0)+ if len(tasks) != 1 {+ t.Fatalf("tick %d: expected one task, got %d", tick, len(tasks))+ }+ if tasks[0].NextRun.After(pending) {+ t.Fatalf("tick %d: pending slot drifted from %s to %s, planning passes compound NextRun past wall-clock",+ tick, pending.Format(time.RFC3339), tasks[0].NextRun.Format(time.RFC3339))+ }+ }++ // Once the pending slot elapses, the next pass advances exactly one interval.+ after := pending.Add(5 * time.Second)+ tasks := scheduler.BuildPlan(after, descriptorAt(after), 0)+ want := pending.Add(60 * time.Second)+ if !tasks[0].NextRun.Equal(want) {+ t.Fatalf("elapsed slot advanced to %s, want %s", tasks[0].NextRun.Format(time.RFC3339), want.Format(time.RFC3339))+ }+}++// TestBuildPlanPendingSlotTightensWhenIntervalShrinks pins the responsiveness+// side of the pending-slot rule: when staleness drives the selected interval+// below the remaining wait, the plan moves the run earlier instead of sitting+// out the rest of a long stretched slot.+func TestBuildPlanPendingSlotTightensWhenIntervalShrinks(t *testing.T) {+ selector := &settableIntervalSelector{interval: 5 * time.Minute}+ scheduler := NewAdaptiveScheduler(DefaultSchedulerConfig(), mockStalenessSource{}, selector, &mockTaskEnqueuer{})++ start := time.Now()+ descriptorAt := func() []InstanceDescriptor {+ desc := InstanceDescriptor{Name: "pve-1", Type: InstanceTypePVE}+ if last, ok := scheduler.LastScheduled(InstanceTypePVE, "pve-1"); ok {+ desc.LastScheduled = last.NextRun+ desc.LastInterval = last.Interval+ }+ return []InstanceDescriptor{desc}+ }++ scheduler.BuildPlan(start, descriptorAt(), 0)+ now := start.Add(10 * time.Second)+ scheduler.BuildPlan(now, descriptorAt(), 0)++ selector.interval = 10 * time.Second+ now = start.Add(20 * time.Second)+ tasks := scheduler.BuildPlan(now, descriptorAt(), 0)+ want := now.Add(10 * time.Second)+ if tasks[0].NextRun.After(want) {+ t.Fatalf("shrunk interval left the run at %s, want no later than %s", tasks[0].NextRun.Format(time.RFC3339), want.Format(time.RFC3339))+ }+}++type settableIntervalSelector struct {+ interval time.Duration+}++func (s *settableIntervalSelector) SelectInterval(req IntervalRequest) time.Duration {+ return s.interval+}