From 3672fa9f4585f7d311c5136972eb729b57aedad0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 20 Sep 2019 22:04:53 +0200 Subject: [PATCH] Clean up notifications package, transition to new module tasks --- notifications/cleaner.go | 18 ++++++++++-------- notifications/database.go | 2 +- notifications/module.go | 13 +++---------- notifications/notification.go | 11 +++++------ 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/notifications/cleaner.go b/notifications/cleaner.go index efd0408..94300c5 100644 --- a/notifications/cleaner.go +++ b/notifications/cleaner.go @@ -1,19 +1,21 @@ package notifications import ( + "context" "time" "github.com/safing/portbase/log" ) -func cleaner() { - shutdownWg.Add(1) - select { - case <-shutdownSignal: - shutdownWg.Done() - return - case <-time.After(5 * time.Second): - cleanNotifications() +//nolint:unparam // must conform to interface +func cleaner(ctx context.Context) error { + for { + select { + case <-ctx.Done(): + return nil + case <-time.After(5 * time.Second): + cleanNotifications() + } } } diff --git a/notifications/database.go b/notifications/database.go index 2cfd29f..debeffc 100644 --- a/notifications/database.go +++ b/notifications/database.go @@ -138,7 +138,7 @@ func UpdateNotification(n *Notification, key string) { n.Lock() defer n.Unlock() - // seperate goroutine in order to correctly lock notsLock + // separate goroutine in order to correctly lock notsLock notsLock.RLock() origN, ok := nots[key] notsLock.RUnlock() diff --git a/notifications/module.go b/notifications/module.go index 63902e2..c0ce406 100644 --- a/notifications/module.go +++ b/notifications/module.go @@ -1,14 +1,13 @@ package notifications import ( - "sync" + "time" "github.com/safing/portbase/modules" ) var ( - shutdownSignal = make(chan struct{}) - shutdownWg sync.WaitGroup + module *modules.Module ) func init() { @@ -21,12 +20,6 @@ func start() error { return err } - go cleaner() - return nil -} - -func stop() error { - close(shutdownSignal) - shutdownWg.Wait() + go module.StartServiceWorker("cleaner", 1*time.Second, cleaner) return nil } diff --git a/notifications/notification.go b/notifications/notification.go index bb5fe58..52af0fc 100644 --- a/notifications/notification.go +++ b/notifications/notification.go @@ -32,15 +32,15 @@ type Notification struct { DataSubject sync.Locker Type uint8 - AvailableActions []*Action - SelectedActionID string - Persistent bool // this notification persists until it is handled and survives restarts Created int64 // creation timestamp, notification "starts" Expires int64 // expiry timestamp, notification is expected to be canceled at this time and may be cleaned up afterwards Responded int64 // response timestamp, notification "ends" Executed int64 // execution timestamp, notification will be deleted soon + AvailableActions []*Action + SelectedActionID string + lock sync.Mutex actionFunction func(*Notification) // call function to process action actionTrigger chan string // and/or send to a channel @@ -54,7 +54,6 @@ type Action struct { } func noOpAction(n *Notification) { - return } // Get returns the notification identifed by the given id or nil if it doesn't exist. @@ -125,7 +124,7 @@ func (n *Notification) Save() *Notification { } // SetActionFunction sets a trigger function to be executed when the user reacted on the notification. -// The provided funtion will be started as its own goroutine and will have to lock everything it accesses, even the provided notification. +// The provided function will be started as its own goroutine and will have to lock everything it accesses, even the provided notification. func (n *Notification) SetActionFunction(fn func(*Notification)) *Notification { n.lock.Lock() defer n.lock.Unlock() @@ -139,7 +138,7 @@ func (n *Notification) MakeAck() *Notification { defer n.lock.Unlock() n.AvailableActions = []*Action{ - &Action{ + { ID: "ack", Text: "OK", },