From 7d0f97445b712f4e725269534eacc4545e47c0da Mon Sep 17 00:00:00 2001 From: Preslav Gerchev Date: Tue, 2 Apr 2024 13:57:27 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20serve=20panics=20if=20spla?= =?UTF-8?q?y=20is=20zero.=20(#1217)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Preslav --- apps/cnspec/cmd/backgroundjob/serve_unix.go | 6 +++++- apps/cnspec/cmd/backgroundjob/serve_windows.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/apps/cnspec/cmd/backgroundjob/serve_unix.go b/apps/cnspec/cmd/backgroundjob/serve_unix.go index 3566026d..5cfa9844 100644 --- a/apps/cnspec/cmd/backgroundjob/serve_unix.go +++ b/apps/cnspec/cmd/backgroundjob/serve_unix.go @@ -49,7 +49,11 @@ func Serve(timer time.Duration, splay time.Duration, handler JobRunner) { if err != nil { log.Error().Err(err).Send() } - nextRun := timer + time.Duration(rand.Int63n(int64(splay))) + splayDur := time.Duration(0) + if splay > 0 { + splayDur = time.Duration(rand.Int63n(int64(splay))) + } + nextRun := timer + splayDur log.Info().Msgf("next scan in %v", nextRun) t.Reset(nextRun) case <-shutdownChannel: diff --git a/apps/cnspec/cmd/backgroundjob/serve_windows.go b/apps/cnspec/cmd/backgroundjob/serve_windows.go index d61b4a36..405fe479 100644 --- a/apps/cnspec/cmd/backgroundjob/serve_windows.go +++ b/apps/cnspec/cmd/backgroundjob/serve_windows.go @@ -80,7 +80,11 @@ loop: default: log.Error().Msg("scan not started. may be stuck") } - nextRun := m.Timer + time.Duration(rand.Int63n(int64(m.Splay))) + splayDur := time.Duration(0) + if m.Splay > 0 { + splayDur = time.Duration(rand.Int63n(int64(m.Splay))) + } + nextRun := m.Timer + splayDur log.Info().Msgf("next scan in %v", nextRun) t.Reset(nextRun) case c := <-r: