cleanup

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.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 cleanup carries out the removal of state that has passed its retention period, and records each removal as a cleanup operation that can be polled.

The same runner serves both drivers, the HTTP endpoints that start a cleanup on request, and the scheduler that starts one on an interval. A category cleaned by one is indistinguishable from the same category cleaned by the other.

Index

Constants

View Source
const DefaultTimeout = 10 * time.Minute

DefaultTimeout bounds a single cleanup run.

Ten minutes is generous enough for a deploy engine with a lot of use, and the work happens outside the request that asked for it, so the bound is on the work rather than on anyone waiting.

Variables

View Source
var ErrCleanupInProgress = errors.New("a cleanup for this category is already in progress")

ErrCleanupInProgress reports that the category is already being cleaned up, so a second run was not started.

Two runs over one category are not merely redundant as they both walk the same records, and a backend that removes them one at a time has the second run deleting what the first already took.

Within one engine, where the scheduler and the cleanup endpoints share a Runner, this is exact. Across engines sharing a state backend it is best effort, the only evidence another engine is mid-run is the operation it recorded, and reading that is not atomic with recording one here.

View Source
var ErrShuttingDown = errors.New("the engine is shutting down, so no cleanup was started")

ErrShuttingDown reports that the engine is shutting down, so no cleanup was started.

Shutdown waits for the runs already under way, which it can only do if no further run begins while it is waiting. Refusing is also the honest answer to the caller, since a run accepted here would be cut short moments later.

Functions

This section is empty.

Types

type Runner

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

Runner starts cleanup runs and keeps their operation records up to date.

func NewRunner

func NewRunner(
	operations manage.CleanupOperations,
	idGenerator core.IDGenerator,
	clock commoncore.Clock,
	logger core.Logger,
	opts ...RunnerOption,
) *Runner

NewRunner creates a runner that records its operations through the given store.

func (*Runner) CompletedWithin

func (r *Runner) CompletedWithin(
	ctx context.Context,
	cleanupType manage.CleanupType,
	window time.Duration,
) (bool, error)

CompletedWithin reports whether the category's most recent run finished successfully less than window ago. Used to leave alone a category another engine has just cleaned.

This only accounts for runs that completed. A run that failed, or that was cut short when its engine shut down, says nothing about whether the category still needs cleaning, so it must not hold off the next attempt for a whole window.

func (*Runner) LatestRun

func (r *Runner) LatestRun(
	ctx context.Context,
	cleanupType manage.CleanupType,
) (*manage.CleanupOperation, bool, error)

LatestRun returns the most recent cleanup operation recorded for the category, and whether there is one at all.

A category nobody has ever cleaned up is not an error, so it comes back as absent rather than as the store's not-found error.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, task Task) (*manage.CleanupOperation, error)

Run records a cleanup operation and carries the task out before returning, reporting the operation as it ended.

The task is bounded by both the runner's timeout and the given context, so a caller that is shutting down can cut a run short.

func (*Runner) Shutdown

func (r *Runner) Shutdown(ctx context.Context)

Shutdown cuts short the cleanups Start left running and waits for every claimed run to finish, or for ctx to be done, whichever comes first.

A cleanup started by a request outlives that request, so without this the state service could close while a run was still deleting through it. A run cut short records itself as failed, which leaves the category for the next pass to pick up rather than reporting it as cleaned.

func (*Runner) Start

func (r *Runner) Start(ctx context.Context, task Task) (*manage.CleanupOperation, error)

Start records a cleanup operation and carries the task out in the background, returning the operation as recorded.

The returned operation is a copy taken before the work begins, so a caller can serialise it while the run is still updating its own copy.

The work is given a context of its own rather than the caller's, a cleanup started by a request outlives the request that asked for it. That context belongs to the runner, so Shutdown can cut the run short and wait for it.

type RunnerOption

type RunnerOption func(*Runner)

RunnerOption configures a Runner.

func WithTimeout

func WithTimeout(timeout time.Duration) RunnerOption

WithTimeout sets how long a single cleanup run is given, in place of DefaultTimeout.

type Scheduler

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

Scheduler carries out the configured cleanup tasks on an interval.

This is what makes the retention periods take effect; core.ScheduledCleanupConfig carries why it is off by default.

func NewScheduler

func NewScheduler(
	runner *Runner,
	tasks []Task,
	interval time.Duration,
	clock commoncore.Clock,
	logger core.Logger,
) *Scheduler

NewScheduler creates a scheduler that runs the given tasks every interval.

func (*Scheduler) Start

func (s *Scheduler) Start() func()

Start begins running the tasks and returns the function that stops them.

The returned function waits for an in-flight pass to finish, so that shutdown does not leave a cleanup half-done with its operation record still reading "running".

type Task

type Task struct {
	// Type names the category, and is what a cleanup operation is recorded under.
	Type manage.CleanupType
	// RetentionPeriod is how far back the category is kept. Records older than this
	// are removed.
	RetentionPeriod time.Duration
	// Remove deletes the records older than thresholdDate, returning how many it
	// removed. This is the category's own store method.
	Remove func(ctx context.Context, thresholdDate time.Time) (int64, error)
	// LogDescription names the category in log messages, as "blueprint validations".
	LogDescription string
	// LogName is the name the run's logger is given, as "validationCleanup".
	LogName string
}

Task describes one category of state to clean up.

func AuditRecordsTask

func AuditRecordsTask(store manage.Audit, retentionPeriod time.Duration) Task

AuditRecordsTask cleans up audit records older than the retention period.

func ChangesetsTask

func ChangesetsTask(store manage.Changesets, retentionPeriod time.Duration) Task

ChangesetsTask cleans up change sets older than the retention period.

func EventsTask

func EventsTask(store manage.Events, retentionPeriod time.Duration) Task

EventsTask cleans up events older than the retention period.

func ReconciliationResultsTask

func ReconciliationResultsTask(
	store manage.ReconciliationResults,
	retentionPeriod time.Duration,
) Task

ReconciliationResultsTask cleans up reconciliation results older than the retention period.

func StateHousekeepingTask

func StateHousekeepingTask(housekeeping manage.Housekeeping) Task

StateHousekeepingTask carries out the upkeep the state backend needs on its own storage.

What that involves, and how old anything has to be before it is touched, is the backend's to decide, so the task has no retention period. The threshold the runner records for it is the time the run started.

func ValidationsTask

func ValidationsTask(store manage.Validation, retentionPeriod time.Duration) Task

ValidationsTask cleans up blueprint validations older than the retention period.

Jump to

Keyboard shortcuts

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