Documentation
¶
Overview ¶
Package notifications provides a notification system.
Notification Lifecycle ¶
1. Create Notification with an ID and Message. 2. Set possible actions and save it. 3. When the user responds, the action is executed.
Example
// create notification n := notifications.New("update-available", "A new update is available. Restart to upgrade.") // set actions and save n.AddAction("later", "Later").AddAction("restart", "Restart now!").Save() // wait for user action selectedAction := <-n.Response() switch selectedAction { case "later": log.Infof("user wants to upgrade later.") case "restart": log.Infof("user wants to restart now.") }
Index ¶
- Constants
- Variables
- func SetPersistenceBasePath(dbBasePath string)
- type Action
- type Notification
- func (n *Notification) AddDataSubject(ds sync.Locker)
- func (n *Notification) Cancel()
- func (n *Notification) Init() *Notification
- func (n *Notification) Lock()
- func (n *Notification) MakeAck() *Notification
- func (n *Notification) Response() <-chan string
- func (n *Notification) Save() *Notification
- func (n *Notification) SelectAndExecuteAction(id string)
- func (n *Notification) SetActionFunction(fn func(*Notification)) *Notification
- func (n *Notification) Unlock()
- type StorageInterface
- func (s *StorageInterface) Delete(key string) error
- func (s *StorageInterface) Get(key string) (record.Record, error)
- func (s *StorageInterface) Put(r record.Record) error
- func (s *StorageInterface) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error)
- func (s *StorageInterface) ReadOnly() bool
Constants ¶
const ( Info uint8 = 0 Warning uint8 = 1 Prompt uint8 = 2 )
Notification types
Variables ¶
var ( ErrInvalidData = errors.New("invalid data, must be a notification object") ErrInvalidPath = errors.New("invalid path") ErrNoDelete = errors.New("notifications may not be deleted, they must be handled") )
Storage interface errors
Functions ¶
func SetPersistenceBasePath ¶
func SetPersistenceBasePath(dbBasePath string)
SetPersistenceBasePath sets the base path for persisting persistent notifications.
Types ¶
type Notification ¶
type Notification struct { record.Base ID string Message string // MessageTemplate string // MessageData []string 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 // contains filtered or unexported fields }
Notification represents a notification that is to be delivered to the user.
func EnsureNotification ¶
func EnsureNotification(r record.Record) (*Notification, error)
EnsureNotification ensures that the given record is a Notification and returns it.
func Get ¶
func Get(id string) *Notification
Get returns the notification identifed by the given id or nil if it doesn't exist.
func (*Notification) AddDataSubject ¶
func (n *Notification) AddDataSubject(ds sync.Locker)
AddDataSubject adds the data subject to the notification. This is the only way how a data subject should be added - it avoids locking problems.
func (*Notification) Cancel ¶
func (n *Notification) Cancel()
Cancel (prematurely) destroys a notification.
func (*Notification) Init ¶
func (n *Notification) Init() *Notification
Init initializes a Notification and returns it.
func (*Notification) Lock ¶
func (n *Notification) Lock()
Lock locks the Notification and the DataSubject, if available.
func (*Notification) MakeAck ¶
func (n *Notification) MakeAck() *Notification
MakeAck sets a default "OK" action and a no-op action function.
func (*Notification) Response ¶
func (n *Notification) Response() <-chan string
Response waits for the user to respond to the notification and returns the selected action.
func (*Notification) Save ¶
func (n *Notification) Save() *Notification
Save saves the notification and returns it.
func (*Notification) SelectAndExecuteAction ¶
func (n *Notification) SelectAndExecuteAction(id string)
SelectAndExecuteAction sets the user response and executes/triggers the action, if possible.
func (*Notification) SetActionFunction ¶
func (n *Notification) SetActionFunction(fn func(*Notification)) *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.
func (*Notification) Unlock ¶
func (n *Notification) Unlock()
Unlock unlocks the Notification and the DataSubject, if available.
type StorageInterface ¶
type StorageInterface struct {
storage.InjectBase
}
StorageInterface provices a storage.Interface to the configuration manager.
func (*StorageInterface) Delete ¶
func (s *StorageInterface) Delete(key string) error
Delete deletes a record from the database.
func (*StorageInterface) Get ¶
func (s *StorageInterface) Get(key string) (record.Record, error)
Get returns a database record.
func (*StorageInterface) Put ¶
func (s *StorageInterface) Put(r record.Record) error
Put stores a record in the database.
func (*StorageInterface) ReadOnly ¶
func (s *StorageInterface) ReadOnly() bool
ReadOnly returns whether the database is read only.