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 (*Janitor) AddTask ¶
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 ¶
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 ¶
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 ¶
WithTimeout sets the per-task context timeout (default 30s).
type PostRunFunc ¶
PostRunFunc is called after each task with its results.
type PostTickFunc ¶
PostTickFunc is called after a scheduled task execution completes.
type PreRunFunc ¶
PreRunFunc is called before each task. Returning an error skips that task.
type PreTickFunc ¶
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.