From 59cf4cd97bb1b63aea1ea83c1bb184916eb10be5 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Sun, 10 Mar 2024 06:03:07 +0100 Subject: [PATCH] Ensure schedule never runs with non-positive ticker --- management/server/network.go | 2 +- management/server/scheduler.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/management/server/network.go b/management/server/network.go index ffe098c96..0e7d753a7 100644 --- a/management/server/network.go +++ b/management/server/network.go @@ -36,7 +36,7 @@ type NetworkMap struct { type Network struct { Identifier string `json:"id"` - Net net.IPNet `gorm:"serializer:gob"` + Net net.IPNet `gorm:"serializer:json"` Dns string // Serial is an ID that increments by 1 when any change to the network happened (e.g. new peer has been added). // Used to synchronize state to the client apps. diff --git a/management/server/scheduler.go b/management/server/scheduler.go index 356348056..3080c460a 100644 --- a/management/server/scheduler.go +++ b/management/server/scheduler.go @@ -85,6 +85,11 @@ func (wm *DefaultScheduler) Schedule(in time.Duration, ID string, job func() (ne return } + if in < time.Second { + log.Warnf("job for %s was scheduled to run in %s which is under 1s. Adjusting that.", ID, in.String()) + in = time.Second + } + ticker := time.NewTicker(in) wm.jobs[ID] = cancel @@ -112,6 +117,10 @@ func (wm *DefaultScheduler) Schedule(in time.Duration, ID string, job func() (ne } // we need this comparison to avoid resetting the ticker with the same duration and missing the current elapsesed time if runIn != in { + if runIn < time.Second { + log.Warnf("job for %s was rescheduled to run in %s which is under 1s. Adjusting that.", ID, runIn.String()) + runIn = time.Second + } ticker.Reset(runIn) } case <-cancel: