cron

package
v0.0.2-alpha Latest Latest
Warning

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

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

Documentation

Overview

Package cron schedules recurring jobs using cron expressions.

hex/cron wraps robfig/cron/v3 with a small facade that fits the hex provider lifecycle: consumers register jobs during Register/Boot, Start begins the ticker in the background, and Stop drains running jobs.

A job is a function that takes a context and returns an error. Errors are logged; they never crash the scheduler. Panics inside a job are recovered so one bad job does not take down the rest.

Example:

sched := cron.New()
if err := sched.Schedule("sync-releases", "*/5 * * * *", func(ctx context.Context) error {
    return svc.Sync(ctx)
}); err != nil {
    return err
}
sched.Start()
defer sched.Stop(ctx) // waits for in-flight jobs

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cron

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

Cron is a hex-owned cron scheduler.

func New

func New(opts ...Option) *Cron

New returns a Cron with the given options. Timezone defaults to time.Local; use robfig options via WithRobfigOption for advanced control.

func (*Cron) Names

func (c *Cron) Names() []string

Names returns the sorted list of registered job names.

func (*Cron) Remove

func (c *Cron) Remove(name string) bool

Remove unregisters the job with the given name. Returns true if a job was removed, false if no job with that name was registered.

func (*Cron) Schedule

func (c *Cron) Schedule(name, spec string, job Job) error

Schedule registers job under name to run on spec. Returns an error if the spec is invalid or a job with the same name is already registered.

func (*Cron) Start

func (c *Cron) Start()

Start begins running scheduled jobs. Safe to call multiple times; only the first call starts the underlying ticker.

func (*Cron) Stop

func (c *Cron) Stop(ctx context.Context) error

Stop signals the scheduler to stop accepting new job invocations and waits for in-flight jobs to finish or ctx to expire, whichever comes first. After Stop returns the scheduler cannot be restarted; construct a new Cron instead.

type Job

type Job func(context.Context) error

Job is a function executed on a cron schedule. The context is derived from the scheduler's root context so shutdown cancellation reaches all jobs.

type Option

type Option func(*Cron)

Option configures a new Cron.

func WithSeconds

func WithSeconds() Option

WithSeconds enables 6-field cron expressions (seconds precision). Without this, expressions are the standard 5-field minute-precision form.

type Scheduler

type Scheduler interface {
	Schedule(name, spec string, job Job) error
	Remove(name string) bool
	Names() []string
}

Scheduler is the minimum surface consumers rely on. Concrete *Cron satisfies it; tests can substitute a fake without importing robfig.

Directories

Path Synopsis
Package provider is the default hex/cron service provider.
Package provider is the default hex/cron service provider.

Jump to

Keyboard shortcuts

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