alert

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 21 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 alert tells people when tasks and schedule runs fail: by email, or in Slack through an incoming webhook.

The Notifier runs on the dispatcher's leader. Every Interval it finds the queues and schedules with failures since their last alert and, once a subject's cooldown has passed, sends each of the workspace's channels one message covering them all. Alerts are sent at least once: a failed send is retried on the next check, and a channel that already got it may get it again.

Index

Constants

View Source
const MaxChannels = 10

MaxChannels is how many channels a workspace can have.

View Source
const SlackPrefix = "https://hooks.slack.com/"

SlackPrefix is how every Slack incoming webhook URL starts. Only these are accepted, so a channel can't make Workly call other addresses.

Variables

View Source
var ErrNoMailer = errors.New("this server isn't set up to send email")

ErrNoMailer is returned when sending email from a server that can't.

Functions

func Masked

func Masked(c store.AlertChannel) string

Masked returns a channel's target to show: a Slack webhook URL is a secret, so only its start and last characters are shown.

func ParseTarget

func ParseTarget(typ store.AlertChannelType, target string) (string, error)

ParseTarget checks a channel's target and returns it as stored.

func WorkspaceName

func WorkspaceName(ctx context.Context, st store.Store, ws workspace.ID) (string, error)

WorkspaceName names the workspace in alerts, unless it's the only one.

Types

type Email

type Email struct {
	To      string
	Subject string
	Text    string
}

Email is an alert by email, in plain text.

type Links string

Links builds links into the dashboard at its base URL, e.g. https://workly.example.com. The zero value builds none.

type Mailer

type Mailer interface {
	// CheckRecipient is called before an email channel is added and may
	// refuse the address, e.g. to only send to a workspace's members.
	CheckRecipient(ctx context.Context, ws workspace.ID, address string) error
	Send(ctx context.Context, ws workspace.ID, e Email) error
}

Mailer sends alert emails. A server without one can't have email channels.

type Message

type Message struct {
	Subject string
	// Text is the email body.
	Text string
	// Slack is the Slack message, in Slack's mrkdwn.
	Slack string
}

Message is an alert, written for both kinds of channel.

type Notifier

type Notifier struct {
	Store  store.Store
	Sender *Sender
	Logger *slog.Logger
	// Interval is how often failures are checked. Default 1m.
	Interval time.Duration
	// Cooldown is how long a queue or schedule waits after an alert before
	// the next. Default 30m.
	Cooldown time.Duration
	// Settle leaves out failures this recent, whose results may still be
	// on their way to the database with an earlier finish time. Default
	// 15s.
	Settle time.Duration
}

Notifier sends the alerts. Run it on one process at a time (the dispatcher's leader).

func (*Notifier) AutoPaused added in v0.5.0

func (n *Notifier) AutoPaused(ev store.AutoPauseEvent)

AutoPaused sends the alert about a queue that paused itself or resumed, at once rather than at the next check, and outside the cooldown: both are news. It returns at once and sends in the background.

func (*Notifier) Check

func (n *Notifier) Check(ctx context.Context, now time.Time)

Check sends the alerts due at now.

func (*Notifier) Run

func (n *Notifier) Run(ctx context.Context)

Run checks for failures every Interval until ctx is cancelled.

type SMTP

type SMTP struct {
	// contains filtered or unexported fields
}

SMTP sends alert emails through an SMTP server.

func NewSMTP

func NewSMTP(rawURL, from string) (*SMTP, error)

NewSMTP parses an SMTP URL: smtp://user:pass@host:587 upgrades the connection with STARTTLS when the server offers it (and refuses to send the password otherwise, except to localhost), smtps://user:pass@host:465 uses TLS from the start. from is the sender, e.g. "Workly <alerts@example.com>".

func (*SMTP) CheckRecipient

func (s *SMTP) CheckRecipient(context.Context, workspace.ID, string) error

CheckRecipient accepts any address: whoever runs the server chooses them.

func (*SMTP) Send

func (s *SMTP) Send(ctx context.Context, _ workspace.ID, e Email) error

type Sender

type Sender struct {
	// Mailer is nil if the server can't send email.
	Mailer Mailer
	// Client posts to Slack. It must not follow redirects.
	Client *http.Client
	// Links builds links to the dashboard; it's empty when the server
	// doesn't know its URL.
	Links Links
}

Sender sends messages to channels.

func NewSender

func NewSender(mailer Mailer, links Links) *Sender

NewSender returns a Sender with a client fit for Slack webhooks.

func (*Sender) CanSend

func (s *Sender) CanSend(c store.AlertChannel) bool

CanSend reports whether the channel's type can be sent to.

func (*Sender) Send

func (s *Sender) Send(ctx context.Context, c store.AlertChannel, m Message) error

Send sends m to the channel.

func (*Sender) Test

func (s *Sender) Test(ctx context.Context, workspaceName string, c store.AlertChannel) error

Test sends a test alert to the channel of the named workspace.

Jump to

Keyboard shortcuts

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