mirror of
https://github.com/safing/portmaster
synced 2025-09-01 18:19:12 +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/modules"
|
||||
"github.com/safing/portbase/updater"
|
||||
"github.com/tevino/abool"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -39,8 +40,10 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
module *modules.Module
|
||||
registry *updater.ResourceRegistry
|
||||
module *modules.Module
|
||||
registry *updater.ResourceRegistry
|
||||
updateTask *modules.Task
|
||||
updateASAP = abool.New()
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -106,19 +109,42 @@ func start() error {
|
|||
}
|
||||
|
||||
// start updater task
|
||||
module.NewTask("updater", func(ctx context.Context, task *modules.Task) error {
|
||||
err := registry.DownloadUpdates(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("updates: failed to update: %s", err)
|
||||
}
|
||||
module.TriggerEvent(ResourceUpdateEvent, nil)
|
||||
return nil
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("updates: failed to update: %s", err)
|
||||
}
|
||||
module.TriggerEvent(ResourceUpdateEvent, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
func stop() error {
|
||||
if registry != nil {
|
||||
return registry.Cleanup()
|
||||
|
|
Loading…
Add table
Reference in a new issue