cron

import cron "ganigeorgiev.com/pocketbase/pocketbase/tools/cron"

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()

Source: tools/cron.

Types

Cron

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

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

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().

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.

Cron.HasStarted

func (c *Cron) HasStarted() bool

HasStarted checks whether the current Cron ticker has been started.

Cron.Jobs

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

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

Cron.MustAdd

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

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

Cron.Remove

func (c *Cron) Remove(jobId string)

Remove removes a single cron job by its id.

Cron.RemoveAll

func (c *Cron) RemoveAll()

RemoveAll removes all registered cron jobs.

Cron.SetInterval

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

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

Cron.SetTimezone

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

SetTimezone changes the current cron tick timezone.

Cron.Start

func (c *Cron) Start()

Start starts the cron ticker.

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

Cron.Stop

func (c *Cron) Stop()

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

You can resume the ticker by calling Start().

Cron.Total

func (c *Cron) Total() int

Total returns the current total number of registered cron jobs.

Job

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

Job defines a single registered cron job.

Job.Expression

func (j *Job) Expression() string

Expression returns the plain cron job schedule expression.

Job.Id

func (j *Job) Id() string

Id returns the cron job id.

Job.MarshalJSON

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

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

Job.Run

func (j *Job) Run()

Run runs the cron job function.

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.

NewMoment

func NewMoment(t time.Time) *Moment

NewMoment creates a new Moment from the specified time.

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.

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

Schedule.IsDue

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

IsDue checks whether the provided Moment satisfies the current Schedule.