runner

package
v0.10.0 Latest Latest
Warning

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

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

Documentation

Overview

Package runner provides background task runner and lifecycle management.

Index

Constants

View Source
const DefaultStopTimeout = 5 * time.Second

DefaultStopTimeout bounds how long Start waits for running tasks after its context is cancelled when NewRunner gets no positive timeout.

Variables

This section is empty.

Functions

This section is empty.

Types

type Runner

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

Runner manages and executes scheduled background tasks.

func NewRunner

func NewRunner(registry *TaskRegistry, stopTimeout time.Duration) *Runner

NewRunner creates a new task runner. stopTimeout bounds how long Start waits for running tasks to return once they have been cancelled. Log lines go through the process-wide slog logger, so they follow LOG_FORMAT, LOG_LEVEL and LOG_OUTPUT.

func (*Runner) Start

func (r *Runner) Start(ctx context.Context) error

Start schedules every registered task and blocks until ctx is cancelled (the caller cancels it on SIGTERM/SIGINT). Cancellation stops new runs, cancels the context of every running task and waits up to the stop timeout for them to return. It returns nil after a clean stop and an error when tasks were still running at the deadline.

type Task

type Task interface {
	// Name returns the unique name of the task
	Name() string

	// Schedule returns the cron schedule expression for this task
	Schedule() string

	// Run executes the task
	Run(ctx context.Context) error

	// Timeout returns the maximum time this task should run
	Timeout() time.Duration
}

Task represents a background task that can be scheduled.

type TaskRegistry

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

TaskRegistry holds all registered tasks.

func NewTaskRegistry

func NewTaskRegistry() *TaskRegistry

NewTaskRegistry creates a new task registry.

func (*TaskRegistry) All

func (r *TaskRegistry) All() map[string]Task

All returns all registered tasks.

func (*TaskRegistry) Get

func (r *TaskRegistry) Get(name string) (Task, bool)

Get returns a task by name.

func (*TaskRegistry) Register

func (r *TaskRegistry) Register(task Task)

Register adds a task to the registry.

Jump to

Keyboard shortcuts

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