Skip to content

Commit

Permalink
Simpler autoreboot calculation (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Feb 26, 2024
1 parent 4920443 commit c92750a
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions pkg/kernel/kernel.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package kernel

import (
"crypto/rand"
"fmt"
"log/slog"
"math/big"
"math/rand/v2"
"os"
"strings"
"time"
Expand Down Expand Up @@ -132,21 +131,16 @@ func Watchdog(log *slog.Logger) {
// AutoReboot will start a timer and reboot after given duration a random variation spread is added
func AutoReboot(log *slog.Logger, after, spread time.Duration, callback func()) {
log.Info("autoreboot set to", "after", after.String(), "spread", spread.String())
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
spreadMinutes := rand.N(spread)
after = after + spreadMinutes

log.Info("autoreboot with spread", "after", after.String())
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)
}
Expand Down

0 comments on commit c92750a

Please sign in to comment.