From a3929d37616bafcab4d24756bd16217058059f9a Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Thu, 14 Apr 2022 07:31:52 +0200 Subject: [PATCH] only reboot every 3 days and spread in one day (#75) --- cmd/root.go | 2 +- pkg/kernel/kernel.go | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b1aab763..235cd3ef 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -66,7 +66,7 @@ func Run(spec *Specification, hal hal.InBand) (*event.EventEmitter, error) { } // Reboot after 24Hours if no allocation was requested. - go kernel.AutoReboot(24*time.Hour, func() { + go kernel.AutoReboot(3*24*time.Hour, 24*time.Hour, func() { eventEmitter.Emit(event.ProvisioningEventPlannedReboot, "autoreboot after 24h") }) diff --git a/pkg/kernel/kernel.go b/pkg/kernel/kernel.go index 9a836c0b..dff4fa66 100644 --- a/pkg/kernel/kernel.go +++ b/pkg/kernel/kernel.go @@ -1,7 +1,9 @@ package kernel import ( + "crypto/rand" "fmt" + "math/big" "os" "strings" "time" @@ -134,15 +136,24 @@ func Watchdog() { } } -// AutoReboot will start a timer and reboot after given duration -func AutoReboot(after time.Duration, callback func()) { - log.Info("autoreboot", "after", after) +// AutoReboot will start a timer and reboot after given duration a random variation spread is added +func AutoReboot(after, spread time.Duration, callback func()) { + log.Info("autoreboot set to", "after", after, "spread", spread) + spreadMinutes, err := rand.Int(rand.Reader, big.NewInt(int64(spread.Minutes()))) + if err != nil { + log.Warn("autoreboot", "unable to calculate spread, disable spread", err) + spread = time.Duration(0) + } + spread = time.Minute * time.Duration(spreadMinutes.Int64()) + after = after + spread + + log.Info("autoreboot with spread", "after", after) rebootTimer := time.NewTimer(after) <-rebootTimer.C log.Info("autoreboot", "timeout reached", "rebooting in 10sec") callback() time.Sleep(10 * time.Second) - err := Reboot() + err = Reboot() if err != nil { log.Error("autoreboot", "unable to reboot, error", err) }