Skip to content

Commit

Permalink
only reboot every 3 days and spread in one day (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Apr 14, 2022
1 parent 3eeeff7 commit a3929d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

Expand Down
19 changes: 15 additions & 4 deletions pkg/kernel/kernel.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kernel

import (
"crypto/rand"
"fmt"
"math/big"
"os"
"strings"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit a3929d3

Please sign in to comment.