Clean up notifications package, transition to new module tasks

This commit is contained in:
Daniel 2019-09-20 22:04:53 +02:00
parent 77ed019f27
commit 3672fa9f45
4 changed files with 19 additions and 25 deletions

View file

@ -1,21 +1,23 @@
package notifications
import (
"context"
"time"
"github.com/safing/portbase/log"
)
func cleaner() {
shutdownWg.Add(1)
//nolint:unparam // must conform to interface
func cleaner(ctx context.Context) error {
for {
select {
case <-shutdownSignal:
shutdownWg.Done()
return
case <-ctx.Done():
return nil
case <-time.After(5 * time.Second):
cleanNotifications()
}
}
}
func cleanNotifications() {
now := time.Now().Unix()

View file

@ -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()

View file

@ -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
}

View file

@ -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",
},