From 5067576260ed2e67df3a9ce776d987c059c05b15 Mon Sep 17 00:00:00 2001 From: Patrick Pacher Date: Mon, 6 Apr 2020 08:59:08 +0000 Subject: [PATCH] Allow modules to trigger the update task --- updates/main.go | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/updates/main.go b/updates/main.go index adbb7e28..9f6a07e2 100644 --- a/updates/main.go +++ b/updates/main.go @@ -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()