scheduler

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package scheduler wraps robfig/cron with a fluent API for defining scheduled tasks, named jobs, and graceful shutdown.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

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

Job is a named scheduled task.

func (*Job) WithTimeout

func (j *Job) WithTimeout(d time.Duration) *Job

WithTimeout sets the per-run timeout for this job.

type Scheduler

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

Scheduler manages cron-based scheduled tasks.

func New

func New(logger ...*slog.Logger) *Scheduler

New creates a Scheduler. Start must be called to begin processing.

func (*Scheduler) Daily

func (s *Scheduler) Daily(at, name string, fn func(ctx context.Context) error) (*Job, error)

Daily schedules fn to run at a specific time each day (HH:MM, 24h).

s.Daily("03:00", "nightly-backup", func(ctx context.Context) error { ... })

func (*Scheduler) Every

func (s *Scheduler) Every(expr, name string, fn func(ctx context.Context) error) *Job

Every schedules fn using a cron expression (supports seconds: "*/5 * * * * *"). It may be called before or after Start: jobs added after Start are registered with the running cron engine immediately.

s.Every("0 * * * * *", "hourly-cleanup", func(ctx context.Context) error { ... })

func (*Scheduler) Hourly

func (s *Scheduler) Hourly(name string, fn func(ctx context.Context) error) *Job

Hourly schedules fn at the top of every hour.

func (*Scheduler) Start

func (s *Scheduler) Start() error

Start registers all jobs and starts the cron engine. Calling Start more than once is a no-op.

func (*Scheduler) Stop

func (s *Scheduler) Stop() context.Context

Stop halts scheduling of new runs and waits — up to the stop timeout (default 30s, see WithStopTimeout) — for in-flight jobs to finish before cancelling their contexts. The returned context completes once all running jobs have returned.

func (*Scheduler) WithStopTimeout

func (s *Scheduler) WithStopTimeout(d time.Duration) *Scheduler

WithStopTimeout sets how long Stop waits for in-flight jobs to finish before cancelling their contexts (default 30s).

Jump to

Keyboard shortcuts

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