schedule

package
v0.4.0 Latest Latest
Warning

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

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

Documentation

Overview

Package schedule provides cron-style task scheduling on robfig/cron with per-job panic recovery, optional overlap skipping, and graceful stop as an application runner.

The scheduler is single-instance in this version: running N replicas executes every job N times. Run it in one instance (or a dedicated binary) when deploying multiple replicas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JobOption

type JobOption func(*jobConfig)

JobOption customizes one scheduled job.

func SkipIfRunning

func SkipIfRunning() JobOption

SkipIfRunning skips a tick while the previous run of the same job is still executing. Overlapping runs are allowed by default.

type Options

type Options struct {
	Logger   *slog.Logger   // defaults to slog.Default()
	Location *time.Location // defaults to time.Local
	// StopTimeout bounds the wait for running jobs on stop, defaulting to 10s.
	StopTimeout time.Duration
}

Options defines an implementation type used by this package.

type Scheduler

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

Scheduler runs named jobs on cron schedules.

func New

func New(options Options) *Scheduler

New performs this package operation.

func (*Scheduler) Cron

func (s *Scheduler) Cron(spec, name string, job func(context.Context) error, opts ...JobOption) error

Cron schedules job with a five-field cron spec such as "*/5 * * * *". The spec is validated immediately. A failing or panicking job is logged and never stops the scheduler or the application.

func (*Scheduler) Daily

func (s *Scheduler) Daily(name string, job func(context.Context) error, opts ...JobOption) error

Daily schedules job at midnight in the scheduler's location.

func (*Scheduler) Every

func (s *Scheduler) Every(interval time.Duration, name string, job func(context.Context) error, opts ...JobOption) error

Every schedules job at a fixed interval of at least one second.

func (*Scheduler) Hourly

func (s *Scheduler) Hourly(name string, job func(context.Context) error, opts ...JobOption) error

Hourly schedules job at the top of every hour.

func (*Scheduler) Run

func (s *Scheduler) Run(ctx context.Context) error

Run starts the scheduler and blocks until ctx is canceled, then stops scheduling and waits up to StopTimeout for running jobs. Intended for app.Go("scheduler", scheduler.Run).

Jump to

Keyboard shortcuts

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