maintenance

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package maintenance holds the leader-only services (scheduler, rescuer, cleaner, reindexer, periodic enqueuer, queue-depth sampler) and the PollOnly cancel poller. Each service is a loop driven by the injected clock that records its last run and rows affected.

maintenance.RunAll(leaderCtx, &maintenance.Scheduler{Base: base}, &maintenance.Rescuer{Base: base, ...})

Index

Constants

View Source
const (
	DefaultSchedulerInterval = time.Second
	DefaultCleanerInterval   = 30 * time.Second
	DefaultDepthInterval     = 15 * time.Second
	DefaultPeriodicInterval  = time.Second
	DefaultBatch             = 1000

	// RunTimeout bounds one run of a service, so a connection that died
	// silently cannot stall it.
	RunTimeout = 2 * time.Minute
	// ReindexTimeout bounds one reindex run (REINDEX CONCURRENTLY of every
	// queue index).
	ReindexTimeout = time.Hour
)

Defaults.

View Source
const DefaultTickRetention = 7 * 24 * time.Hour

DefaultTickRetention is how long periodic tick records are kept.

View Source
const PeriodicGrace = 15 * time.Second

PeriodicGrace is added to every catch-up window: a tick enqueued this late during normal operation is not considered missed.

Variables

This section is empty.

Functions

func RescueCompletion

func RescueCompletion(row *job.Row, now time.Time, retry func(*job.Row, time.Time) time.Time) job.Completion

RescueCompletion maps a stuck running job to retryable, or discarded when its attempts are exhausted, recording a rescue error.

func RunAll

func RunAll(ctx context.Context, services ...Service)

RunAll runs services concurrently and returns when all have stopped.

Types

type Base

type Base struct {
	Store  job.MaintenanceStore
	Clock  clock.Clock
	Logger *slog.Logger
	// Observe records the last run time and rows affected of a service.
	Observe func(service string, rows int, at time.Time)
}

Base holds dependencies shared by every service.

type CancelPoller

type CancelPoller struct {
	Store    job.CancelPoller
	Clock    clock.Clock
	Logger   *slog.Logger
	Interval time.Duration
	Running  func() []int64
	OnCancel func(id int64)
}

CancelPoller finds cancel requests for this client's running jobs by polling; it runs on every client in PollOnly mode, where NOTIFY is unavailable.

func (*CancelPoller) Poll

func (p *CancelPoller) Poll(ctx context.Context)

Poll checks the running jobs once.

func (*CancelPoller) Run

func (p *CancelPoller) Run(ctx context.Context) error

Run polls every Interval.

type Cleaner

type Cleaner struct {
	Base
	Interval time.Duration
	// Retention per final state; zero or negative keeps jobs of that state forever.
	Retention map[job.State]time.Duration
	// TickRetention keeps periodic tick records this long (default 7 days);
	// the latest tick of every periodic job is always kept.
	TickRetention time.Duration
	Batch         int
}

Cleaner deletes finalized jobs older than their retention in batches.

func (*Cleaner) Run

func (c *Cleaner) Run(ctx context.Context) error

Run deletes expired jobs every Interval (default 30s).

type DepthSampler

type DepthSampler struct {
	Base
	Interval time.Duration
	OnDepths func([]job.QueueDepth)
}

DepthSampler samples queue depth per state for metrics.

func (*DepthSampler) Run

func (d *DepthSampler) Run(ctx context.Context) error

Run samples every Interval (default 15s).

type Periodic

type Periodic struct {
	Base
	Jobs     []PeriodicJob
	Interval time.Duration
	// OnSkipped receives the number of ticks skipped beyond the catch-up window.
	OnSkipped func(name string, n int)
	// OnEnqueued is called for each tick this leader inserted.
	OnEnqueued func(name string, tick time.Time)
}

Periodic enqueues periodic jobs exactly once per tick: each tick is recorded in foreman_periodic_ticks under a unique constraint in the same transaction as the job, so overlapping leaders cannot enqueue it twice. After a leader gap, missed ticks within CatchUp are enqueued oldest first and older ones are skipped and counted.

func (*Periodic) Run

func (p *Periodic) Run(ctx context.Context) error

Run enqueues due ticks every Interval (default 1s) while leader.

type PeriodicJob

type PeriodicJob struct {
	Name       string
	Schedule   schedule.Schedule
	CatchUp    time.Duration
	RunOnStart bool
	// Build returns the job to insert for tick.
	Build func(tick time.Time) (job.InsertParams, error)
}

PeriodicJob is one registered periodic job.

type Reindexer

type Reindexer struct {
	Base
	Interval time.Duration
}

Reindexer rebuilds the queue indexes concurrently at Interval; a zero Interval disables it.

func (*Reindexer) Run

func (r *Reindexer) Run(ctx context.Context) error

Run reindexes every Interval, starting one Interval after election.

type Rescuer

type Rescuer struct {
	Base
	// Interval is the run period (Config.HeartbeatInterval).
	Interval time.Duration
	// StuckAfter is Config.RescueStuckJobsAfter.
	StuckAfter time.Duration
	// Retry computes the next run time of a rescued job.
	Retry func(row *job.Row, now time.Time) time.Time
	Batch int
	// OnRescued receives the number of jobs rescued per run.
	OnRescued func(n int)
}

Rescuer returns running jobs whose worker stopped heart-beating to retryable, or discards them when attempts are exhausted.

func (*Rescuer) Run

func (r *Rescuer) Run(ctx context.Context) error

Run rescues stuck jobs every Interval.

type Scheduler

type Scheduler struct {
	Base
	Interval time.Duration
	Batch    int
}

Scheduler moves scheduled and retryable jobs to available when due.

func (*Scheduler) Run

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

Run promotes due jobs every Interval (default 1s) in batches.

type Service

type Service interface {
	Run(ctx context.Context) error
}

Service is a long-running maintenance loop.

Jump to

Keyboard shortcuts

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