janitor

package
v0.37.0 Latest Latest
Warning

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

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

Documentation

Overview

Package janitor provides a cron-based background task runner with per-task schedules, timeouts, and pre/post hooks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Janitor

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

Janitor runs Tasks on individual cron schedules.

func New

func New(opts ...Option) *Janitor

New creates a Janitor. Options configure timeout, logger, and hooks.

func (*Janitor) AddTask

func (j *Janitor) AddTask(schedule string, task Task) *Janitor

AddTask registers a task with a cron schedule expression. Supported formats include standard cron ("0 0 * * *") and robfig/cron descriptors ("@every 5m", "@daily", "@hourly").

func (*Janitor) Start

func (j *Janitor) Start(ctx context.Context) error

Start validates configuration, registers all tasks with the cron scheduler, and starts it. The provided context controls the lifetime — cancelling it stops the scheduler.

func (*Janitor) Stop

func (j *Janitor) Stop()

Stop stops the cron scheduler and waits for in-flight tasks to return, so a caller can safely tear down shared resources (DB connections, etc.) once Stop returns. It is safe to call multiple times. Tasks observe the cancelled context, so well-behaved tasks return promptly.

type Option

type Option func(*Janitor)

Option configures a Janitor.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the structured logger used for task execution logging. A nil logger is resolved to slog.Default() at Start time.

func WithPostRun

func WithPostRun(fn PostRunFunc) Option

WithPostRun appends a per-task post-run hook. Multiple hooks run in order.

func WithPostTick

func WithPostTick(fn PostTickFunc) Option

WithPostTick appends a post-execution hook. Multiple hooks run in order.

func WithPreRun

func WithPreRun(fn PreRunFunc) Option

WithPreRun appends a per-task pre-run hook. Multiple hooks run in order; the first error skips the task.

func WithPreTick

func WithPreTick(fn PreTickFunc) Option

WithPreTick appends a pre-execution hook. Multiple hooks run in order; the first error skips the execution.

func WithRunImmediately

func WithRunImmediately() Option

WithRunImmediately causes all registered tasks to execute once as soon as Start is called, in addition to their regular cron schedules.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the per-task context timeout (default 30s).

type PostRunFunc

type PostRunFunc func(ctx context.Context, taskName string, affected int64, taskErr error)

PostRunFunc is called after each task with its results.

type PostTickFunc

type PostTickFunc func(ctx context.Context)

PostTickFunc is called after a scheduled task execution completes.

type PreRunFunc

type PreRunFunc func(ctx context.Context, taskName string) error

PreRunFunc is called before each task. Returning an error skips that task.

type PreTickFunc

type PreTickFunc func(ctx context.Context) error

PreTickFunc is called before a scheduled task execution. Returning an error skips that execution.

type Task

type Task interface {
	// Name returns a human-readable identifier used for logging and hooks.
	Name() string
	// Run performs the work and returns the number of affected rows/items and
	// any error. The context carries the per-task timeout.
	Run(ctx context.Context) (int64, error)
}

Task is a single unit of maintenance work executed by the Janitor.

Jump to

Keyboard shortcuts

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