scheduler

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package scheduler provides stable background job scheduling primitives.

Runner executes jobs immediately on start and then on their configured intervals. Panics inside scheduled jobs are recovered, logged, and recorded as failed runs so later intervals can continue. At most one schedule is active per job name, even if the same runner is started more than once or duplicate named jobs are configured, and same-name jobs never overlap with themselves while a prior run is still in flight.

Recorder persistence failures are treated as observability failures rather than job failures. The runner should surface them through logging and any configured callback hook, but it should not rerun the job immediately or stop future intervals after the job function itself has already completed. Final run persistence uses a short-lived cleanup context so graceful shutdown can still record outcomes after the job function returns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	Name     string
	Interval time.Duration
	Run      func(ctx context.Context) error
}

Job represents a periodic task.

type LastRunProvider

type LastRunProvider interface {
	LastFinished(ctx context.Context, jobName string) (time.Time, bool, error)
}

LastRunProvider exposes last finished run for a job.

type Logger

type Logger interface {
	Info(msg string, args ...any)
	Error(msg string, args ...any)
}

Logger is a minimal interface for logging.

type Recorder

type Recorder interface {
	Record(ctx context.Context, jobName string, startedAt, finishedAt time.Time, success bool, errMsg string) error
}

Recorder persists job runs.

Persistence errors are observability failures. Runner implementations should surface them, but a failed write must not retroactively change the outcome of a job function that already ran to completion.

type RecorderFailure added in v2.1.0

type RecorderFailure struct {
	JobName    string
	StartedAt  time.Time
	FinishedAt time.Time
	Success    bool
	ErrMsg     string
	Err        error
}

RecorderFailure captures the context for a failed run-record write.

type RecorderFailureHandler added in v2.1.0

type RecorderFailureHandler interface {
	OnRecorderFailure(ctx context.Context, failure RecorderFailure)
}

RecorderFailureHandler receives recorder write failures.

type RecorderFailureHandlerFunc added in v2.1.0

type RecorderFailureHandlerFunc func(context.Context, RecorderFailure)

RecorderFailureHandlerFunc turns a function into a RecorderFailureHandler.

func (RecorderFailureHandlerFunc) OnRecorderFailure added in v2.1.0

func (f RecorderFailureHandlerFunc) OnRecorderFailure(
	ctx context.Context, failure RecorderFailure,
)

OnRecorderFailure calls the underlying function.

type RecorderFunc

type RecorderFunc func(ctx context.Context, jobName string, startedAt, finishedAt time.Time, success bool, errMsg string) error

RecorderFunc turns a function into a Recorder.

func (RecorderFunc) Record

func (f RecorderFunc) Record(ctx context.Context, jobName string, startedAt, finishedAt time.Time, success bool, errMsg string) error

Record calls the underlying function.

type Runner

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

Runner executes jobs on a ticker until ctx is cancelled.

func New

func New(log Logger, rec Recorder, last LastRunProvider, jobs ...Job) *Runner

New creates a Runner.

func (*Runner) SetRecorderFailureHandler added in v2.1.0

func (r *Runner) SetRecorderFailureHandler(handler RecorderFailureHandler)

SetRecorderFailureHandler configures a callback for recorder persistence failures. Set it before calling Start.

func (*Runner) Start

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

Start launches all jobs in separate goroutines.

Directories

Path Synopsis
Package migrations provides stable scheduler migration helpers.
Package migrations provides stable scheduler migration helpers.

Jump to

Keyboard shortcuts

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