notify

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: AGPL-3.0 Imports: 28 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package notify sends events to notification channels: webhooks, chat services, push services and email (#86, ADR-0034). Channels are configured by administrators and stored in the database; each receives the event kinds it subscribed to.

Index

Constants

View Source
const (
	KindImageUpdate        = "image.update"        // a tag in use points to a new image (#96)
	KindAutoUpdateDone     = "autoupdate.updated"  // an automatic update succeeded (#54)
	KindAutoUpdateFailed   = "autoupdate.failed"   // an automatic update failed and was rolled back
	KindAutoUpdatePending  = "autoupdate.pending"  // automatic updates wait for approval (#56)
	KindContainerDied      = "container.died"      // a container exited with an error code
	KindContainerOOM       = "container.oom"       // a container ran out of memory
	KindContainerUnhealthy = "container.unhealthy" // a container's healthcheck started failing (#22)
	KindContainerAutoHeal  = "container.autoheal"  // nerdui restarted an unhealthy container, or gave up (#22)
	KindJobFailed          = "job.failed"          // a background operation failed
	KindGitUpdate          = "git.update"          // a Git stack has a new commit or was updated from it (#38, #39)
	KindHostAlert          = "host.alert"          // host CPU, memory or disk crossed a threshold, or recovered (#64)
	KindTest               = "test"                // sent by "Send test"; every channel receives it
)

Event kinds. Keep in sync with api/openapi.yaml and the web UI.

View Source
const (
	SeverityInfo    = "info"
	SeverityWarning = "warning"
	SeverityError   = "error"
)

Severities.

View Source
const (
	TypeWebhook  = "webhook"
	TypeSlack    = "slack"
	TypeDiscord  = "discord"
	TypeTelegram = "telegram"
	TypeNtfy     = "ntfy"
	TypeGotify   = "gotify"
	TypeEmail    = "email"
)

Channel types.

View Source
const DedupeWindow = 15 * time.Minute

DedupeWindow suppresses repeats of an event (same kind, environment, namespace and object).

Variables

Kinds are the event kinds a channel can subscribe to.

Types are the supported channel types.

Functions

This section is empty.

Types

type Channel

type Channel struct {
	ID      int64
	Name    string
	Type    string
	Config  Config
	Events  []string
	Enabled bool
	// LastSentAt and LastError describe the newest delivery attempt.
	LastSentAt time.Time
	LastError  string
}

Channel is a configured notification channel.

func FromRow

func FromRow(r db.NotificationChannel) (Channel, error)

FromRow decodes a stored channel.

func (Channel) Validate

func (ch Channel) Validate() error

Validate checks a channel's name, type, events and the settings its type needs.

type Config

type Config struct {
	// URL: webhook, Slack and Discord webhook URL, ntfy topic URL, Gotify server URL.
	URL string `json:"url,omitempty"`
	// Secret: HMAC key of webhook signatures. Write-only.
	Secret string `json:"secret,omitempty"`
	// Token: Telegram bot token, ntfy access token, Gotify application token. Write-only.
	Token string `json:"token,omitempty"`
	// ChatID: Telegram chat.
	ChatID string `json:"chatId,omitempty"`
	// Email.
	Host     string `json:"host,omitempty"`
	Port     int    `json:"port,omitempty"`
	TLS      string `json:"tls,omitempty"` // starttls (default), tls or none
	Username string `json:"username,omitempty"`
	Password string `json:"password,omitempty"` // write-only
	From     string `json:"from,omitempty"`
	To       string `json:"to,omitempty"` // comma-separated
}

Config is a channel's settings; which fields apply depends on the type. Keep in sync with api/openapi.yaml.

func (*Config) KeepSecrets

func (c *Config) KeepSecrets(typ string, stored Config)

KeepSecrets fills secrets left empty in c from the stored config, when the type is unchanged.

func (Config) Masked

func (c Config) Masked(typ string) (Config, []string)

Masked returns the config without secrets, and which secrets are set.

type Event

type Event struct {
	Kind        string `json:"kind"`
	Severity    string `json:"severity"`
	Title       string `json:"title"`
	Message     string `json:"message,omitempty"`
	Host        string `json:"host,omitempty"`
	Environment string `json:"environment,omitempty"`
	Namespace   string `json:"namespace,omitempty"`
	// Object names what the event is about (container, stack, image reference, job); events of
	// the same kind and object are sent at most once per DedupeWindow.
	Object string    `json:"object,omitempty"`
	Time   time.Time `json:"time"`
}

Event is something to tell people about. It is the webhook payload: keep in sync with docs/notifications.md.

func TestEvent

func TestEvent() Event

TestEvent is sent by "Send test".

type Service

type Service struct {
	Store  Store
	Host   string // shown in messages: which server the event comes from
	Client *http.Client
	Logger *slog.Logger
	Now    func() time.Time
	// contains filtered or unexported fields
}

Service queues events and sends them to the channels subscribed to their kind.

func (*Service) Notify

func (s *Service) Notify(ev Event)

Notify queues an event without blocking; it is dropped when the queue is full or a repeat within DedupeWindow. A nil Service ignores events.

func (*Service) Run

func (s *Service) Run(ctx context.Context)

Run sends queued events until ctx ends.

func (*Service) Send

func (s *Service) Send(ctx context.Context, ch Channel, ev Event) error

Send delivers one event to a channel now and records the result on the channel.

type Store

type Store interface {
	ListNotificationChannels(ctx context.Context) ([]db.NotificationChannel, error)
	SetNotificationResult(ctx context.Context, arg db.SetNotificationResultParams) error
}

Store is the database of channels; *db.Queries of the write connection implements it.

type Watcher

type Watcher struct {
	Env      *environment.Environment
	Notifier *Service
	Logger   *slog.Logger
	// Delay overrides settleDelay (tests).
	Delay time.Duration
}

Watcher turns containerd events of an environment into notifications about containers that crashed or ran out of memory (#86).

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context)

Run watches until ctx ends.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL