From 5067576260ed2e67df3a9ce776d987c059c05b15 Mon Sep 17 00:00:00 2001 From: Patrick Pacher Date: Mon, 6 Apr 2020 08:59:08 +0000 Subject: [PATCH 1/3] 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() From 30800f7383df6e00ab19d76657fa0d07ddfe2889 Mon Sep 17 00:00:00 2001 From: ppacher Date: Mon, 6 Apr 2020 12:35:29 +0200 Subject: [PATCH 2/3] Allow to disable updates schedule --- updates/main.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/updates/main.go b/updates/main.go index 9f6a07e2..240924fc 100644 --- a/updates/main.go +++ b/updates/main.go @@ -40,10 +40,11 @@ const ( ) var ( - module *modules.Module - registry *updater.ResourceRegistry - updateTask *modules.Task - updateASAP = abool.New() + module *modules.Module + registry *updater.ResourceRegistry + updateTask *modules.Task + updateASAP = abool.New() + disableTaskSchedule bool ) func init() { @@ -111,7 +112,14 @@ func start() error { // start updater task 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 !disableTaskSchedule { + updateTask. + Repeat(24 * time.Hour). + MaxDelay(1 * time.Hour). + Schedule(time.Now().Add(10 * time.Second)) + } if updateASAP.IsSet() { updateTask.StartASAP() @@ -136,6 +144,19 @@ func TriggerUpdate() error { return nil } +// DisableUpdateSchedule disables the update schedule. +// If called, updates are only checked when TriggerUpdate() +// is called. +func DisableUpdateSchedule() error { + if module.Online() { + return fmt.Errorf("module already online") + } + + disableTaskSchedule = true + + return nil +} + func checkForUpdates(ctx context.Context) error { err := registry.DownloadUpdates(ctx) if err != nil { From 65089f47ddf3d9415abbf5e6ed5a9e2e73d64347 Mon Sep 17 00:00:00 2001 From: ppacher Date: Mon, 6 Apr 2020 13:14:45 +0200 Subject: [PATCH 3/3] Change error msg and use module online check in TriggerUpdate() --- updates/main.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/updates/main.go b/updates/main.go index 240924fc..f2c9b78c 100644 --- a/updates/main.go +++ b/updates/main.go @@ -10,7 +10,6 @@ import ( "github.com/safing/portbase/log" "github.com/safing/portbase/modules" "github.com/safing/portbase/updater" - "github.com/tevino/abool" ) const ( @@ -43,7 +42,7 @@ var ( module *modules.Module registry *updater.ResourceRegistry updateTask *modules.Task - updateASAP = abool.New() + updateASAP bool disableTaskSchedule bool ) @@ -121,7 +120,7 @@ func start() error { Schedule(time.Now().Add(10 * time.Second)) } - if updateASAP.IsSet() { + if updateASAP { updateTask.StartASAP() } @@ -131,14 +130,14 @@ func start() error { // TriggerUpdate queues the update task to execute ASAP. func TriggerUpdate() error { - if updateTask == nil { + if !module.Online() { if !module.OnlineSoon() { - return fmt.Errorf("module not started") + return fmt.Errorf("module not enabled") } - updateASAP.Set() + updateASAP = true } else { - updateTask.Queue() + updateTask.StartASAP() } return nil @@ -148,7 +147,7 @@ func TriggerUpdate() error { // If called, updates are only checked when TriggerUpdate() // is called. func DisableUpdateSchedule() error { - if module.Online() { + if module.OnlineSoon() { return fmt.Errorf("module already online") }