mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Clean up notifications package, transition to new module tasks
This commit is contained in:
parent
77ed019f27
commit
3672fa9f45
4 changed files with 19 additions and 25 deletions
|
@ -1,19 +1,21 @@
|
||||||
package notifications
|
package notifications
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func cleaner() {
|
//nolint:unparam // must conform to interface
|
||||||
shutdownWg.Add(1)
|
func cleaner(ctx context.Context) error {
|
||||||
select {
|
for {
|
||||||
case <-shutdownSignal:
|
select {
|
||||||
shutdownWg.Done()
|
case <-ctx.Done():
|
||||||
return
|
return nil
|
||||||
case <-time.After(5 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
cleanNotifications()
|
cleanNotifications()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -138,7 +138,7 @@ func UpdateNotification(n *Notification, key string) {
|
||||||
n.Lock()
|
n.Lock()
|
||||||
defer n.Unlock()
|
defer n.Unlock()
|
||||||
|
|
||||||
// seperate goroutine in order to correctly lock notsLock
|
// separate goroutine in order to correctly lock notsLock
|
||||||
notsLock.RLock()
|
notsLock.RLock()
|
||||||
origN, ok := nots[key]
|
origN, ok := nots[key]
|
||||||
notsLock.RUnlock()
|
notsLock.RUnlock()
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
package notifications
|
package notifications
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"time"
|
||||||
|
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
shutdownSignal = make(chan struct{})
|
module *modules.Module
|
||||||
shutdownWg sync.WaitGroup
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -21,12 +20,6 @@ func start() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
go cleaner()
|
go module.StartServiceWorker("cleaner", 1*time.Second, cleaner)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func stop() error {
|
|
||||||
close(shutdownSignal)
|
|
||||||
shutdownWg.Wait()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,15 +32,15 @@ type Notification struct {
|
||||||
DataSubject sync.Locker
|
DataSubject sync.Locker
|
||||||
Type uint8
|
Type uint8
|
||||||
|
|
||||||
AvailableActions []*Action
|
|
||||||
SelectedActionID string
|
|
||||||
|
|
||||||
Persistent bool // this notification persists until it is handled and survives restarts
|
Persistent bool // this notification persists until it is handled and survives restarts
|
||||||
Created int64 // creation timestamp, notification "starts"
|
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
|
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"
|
Responded int64 // response timestamp, notification "ends"
|
||||||
Executed int64 // execution timestamp, notification will be deleted soon
|
Executed int64 // execution timestamp, notification will be deleted soon
|
||||||
|
|
||||||
|
AvailableActions []*Action
|
||||||
|
SelectedActionID string
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
actionFunction func(*Notification) // call function to process action
|
actionFunction func(*Notification) // call function to process action
|
||||||
actionTrigger chan string // and/or send to a channel
|
actionTrigger chan string // and/or send to a channel
|
||||||
|
@ -54,7 +54,6 @@ type Action struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func noOpAction(n *Notification) {
|
func noOpAction(n *Notification) {
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the notification identifed by the given id or nil if it doesn't exist.
|
// 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.
|
// 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 {
|
func (n *Notification) SetActionFunction(fn func(*Notification)) *Notification {
|
||||||
n.lock.Lock()
|
n.lock.Lock()
|
||||||
defer n.lock.Unlock()
|
defer n.lock.Unlock()
|
||||||
|
@ -139,7 +138,7 @@ func (n *Notification) MakeAck() *Notification {
|
||||||
defer n.lock.Unlock()
|
defer n.lock.Unlock()
|
||||||
|
|
||||||
n.AvailableActions = []*Action{
|
n.AvailableActions = []*Action{
|
||||||
&Action{
|
{
|
||||||
ID: "ack",
|
ID: "ack",
|
||||||
Text: "OK",
|
Text: "OK",
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue