cron

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cron creates a ticker (similar to time.Ticker) from a cron schedule.

The Cron schedule can be in Unix or Quartz format. Directives like '@weekly' or '@daily' can also be parsed as defined in the package github.com/robfig/cron/v3.

You may add the TimeZone/location to the beginning of the cron schedule to change the time zone. Default is UTC.

See the NewTicker section for examples.

Cronticker calculates the duration until the next scheduled 'tick' based on the cron schedule, and starts a new timer based on the duration calculated.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ticker

type Ticker struct {
	C chan time.Time
	// contains filtered or unexported fields
}

Ticker is the struct returned to the user as a proxy to the ticker. The user can check the ticker channel for the next 'tick' via Ticker.C (similar to the user of time.Timer).

func NewTicker

func NewTicker(schedule string) (Ticker, error)

NewTicker returns a Ticker for the UTC Time Zone.

Example
// The Cron schedule can be in Unix or Quartz format. Directives like
// '@weekly' or '@daily' can also be parsed as defined in the
// package github.com/robfig/cron/v3.

// Example: "0 0 * * *"   -> Unix format: Daily at 12 AM UTC
// Example: "0 0 0 * * ?" -> Quartz format: Daily at 12 AM UTC
// Example: "@daily"      -> Directive: Every day at 12 AM UTC

ticker, err := NewTicker("@daily")
if err != nil {
	log.Fatal(err)
}
defer ticker.Stop()

tick := <-ticker.C
log.Print(tick)
Output:

func NewTickerWithLocation

func NewTickerWithLocation(schedule string, loc *time.Location) (Ticker, error)

NewTicker returns a Ticker based on a pre-defined time zone. You can check the ticker channel for the next tick by `Ticker.C`.

func (*Ticker) Reset

func (c *Ticker) Reset(schedule string, loc *time.Location) error

Reset kills the ticker and starts it up again with the new schedule. The channel remains the same.

Example

If you want to change the cron schedule of a ticker instead of creating a new one you can reset it.

ticker, err := NewTicker("0 0 0 ? * SUN")
if err != nil {
	log.Fatal(err)
}
defer ticker.Stop()

<-ticker.C
log.Print("It's Sunday!")

err = ticker.Reset("0 0 0 ? * WED", time.UTC)
if err != nil {
	log.Fatal(err)
}

<-ticker.C
log.Print("It's Wednesday!")
Output:

func (*Ticker) Stop

func (c *Ticker) Stop()

Stop sends the appropriate message on the control channel to kill the Ticker goroutines. It's good practice to use `defer Ticker.Stop()`.

Example
ticker, err := NewTicker("0 0 0 ? * SUN")
if err != nil {
	log.Fatal(err)
}
defer ticker.Stop()

<-ticker.C
Output:

Jump to

Keyboard shortcuts

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