mirror of
https://github.com/safing/portmaster
synced 2025-09-02 02:29:12 +00:00
Implement review suggestions
This commit is contained in:
parent
092da058a5
commit
ae5e7eb767
3 changed files with 54 additions and 41 deletions
|
@ -43,8 +43,6 @@ func shutdown(ctx context.Context, _ interface{}) error {
|
||||||
// restart restarts the Portmaster.
|
// restart restarts the Portmaster.
|
||||||
func restart(ctx context.Context, data interface{}) error {
|
func restart(ctx context.Context, data interface{}) error {
|
||||||
log.Info("core: user requested restart")
|
log.Info("core: user requested restart")
|
||||||
modules.SetExitStatusCode(updates.RestartExitCode)
|
updates.RestartNow()
|
||||||
// Do not use a worker, as this would block itself here.
|
|
||||||
go modules.Shutdown() //nolint:errcheck
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,21 +21,38 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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()
|
restartPending.Set()
|
||||||
|
|
||||||
|
// Schedule the restart task.
|
||||||
restartTask.Schedule(time.Now().Add(delay))
|
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() {
|
func TriggerRestartIfPending() {
|
||||||
if restartPending.IsSet() {
|
if restartPending.IsSet() {
|
||||||
_ = automaticRestart(module.Ctx, nil)
|
_ = 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 {
|
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")
|
||||||
|
|
|
@ -61,11 +61,8 @@ func upgrader(_ context.Context, _ interface{}) error {
|
||||||
log.Warningf("updates: failed to upgrade portmaster-start: %s", err)
|
log.Warningf("updates: failed to upgrade portmaster-start: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
binName := strings.TrimSuffix(
|
binBaseName := strings.Split(filepath.Base(os.Args[0]), "_")[0]
|
||||||
filepath.Base(os.Args[0]),
|
switch binBaseName {
|
||||||
".exe",
|
|
||||||
)
|
|
||||||
switch binName {
|
|
||||||
case "portmaster-core":
|
case "portmaster-core":
|
||||||
err = upgradeCoreNotify()
|
err = upgradeCoreNotify()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -83,23 +80,24 @@ func upgrader(_ context.Context, _ interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func upgradeCoreNotify() error {
|
func upgradeCoreNotify() error {
|
||||||
// check if we can upgrade
|
if pmCoreUpdate != nil && !pmCoreUpdate.UpgradeAvailable() {
|
||||||
if pmCoreUpdate == nil || pmCoreUpdate.UpgradeAvailable() {
|
|
||||||
identifier := "core/portmaster-core" // identifier, use forward slash!
|
|
||||||
if onWindows {
|
|
||||||
identifier += exeExt
|
|
||||||
}
|
|
||||||
|
|
||||||
// get newest portmaster-core
|
|
||||||
new, err := GetPlatformFile(identifier)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
pmCoreUpdate = new
|
|
||||||
} else {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make identifier
|
||||||
|
identifier := "core/portmaster-core" // identifier, use forward slash!
|
||||||
|
if onWindows {
|
||||||
|
identifier += exeExt
|
||||||
|
}
|
||||||
|
|
||||||
|
// get newest portmaster-core
|
||||||
|
new, err := GetPlatformFile(identifier)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pmCoreUpdate = new
|
||||||
|
|
||||||
|
// check for new version
|
||||||
if info.GetInfo().Version != pmCoreUpdate.Version() {
|
if info.GetInfo().Version != pmCoreUpdate.Version() {
|
||||||
n := notifications.NotifyInfo(
|
n := notifications.NotifyInfo(
|
||||||
"updates:core-update-available",
|
"updates:core-update-available",
|
||||||
|
@ -143,24 +141,24 @@ func upgradeHub() error {
|
||||||
if hubUpgradeStarted {
|
if hubUpgradeStarted {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if spnHubUpdate != nil && !spnHubUpdate.UpgradeAvailable() {
|
||||||
// check if we can upgrade
|
|
||||||
if spnHubUpdate == nil || spnHubUpdate.UpgradeAvailable() {
|
|
||||||
identifier := "hub/spn-hub" // identifier, use forward slash!
|
|
||||||
if onWindows {
|
|
||||||
identifier += exeExt
|
|
||||||
}
|
|
||||||
|
|
||||||
// get newest spn-hub
|
|
||||||
new, err := GetPlatformFile(identifier)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
spnHubUpdate = new
|
|
||||||
} else {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make identifier
|
||||||
|
identifier := "hub/spn-hub" // identifier, use forward slash!
|
||||||
|
if onWindows {
|
||||||
|
identifier += exeExt
|
||||||
|
}
|
||||||
|
|
||||||
|
// get newest spn-hub
|
||||||
|
new, err := GetPlatformFile(identifier)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
spnHubUpdate = new
|
||||||
|
|
||||||
|
// check for new version
|
||||||
if info.GetInfo().Version != spnHubUpdate.Version() {
|
if info.GetInfo().Version != spnHubUpdate.Version() {
|
||||||
// get random delay with up to three hours
|
// get random delay with up to three hours
|
||||||
delayMinutes, err := rng.Number(3 * 60)
|
delayMinutes, err := rng.Number(3 * 60)
|
||||||
|
@ -168,7 +166,7 @@ func upgradeHub() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
triggerRestart(time.Duration(delayMinutes) * time.Minute)
|
DelayedRestart(time.Duration(delayMinutes) * time.Minute)
|
||||||
hubUpgradeStarted = true
|
hubUpgradeStarted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue