monitor

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: AGPL-3.0 Imports: 8 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 monitor keeps an eye on the things Mediarium depends on (Usenet servers, indexers) by checking each one on a schedule, remembering the last result, and raising a notification only when something changes state, so a server that stays down is reported once, not every cycle.

The package knows nothing about how a check is made: callers hand it a function that lists the current checks (internal/api wires in the real Usenet login and indexer probes), a clock and an interval, all injectable so the logic is testable without waiting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hint

func Hint(err error) string

Hint turns a failure into a sentence a person can act on.

func Redact

func Redact(msg string) string

Redact masks credential-looking query parameters in an error message.

Types

type Check

type Check struct {
	Kind Kind
	ID   int64
	Name string
	Run  func(ctx context.Context) error
}

Check is one thing to test. Run returns nil when it is healthy.

type Event

type Event struct {
	Title   string
	Message string
	Failing bool // false means "recovered"

	Kind  Kind // what sort of thing it is, and its name
	Name  string
	Error string // what the failed check said, with credentials masked (empty when recovered)
}

Event is what the monitor asks to have announced: a check that started failing, or one that recovered.

type Kind

type Kind string

Kind is what sort of thing a check covers.

const (
	KindUsenet  Kind = "usenet"
	KindIndexer Kind = "indexer"
)

type Monitor

type Monitor struct {
	// Checks lists what to test right now (the enabled servers and indexers).
	Checks func(ctx context.Context) ([]Check, error)
	// Interval is read before every wait, so a settings change takes effect on
	// the next cycle. Zero or less means the monitor is off.
	Interval func() time.Duration
	// Notify announces a state change. May be nil.
	Notify func(Event)

	// Now is the clock (default time.Now).
	Now func() time.Time
	// After returns a channel that fires after d (default time.After); tests
	// replace it to drive the loop without sleeping.
	After func(d time.Duration) <-chan time.Time
	// InitialDelay is how long the loop waits before its first pass, giving the
	// network time to come up after a restart. Default one minute.
	InitialDelay time.Duration
	// OffPoll is how often an "off" monitor re-reads Interval to notice it was
	// switched on. Default one minute.
	OffPoll time.Duration
	// contains filtered or unexported fields
}

Monitor runs checks and tracks their state. Fill in the function fields, then call Run (background loop) or RunOnce (a single pass).

func (*Monitor) Run

func (m *Monitor) Run(ctx context.Context)

Run loops until ctx is done: wait, run a pass, repeat. With the interval at zero it runs nothing, just keeps watching for the setting to change.

func (*Monitor) RunOnce

func (m *Monitor) RunOnce(ctx context.Context)

RunOnce checks everything now, updates the stored results and announces any state changes. Concurrent calls queue up rather than overlap.

func (*Monitor) Statuses

func (m *Monitor) Statuses() []Status

Statuses returns the last result of every check, ordered by kind, then name.

type Status

type Status struct {
	Kind      Kind      `json:"kind"`
	ID        int64     `json:"id"`
	Name      string    `json:"name"`
	OK        bool      `json:"ok"`
	CheckedAt time.Time `json:"checkedAt"`
	Error     string    `json:"error"`
	Hint      string    `json:"hint"`
}

Status is the last known result for one check.

Jump to

Keyboard shortcuts

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