Help us shape the future of Go! Take the 2026 Go Developer Survey

cron

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cron implements a crontab-like service to execute and schedule repeative tasks/jobs.

Example:

c := cron.New()
c.MustAdd("dailyReport", "0 0 * * *", func() { ... })
c.Start()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClaimFunc

type ClaimFunc func(jobId string, slot time.Time) bool

ClaimFunc is called before running a scheduled job.

slot is the scheduled tick time (truncated to the cron interval) and it is the same for all instances with the same schedule.

The job execution is skipped if it returns false (eg. because the job was already executed for the same slot by another app instance).

popgres: used to run each scheduled job by a single app instance (node)

type Cron

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

Cron is a crontab-like struct for tasks/jobs scheduling.

popgres: the claim field (see ClaimFunc)

func New

func New() *Cron

New create a new Cron struct with default tick interval of 1 minute and timezone in UTC.

You can change the default tick interval with Cron.SetInterval(). You can change the default timezone with Cron.SetTimezone().

func (*Cron) Add

func (c *Cron) Add(jobId string, cronExpr string, fn func()) error

Add registers a single cron job.

If there is already a job with the provided id, then the old job will be replaced with the new one.

cronExpr is a regular cron expression, eg. "0 */3 * * *" (aka. at minute 0 past every 3rd hour). Check cron.NewSchedule() for the supported tokens.

func (*Cron) HasStarted

func (c *Cron) HasStarted() bool

HasStarted checks whether the current Cron ticker has been started.

func (*Cron) Jobs

func (c *Cron) Jobs() []*Job

Jobs returns a shallow copy of the currently registered cron jobs.

func (*Cron) MustAdd

func (c *Cron) MustAdd(jobId string, cronExpr string, run func())

MustAdd is similar to Add() but panic on failure.

func (*Cron) Remove

func (c *Cron) Remove(jobId string)

Remove removes a single cron job by its id.

func (*Cron) RemoveAll

func (c *Cron) RemoveAll()

RemoveAll removes all registered cron jobs.

func (*Cron) SetClaimFunc

func (c *Cron) SetClaimFunc(fn ClaimFunc)

SetClaimFunc registers an optional function that is called before running each scheduled job (see ClaimFunc).

It is not called for the jobs that are executed manually with Job.Run.

func (*Cron) SetInterval

func (c *Cron) SetInterval(d time.Duration)

SetInterval changes the current cron tick interval (it usually should be >= 1 minute).

func (*Cron) SetTimezone

func (c *Cron) SetTimezone(l *time.Location)

SetTimezone changes the current cron tick timezone.

func (*Cron) Start

func (c *Cron) Start()

Start starts the cron ticker.

Calling Start() on already started cron will restart the ticker.

func (*Cron) Stop

func (c *Cron) Stop()

Stop stops the current cron ticker (if not already).

You can resume the ticker by calling Start().

func (*Cron) Total

func (c *Cron) Total() int

Total returns the current total number of registered cron jobs.

type Job

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

Job defines a single registered cron job.

func (*Job) Expression

func (j *Job) Expression() string

Expression returns the plain cron job schedule expression.

func (*Job) Id

func (j *Job) Id() string

Id returns the cron job id.

func (Job) MarshalJSON

func (j Job) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler and export the current jobs data into valid JSON.

func (*Job) Run

func (j *Job) Run()

Run runs the cron job function.

type Moment

type Moment struct {
	Minute    int `json:"minute"`
	Hour      int `json:"hour"`
	Day       int `json:"day"`
	Month     int `json:"month"`
	DayOfWeek int `json:"dayOfWeek"`
}

Moment represents a parsed single time moment.

func NewMoment

func NewMoment(t time.Time) *Moment

NewMoment creates a new Moment from the specified time.

type Schedule

type Schedule struct {
	Minutes    map[int]struct{} `json:"minutes"`
	Hours      map[int]struct{} `json:"hours"`
	Days       map[int]struct{} `json:"days"`
	Months     map[int]struct{} `json:"months"`
	DaysOfWeek map[int]struct{} `json:"daysOfWeek"`
	// contains filtered or unexported fields
}

Schedule stores parsed information for each time component when a cron job should run.

func NewSchedule

func NewSchedule(cronExpr string) (*Schedule, error)

NewSchedule creates a new Schedule from a cron expression.

A cron expression could be a macro OR 5 segments separated by space, representing: minute, hour, day of the month, month and day of the week.

The following segment formats are supported:

  • wildcard: *
  • range: 1-30
  • step: */n or 1-30/n
  • list: 1,2,3,10-20/n

The following macros are supported:

  • @yearly (or @annually)
  • @monthly
  • @weekly
  • @daily (or @midnight)
  • @hourly

func (*Schedule) IsDue

func (s *Schedule) IsDue(m *Moment) bool

IsDue checks whether the provided Moment satisfies the current Schedule.

Jump to

Keyboard shortcuts

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