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 ¶
CronTicker is the struct/object returned to the user as a proxy to the ticker. The user can check the ticker channel for the next 'tick' via CronTicker.C (kind of what you can do with 'time.Time.C').
func NewTicker ¶
func NewTicker(schedule string) (CronTicker, error)
NewTicker returns a CronTicker object. 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.
Example ¶
ticker, err := NewTicker("TZ=UTC 0 0 0 ? * SUN") if err != nil { log.Fatal(err) } defer ticker.Stop() <-ticker.C
Output: