cronticker

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cronticker 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.

You can access the channel with `CronTicker.C`.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CronTicker

type CronTicker struct {
	GobNop bool
	C      chan time.Time
	// contains filtered or unexported fields
}

CronTicker 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 CronTicker.C (similar to the user of time.Timer).

func NewTicker

func NewTicker(schedule string) (CronTicker, error)

NewTicker returns a CronTicker struct. You can check the ticker channel for the next tick by `CronTicker.C`

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.

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

// Example: "TZ=America/Los_Angeles 0 0 * * *"   -> Unix format: Daily at 12 AM in Los Angeles
// Example: "TZ=America/Los_Angeles 0 0 0 * * ?" -> Quartz format: Daily at 12 AM in Los Angeles
// Example: "TZ=America/Los_Angeles @daily"      -> Directive: Daily at 12 AM in Los Angeles
// 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 (*CronTicker) Reset

func (c *CronTicker) Reset(schedule string) 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("TZ=UTC 0 0 0 ? * SUN")
if err != nil {
	log.Fatal(err)
}
defer ticker.Stop()

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

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

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

func (*CronTicker) Stop

func (c *CronTicker) Stop()

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

Example
ticker, err := NewTicker("TZ=UTC 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