Update and improve events according to new system

This commit is contained in:
Daniel
2021-05-05 00:29:53 +02:00
parent 2e73d4fbe6
commit dfc1cdeb1c
9 changed files with 66 additions and 249 deletions

View File

@@ -12,6 +12,7 @@ import (
"github.com/safing/portbase/dataroot"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/notifications"
"github.com/safing/portbase/updater"
)
@@ -41,10 +42,6 @@ const (
// to check if new versions of their resources are
// available by checking File.UpgradeAvailable().
ResourceUpdateEvent = "resource update"
// TriggerUpdateEvent is the event that can be emitted
// by the updates module to trigger an update.
TriggerUpdateEvent = "trigger update"
)
var (
@@ -68,18 +65,17 @@ var (
)
const (
updateInProgress = "update-in-progress"
updateInProcessDescr = "Portmaster is currently checking and downloading updates."
updateFailed = "update-failed"
updateInProgress = "updates:in-progress"
updateFailed = "updates:failed"
)
func init() {
module = modules.Register(ModuleName, prep, start, stop, "base")
module.RegisterEvent(VersionUpdateEvent)
module.RegisterEvent(ResourceUpdateEvent)
module.RegisterEvent(VersionUpdateEvent, true)
module.RegisterEvent(ResourceUpdateEvent, true)
flag.StringVar(&userAgentFromFlag, "update-agent", "", "Sets the user agent for requests to the update server")
flag.BoolVar(&staging, "staging", false, "Use staging update channel (for testing only)")
flag.StringVar(&userAgentFromFlag, "update-agent", "", "set the user agent for requests to the update server")
flag.BoolVar(&staging, "staging", false, "use staging update channel; for testing only")
// initialize mandatory updates
if onWindows {
@@ -109,7 +105,7 @@ func prep() error {
return err
}
module.RegisterEvent(TriggerUpdateEvent)
return registerAPIEndpoints()
return nil
}
@@ -127,18 +123,6 @@ func start() error {
return err
}
if err := module.RegisterEventHook(
module.Name,
TriggerUpdateEvent,
"Check for and download available updates",
func(context.Context, interface{}) error {
_ = TriggerUpdate()
return nil
},
); err != nil {
return err
}
// create registry
registry = &updater.ResourceRegistry{
Name: ModuleName,
@@ -244,17 +228,21 @@ func start() error {
// TriggerUpdate queues the update task to execute ASAP.
func TriggerUpdate() error {
if !module.Online() {
if !module.OnlineSoon() {
return fmt.Errorf("module not enabled")
}
switch {
case !module.OnlineSoon():
return fmt.Errorf("updates module is disabled")
case !module.Online():
updateASAP = true
} else {
case !enableUpdates():
return fmt.Errorf("automatic updating is disabled")
default:
updateTask.StartASAP()
log.Debugf("updates: triggering update to run as soon as possible")
}
log.Debugf("updates: triggering update to run as soon as possible")
return nil
}

View File

@@ -135,18 +135,10 @@ func upgradeCoreNotify() error {
func upgradeCoreNotifyActionHandler(_ context.Context, n *notifications.Notification) error {
switch n.SelectedActionID {
case "restart":
// Cannot directly trigger due to import loop.
err := module.InjectEvent(
"user triggered restart via notification",
"core",
"restart",
nil,
)
if err != nil {
return fmt.Errorf("failed to trigger restart via notification: %s", err)
}
log.Infof("updates: user triggered restart via core update notification")
RestartNow()
case "later":
return n.Delete()
n.Delete()
}
return nil
@@ -266,7 +258,7 @@ func warnOnIncorrectParentPath() {
absPath,
filepath.Join(root, expectedFileName),
),
})
)
}
}