Fix restarting flag for direct restarts

This commit is contained in:
Daniel 2021-10-19 11:48:17 +02:00
parent 003f99e392
commit 8c57baa12a
3 changed files with 11 additions and 6 deletions

View file

@ -68,8 +68,6 @@ func shutdown(_ *api.Request) (msg string, err error) {
func restart(_ *api.Request) (msg string, err error) { func restart(_ *api.Request) (msg string, err error) {
log.Info("core: user requested restart via action") log.Info("core: user requested restart via action")
// Trigger restart event instead of shutdown event.
restarting.Set()
// Let the updates module handle restarting. // Let the updates module handle restarting.
updates.RestartNow() updates.RestartNow()

View file

@ -7,13 +7,12 @@ import (
"github.com/safing/portbase/modules" "github.com/safing/portbase/modules"
"github.com/safing/portbase/modules/subsystems" "github.com/safing/portbase/modules/subsystems"
"github.com/tevino/abool" "github.com/safing/portmaster/updates"
// module dependencies // module dependencies
_ "github.com/safing/portmaster/netenv" _ "github.com/safing/portmaster/netenv"
_ "github.com/safing/portmaster/status" _ "github.com/safing/portmaster/status"
_ "github.com/safing/portmaster/ui" _ "github.com/safing/portmaster/ui"
_ "github.com/safing/portmaster/updates"
) )
const ( const (
@ -24,7 +23,6 @@ const (
var ( var (
module *modules.Module module *modules.Module
restarting = abool.New()
disableShutdownEvent bool disableShutdownEvent bool
) )
@ -82,7 +80,7 @@ func registerEvents() {
func shutdownHook() { func shutdownHook() {
// Notify everyone of the restart/shutdown. // Notify everyone of the restart/shutdown.
if restarting.IsNotSet() { if !updates.IsRestarting() {
// Only trigger shutdown event if not disabled. // Only trigger shutdown event if not disabled.
if !disableShutdownEvent { if !disableShutdownEvent {
module.TriggerEvent(eventShutdown, nil) module.TriggerEvent(eventShutdown, nil)

View file

@ -20,6 +20,11 @@ var (
restartTriggered = abool.New() restartTriggered = abool.New()
) )
// IsRestarting returns whether a restart is pending or currently in progress.
func IsRestarting() bool {
return restartPending.IsSet()
}
// DelayedRestart triggers a restart of the application by shutting down the // DelayedRestart triggers a restart of the application by shutting down the
// module system gracefully and returning with RestartExitCode. The restart // module system gracefully and returning with RestartExitCode. The restart
// may be further delayed by up to 10 minutes by the internal task scheduling // may be further delayed by up to 10 minutes by the internal task scheduling
@ -52,6 +57,10 @@ func RestartNow() {
func automaticRestart(_ context.Context, _ *modules.Task) error { func automaticRestart(_ context.Context, _ *modules.Task) error {
if restartTriggered.SetToIf(false, true) { if restartTriggered.SetToIf(false, true) {
log.Info("updates: initiating (automatic) restart") log.Info("updates: initiating (automatic) restart")
// Set restart pending to ensure IsRestarting() returns true.
restartPending.Set()
// Set restart exit code.
modules.SetExitStatusCode(RestartExitCode) modules.SetExitStatusCode(RestartExitCode)
// Do not use a worker, as this would block itself here. // Do not use a worker, as this would block itself here.
go modules.Shutdown() //nolint:errcheck go modules.Shutdown() //nolint:errcheck