Implement review suggestions

This commit is contained in:
Daniel
2020-08-11 16:16:01 +02:00
parent 092da058a5
commit ae5e7eb767
3 changed files with 54 additions and 41 deletions

View File

@@ -21,21 +21,38 @@ var (
)
func init() {
restartTask = module.NewTask("automatic restart", automaticRestart)
restartTask = module.NewTask("automatic restart", automaticRestart).MaxDelay(10 * time.Minute)
}
func triggerRestart(delay time.Duration) {
// DelayedRestart triggers a restart of the application by shutting down the
// module system gracefully and returning with RestartExitCode. The restart
// may be further delayed by up to 10 minutes by the internal task scheduling
// system. This only works if the process is managed by portmaster-start.
func DelayedRestart(delay time.Duration) {
log.Warningf("updates: restart triggered, will execute in %s", delay)
// This enables TriggerRestartIfPending.
// Subsequent calls to TriggerRestart should be able to set a new delay.
restartPending.Set()
// Schedule the restart task.
restartTask.Schedule(time.Now().Add(delay))
}
// TriggerRestartIfPending triggers an automatic restart, if one is pending. This can be used to prepone a scheduled restart if the conditions are preferable.
// TriggerRestartIfPending triggers an automatic restart, if one is pending.
// This can be used to prepone a scheduled restart if the conditions are preferable.
func TriggerRestartIfPending() {
if restartPending.IsSet() {
_ = automaticRestart(module.Ctx, nil)
}
}
// RestartNow immediately executes a restart.
// This only works if the process is managed by portmaster-start.
func RestartNow() {
_ = automaticRestart(module.Ctx, nil)
}
func automaticRestart(_ context.Context, _ *modules.Task) error {
if restartTriggered.SetToIf(false, true) {
log.Info("updates: initiating automatic restart")