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
- func RescueCompletion(row *job.Row, now time.Time, retry func(*job.Row, time.Time) time.Time) job.Completion
- func RunAll(ctx context.Context, services ...Service)
- type Base
- type CancelPoller
- type Cleaner
- type DepthSampler
- type Periodic
- type PeriodicJob
- type Reindexer
- type Rescuer
- type Scheduler
- type Service
Constants ¶
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.
const DefaultTickRetention = 7 * 24 * time.Hour
DefaultTickRetention is how long periodic tick records are kept.
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 ¶
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.
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.
type DepthSampler ¶
type DepthSampler struct {
Base
Interval time.Duration
OnDepths func([]job.QueueDepth)
}
DepthSampler samples queue depth per state for metrics.
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.
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 ¶
Reindexer rebuilds the queue indexes concurrently at Interval; a zero Interval disables it.
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.