scheduler

package
v1.4.9 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package scheduler is the generic, in-process recurring-evaluation seam on the agent runtime. It runs registered jobs at a fixed interval inside the worker lifecycle, so a consumer can re-pull signals and derive standing state on a schedule (e.g. learn metric baselines, evaluate SLO burn) without adding any new mutation path.

It is deliberately unopinionated and tier-neutral: the scheduler owns timing and lifecycle only; the content of every job belongs to the consumer. Registration mirrors the middleware.SetOrgResolver / SetAuthMiddleware seam — a consumer (the enterprise hooks.Register) registers jobs once at boot, before the worker starts. OSS registers none, so the scheduler is a no-op and community behaviour is byte-for- byte unchanged (no goroutines, no timers).

READ-ONLY CONTRACT (enforced by review + the import-graph guard in guard_test.go): a Job MUST be read-only / analyze-kind. It may pull signals and derive state, but it MUST NOT mutate cluster state and MUST NOT bypass the one emission path (services.CreateIncident) — findings it raises flow through that single path exactly like on-demand analyze. This package itself imports no write/emit/storage path, so a registered job cannot acquire write capability through the scheduler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(j Job) error

Register adds a job to the process-wide registry. Call once at boot, before the worker starts. Returns an error for an invalid job or a duplicate name. This is the setter the enterprise hooks.Register attaches to (mirror of middleware.SetOrgResolver).

func Reset

func Reset()

Reset clears the registry. Test-only helper to isolate cases; not used in production.

func SetOwnership

func SetOwnership(pred func(name string) bool)

SetOwnership installs the process-wide job-ownership predicate. A job runs on this instance only when pred(job.Name) is true. Passing nil clears the slot (back to own-everything). Last-wins: a second call replaces the first. Call at boot, before the worker starts. This is the entry point the enterprise cluster.Identity attaches to (mirror of middleware.SetOrgResolver); community OSS installs none.

Types

type Job

type Job struct {
	// Name uniquely identifies the job (used in logs and dedup). Required.
	Name string
	// Interval is the period between runs. Required; must be > 0.
	Interval time.Duration
	// Run executes one evaluation. It MUST honour ctx cancellation and MUST
	// be read-only / analyze-kind (see the package contract). Required.
	Run func(ctx context.Context) error
}

Job is one registered recurring evaluation.

func Registered

func Registered() []Job

Registered returns a copy of the registered jobs.

type Scheduler

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

Scheduler runs a fixed set of jobs until its context is canceled. Construct it with New (explicit jobs) or NewFromRegistry (the process-wide registry snapshot).

func New

func New(jobs []Job) *Scheduler

New returns a Scheduler over the given jobs with the default startup jitter.

func NewFromRegistry

func NewFromRegistry() *Scheduler

NewFromRegistry returns a Scheduler over a snapshot of the process-wide registry. The worker calls this; in community mode the snapshot is empty and Run is a no-op.

func (*Scheduler) Len

func (s *Scheduler) Len() int

Len reports how many jobs the scheduler holds in its snapshot. Run starts a goroutine and timer only for the subset this instance owns under the installed ownership predicate; with no predicate (community / single-instance) the owned set is every job, so Run behaves exactly as before.

func (*Scheduler) Run

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

Run drives every owned job until ctx is canceled, one goroutine per job, and returns once all jobs have stopped. With no owned jobs it returns immediately and starts nothing — the OSS runtime is unaffected, and an un-owned job (one the ownership predicate rejects) starts no goroutine and no timer.

func (*Scheduler) SetJitter

func (s *Scheduler) SetJitter(d time.Duration) *Scheduler

SetJitter overrides the startup jitter (use 0 for deterministic tests). Returns the receiver for chaining.

Jump to

Keyboard shortcuts

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