Skip to content

Commit

Permalink
Add option to reboot device after a certain amount of time
Browse files Browse the repository at this point in the history
  • Loading branch information
ale7714 committed Dec 5, 2024
1 parent b19180f commit 9ebc446
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions subsystems/provisioning/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ func ConfigFromJSON(defaultConf Config, jsonBytes []byte) (*Config, error) {
return &conf, errw.Errorf("timeout values cannot be less than %s", time.Duration(minTimeout))
}

if conf.DeviceRebootAfterOfflineMinutes != 0 &&
conf.DeviceRebootAfterOfflineMinutes < int(time.Duration(conf.OfflineTimeout).Minutes()) &&
conf.DeviceRebootAfterOfflineMinutes < int(time.Duration(conf.UserTimeout).Minutes()) {
return &conf, errw.Errorf("device_reboot_after_offline_minutes cannot be less than offline_timeout or user_timeout")
}

return &conf, nil
}

Expand Down Expand Up @@ -372,6 +378,10 @@ type Config struct {

// If set, will explicitly enable or disable power save for all wifi connections managed by NetworkManager.
WifiPowerSave *bool `json:"wifi_power_save"`

// If set, will reboot the device after it has been offline for this duration
// 0 will disable this feature.
DeviceRebootAfterOfflineMinutes int `json:"device_reboot_after_offline_minutes"`
}

// Timeout allows parsing golang-style durations (1h20m30s) OR seconds-as-float from/to json.
Expand Down
8 changes: 8 additions & 0 deletions subsystems/provisioning/networkmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"reflect"
"sort"
"syscall"
"time"

gnm "github.com/Otterverse/gonetworkmanager/v2"
Expand Down Expand Up @@ -698,6 +699,13 @@ func (w *Provisioning) mainLoop(ctx context.Context) {

if pMode {

Check failure on line 700 in subsystems/provisioning/networkmanager.go

View workflow job for this annotation

GitHub Actions / Test lint and build

Error return value of `syscall.Reboot` is not checked (errcheck)
// complex logic, so wasting some variables for readability
deviceRebootAfterOfflineDuration := time.Duration(w.cfg.DeviceRebootAfterOfflineMinutes) * time.Minute

if w.cfg.DeviceRebootAfterOfflineMinutes > 0 && now.After(lastOnline.Add(deviceRebootAfterOfflineDuration)) {
w.logger.Infof("device has been offline for more than %s minutes, rebooting", deviceRebootAfterOfflineDuration)
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
}

// portal interaction time is updated when a user loads a page or makes a grpc request
inactivePortal := w.connState.getLastInteraction().Before(now.Add(time.Duration(w.cfg.UserTimeout)*-1)) || userInputReceived
Expand Down

0 comments on commit 9ebc446

Please sign in to comment.