scheduler

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 16 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 scheduler runs the checks this node is responsible for (FA-SCH-*).

Index

Constants

View Source
const DefaultMaxConcurrent = 256

DefaultMaxConcurrent is the default limit of simultaneous checks (FA-SCH-07).

Variables

This section is empty.

Functions

func ActiveMonitors

func ActiveMonitors(monitors []store.Monitor, tenants []store.Tenant) []store.Monitor

ActiveMonitors returns the monitors whose tenant is listed in tenants and not suspended, in their original order. The system tenant always counts as active.

func ActiveTenantIDs

func ActiveTenantIDs(tenants []store.Tenant) map[string]bool

ActiveTenantIDs returns the set of tenants that are listed and not suspended.

func Capabilities

func Capabilities(r *checker.Registry) func(monitorType string) string

Capabilities returns the capability function of an Assigner for the checkers of r: the capability a checker requires (checker.CapabilityRequirer), or "" if it requires none or the type is unknown.

func NextRun

func NextRun(now time.Time, interval, phase time.Duration) time.Time

NextRun returns the first time strictly after now that lies on the grid phase + k*interval (Unix epoch based).

func Phase

func Phase(monitorID string, interval time.Duration) time.Duration

Phase returns the deterministic offset of a monitor within its interval.

func Reconcile

func Reconcile(ctx context.Context, state store.State, changes <-chan struct{}, interval time.Duration, log *slog.Logger, targets ...Syncer)

Reconcile keeps the targets (the scheduler and everything that tracks monitors) in line with state.db: it loads all monitors when changes signals and at least every interval, so a configuration change is active within seconds (FA-SCH-06). A target that runs checks is wrapped in an AssignmentSyncer, so it gets the monitors of its node only (ADR 0017).

Types

type Assigner

type Assigner struct {
	// Capability returns the capability a monitor type requires, "" if none.
	Capability func(monitorType string) string
}

Assigner decides which nodes run a monitor (ADR 0017, FA-SCH-01/02). It is pure: the result depends only on the monitor, the registry and the capability mapping, never on registry order.

func (Assigner) Nodes

func (a Assigner) Nodes(m store.Monitor, registry []store.Node) []string

Nodes returns the IDs of the nodes that run m, sorted by ID, given the registry. With an empty registry no node IDs are known, so it returns nil; Runs treats that case as "every node runs every monitor".

func (Assigner) Runs

func (a Assigner) Runs(m store.Monitor, registry []store.Node, nodeID string) bool

Runs reports whether nodeID runs m. With an empty registry every node runs every monitor (single voter before registration).

type AssignmentSyncer

type AssignmentSyncer struct {
	Target   Syncer
	NodeID   string
	Assigner Assigner
	Registry func() ([]store.Node, error)
	Log      *slog.Logger
}

AssignmentSyncer hands Target only the monitors that node NodeID runs (ADR 0017). Registry reads the node registry on every Sync; if that fails, Target keeps the previous monitors.

func (AssignmentSyncer) Sync

func (s AssignmentSyncer) Sync(monitors []store.Monitor)

Sync implements Syncer.

type CheckObserver

type CheckObserver interface {
	CheckStarted(typ string)
	CheckFinished(typ, result string, d time.Duration)
}

CheckObserver is told when a check starts and when it finishes. It is optional; metrics implement it. Calls come from the check goroutines and must not block for long. The result is the lower-case status (up, down, degraded).

type HeartbeatWriter

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

HeartbeatWriter turns outcomes into heartbeats of this node's stream and stores them in batches. seq continues after a restart and is never reused within an incarnation (FA-REP-01).

func NewHeartbeatWriter

func NewHeartbeatWriter(ctx context.Context, metrics store.Metrics, nodeID string, log *slog.Logger) (*HeartbeatWriter, error)

NewHeartbeatWriter continues the stream (nodeID, metrics.Incarnation()).

func (*HeartbeatWriter) Record

func (w *HeartbeatWriter) Record(ctx context.Context, o Outcome)

Record implements Sink. It blocks only if the queue is full, which applies backpressure to the checks instead of losing heartbeats.

func (*HeartbeatWriter) Run

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

Run writes queued heartbeats until ctx ends, then flushes what is left.

func (*HeartbeatWriter) SetOnStored

func (w *HeartbeatWriter) SetOnStored(fn func([]store.Heartbeat))

SetOnStored registers fn to receive every batch after it was stored. fn must not block, because the writer runs on; ADR 0021. The slice is only valid during the call, so fn must copy what it keeps. Several functions may be registered; they are called in registration order. Register before Run starts.

type Options

type Options struct {
	Registry      *checker.Registry
	Env           *checker.Env
	Sink          Sink
	Logger        *slog.Logger
	MaxConcurrent int
	// Observer receives check start and finish events; nil disables them.
	Observer CheckObserver
	// Unit is the length of one interval second; tests shrink it.
	Unit time.Duration
	// Resolve returns the configuration with its secrets decrypted (ADR 0015);
	// nil uses the stored configuration as it is.
	Resolve func(ctx context.Context, m store.Monitor) (json.RawMessage, error)
	// Now is the wall clock for the schedule and the start time of a check
	// (heartbeat timestamps); nil means time.Now. Durations stay monotonic (ADR 0020).
	Now func() time.Time
}

Options configure a Scheduler.

type Outcome

type Outcome struct {
	Monitor store.Monitor
	// Scheduled is the planned start; Started may be later if the
	// concurrency limit delayed the check.
	Scheduled time.Time
	Started   time.Time
	Result    checker.Result
}

Outcome is one finished check.

type Scheduler

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

Scheduler runs one timer loop per monitor.

func New

func New(opts Options) *Scheduler

New creates a scheduler; call Run to start it.

func (*Scheduler) Len

func (s *Scheduler) Len() int

Len returns the number of scheduled monitors.

func (*Scheduler) Run

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

Run starts the scheduler and blocks until ctx ends. It then stops all timers and waits for running checks, whose contexts are cancelled.

func (*Scheduler) Stats

func (s *Scheduler) Stats() Stats

Stats returns the current counters.

func (*Scheduler) Sync

func (s *Scheduler) Sync(monitors []store.Monitor)

Sync sets the monitors to run. New monitors start, removed or paused ones stop, and monitors whose revision changed restart with the new configuration (FA-SCH-06).

type Sink

type Sink interface {
	Record(ctx context.Context, o Outcome)
}

Sink receives outcomes. Record must not block for long; it is called from the check goroutines.

type Sinks

type Sinks []Sink

Sinks passes every outcome to each sink in order.

func (Sinks) Record

func (s Sinks) Record(ctx context.Context, o Outcome)

Record implements Sink.

type Stats

type Stats struct {
	Started int64
	// Skipped counts runs dropped because the previous run of the same
	// monitor was still in progress (FA-SCH-04).
	Skipped int64
	Running int64
}

Stats are counters for metrics and tests.

type Syncer

type Syncer interface {
	Sync(monitors []store.Monitor)
}

Syncer receives the current set of monitors.

type TenantGate

type TenantGate struct {
	Target  Syncer
	Tenants func() ([]store.Tenant, error)
	Log     *slog.Logger
}

TenantGate hands Target only the monitors of tenants that may run: a suspended tenant rests its checks and alerts, and the monitors of a tenant that no longer exists are gone (ADR 0029). The system tenant is never suspended. Tenants returns the tenant list; if that fails, Target keeps the previous monitors.

func (TenantGate) Sync

func (g TenantGate) Sync(monitors []store.Monitor)

Sync implements Syncer.

Jump to

Keyboard shortcuts

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