mirror of
https://github.com/safing/portmaster
synced 2025-09-04 11:39:29 +00:00
Allow modules to trigger the update task
This commit is contained in:
parent
2987100c07
commit
5067576260
1 changed files with 35 additions and 9 deletions
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
"github.com/safing/portbase/updater"
|
"github.com/safing/portbase/updater"
|
||||||
|
"github.com/tevino/abool"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -41,6 +42,8 @@ const (
|
||||||
var (
|
var (
|
||||||
module *modules.Module
|
module *modules.Module
|
||||||
registry *updater.ResourceRegistry
|
registry *updater.ResourceRegistry
|
||||||
|
updateTask *modules.Task
|
||||||
|
updateASAP = abool.New()
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -106,17 +109,40 @@ func start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// start updater task
|
// start updater task
|
||||||
module.NewTask("updater", func(ctx context.Context, task *modules.Task) error {
|
updateTask = module.NewTask("updater", func(ctx context.Context, task *modules.Task) error {
|
||||||
|
return checkForUpdates(ctx)
|
||||||
|
}).Repeat(24 * time.Hour).MaxDelay(1 * time.Hour).Schedule(time.Now().Add(10 * time.Second))
|
||||||
|
|
||||||
|
if updateASAP.IsSet() {
|
||||||
|
updateTask.StartASAP()
|
||||||
|
}
|
||||||
|
|
||||||
|
// react to upgrades
|
||||||
|
return initUpgrader()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TriggerUpdate queues the update task to execute ASAP.
|
||||||
|
func TriggerUpdate() error {
|
||||||
|
if updateTask == nil {
|
||||||
|
if !module.OnlineSoon() {
|
||||||
|
return fmt.Errorf("module not started")
|
||||||
|
}
|
||||||
|
|
||||||
|
updateASAP.Set()
|
||||||
|
} else {
|
||||||
|
updateTask.Queue()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkForUpdates(ctx context.Context) error {
|
||||||
err := registry.DownloadUpdates(ctx)
|
err := registry.DownloadUpdates(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("updates: failed to update: %s", err)
|
return fmt.Errorf("updates: failed to update: %s", err)
|
||||||
}
|
}
|
||||||
module.TriggerEvent(ResourceUpdateEvent, nil)
|
module.TriggerEvent(ResourceUpdateEvent, nil)
|
||||||
return nil
|
return nil
|
||||||
}).Repeat(24 * time.Hour).MaxDelay(1 * time.Hour).Schedule(time.Now().Add(10 * time.Second))
|
|
||||||
|
|
||||||
// react to upgrades
|
|
||||||
return initUpgrader()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func stop() error {
|
func stop() error {
|
||||||
|
|
Loading…
Add table
Reference in a new issue