Skip to content

Commit

Permalink
[APP-6886] 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 authored Dec 17, 2024
1 parent da5c245 commit fc02271
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion subsystems/provisioning/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,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 < conf.OfflineTimeout ||
conf.DeviceRebootAfterOfflineMinutes < conf.UserTimeout {
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 @@ -370,6 +376,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, default, will disable this feature.
DeviceRebootAfterOfflineMinutes Timeout `json:"device_reboot_after_offline_minutes"`
}

// Timeout allows parsing golang-style durations (1h20m30s) OR seconds-as-float from/to json.
Expand All @@ -386,7 +396,7 @@ func (t *Timeout) UnmarshalJSON(b []byte) error {
}
switch value := v.(type) {
case float64:
*t = Timeout(value * float64(time.Second))
*t = Timeout(value * float64(time.Minute))
return nil
case string:
tmp, err := time.ParseDuration(value)
Expand Down
16 changes: 16 additions & 0 deletions subsystems/provisioning/networkmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"os"
"os/exec"
"reflect"
"sort"
"time"
Expand Down Expand Up @@ -737,6 +738,21 @@ func (w *Provisioning) mainLoop(ctx context.Context) {
}
}

offlineRebootTimeout := w.cfg.DeviceRebootAfterOfflineMinutes > 0 &&
lastConnectivity.Before(now.Add(time.Duration(w.cfg.DeviceRebootAfterOfflineMinutes)*-1))
if offlineRebootTimeout {
w.logger.Infof("device has been offline for more than %s minutes, rebooting", w.cfg.DeviceRebootAfterOfflineMinutes)
cmd := exec.Command("systemctl", "reboot")
output, err := cmd.CombinedOutput()
if err != nil {
w.logger.Error(errw.Wrapf(err, "running 'systemctl reboot' %s", output))
}
if !w.mainLoopHealth.Sleep(ctx, time.Minute*5) {
return
}
w.logger.Errorf("failed to reboot after %s time", time.Minute*5)
}

hitOfflineTimeout := lastConnectivity.Before(now.Add(time.Duration(w.cfg.OfflineTimeout)*-1)) &&
pModeChange.Before(now.Add(time.Duration(w.cfg.OfflineTimeout)*-1))
// not in provisioning mode, so start it if not configured (/etc/viam.json)
Expand Down

0 comments on commit fc02271

Please sign in to comment.