cron

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 5 Imported by: 1

README

scheduler

Go Report Card Godoc Releases LICENSE

Package cron provides mechanism for executing scheduled tasks with cron-like expression.

Documentation & Examples

Documentation & Examples can be found on the packages documentation.

Documentation

Overview

Package cron provides mechanism for executing scheduled tasks with cron-like expression.

Cron expressions are a simple and flexible way to configure a schedule for which an automated task should run.

  • * * * * | | | | \ | | | \ The day of the week (0 = Sunday, 6 = Saturday) | | \ The day of the month (1-31) | \ Month (1-12) \ Hour (0-23) Minute (0-59)

Each component of the expression can be a numerical value, an expression, or a wildcard. All components must match the current time for the job to run.

If the component is a numerical value, then the same component (minute, hour, month, etc...) of the current time must match the exact value for the component.

Components can also be an expression for a mod operation, such as */5 or */2. Where if the remainder from the current times component and the expression is zero, it matches.

Lastly, components can be a wildcard *, which will match any time value.

Some common expressions are:

"* * * * *" Run every minute
"0 * * * *" Run at the start of every hour
"0 0 * * *" Run every day at midnight
"*/5 * * *" Run every 5 minutes
"* */2 * *" Run every 2 hours

Under normal circumstances cron is accurate up-to 1 second. Each job's method is called in a unique goroutine and will recover from any panics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	// Cron pattern describing the schedule of this job
	Pattern string
	// The name of this job, only used for logging
	Name string
	// The method to invoke when the job runs
	Exec func()
}

Job describes a single job that will run based on the pattern

func (Job) WouldRunNow

func (job Job) WouldRunNow() bool

WouldRunNow returns true if the pattern for the job matches the current time

type Tab

type Tab struct {
	// The jobs to run
	Jobs []Job
	// Optional time when the schedule should expire. Set to nil for no expiry date.
	ExpireAfter *time.Time
	// The frequency to check if the jobs should run. By default this is 60 seconds and should not be changed.
	Interval time.Duration
}

Tab describes a group of jobs, known as a "Tab"

func New

func New(Jobs []Job) *Tab

New create a tab for the given slice of jobs. Does not start the tab.

Example
package main

import (
	"github.com/ecnepsnai/cron"
)

func main() {
	schedule := cron.New([]cron.Job{
		{
			Pattern: "* * * * *",
			Name:    "RunsEveryMinute",
			Exec: func() {
				// This would run every minute
			},
		},
		{
			Pattern: "0 * * * *",
			Name:    "OnTheHour",
			Exec: func() {
				// This would run at the start of every hour
			},
		},
		{
			Pattern: "*/5 * * * *",
			Name:    "Every5Minutes",
			Exec: func() {
				// This would run every 5 minutes
			},
		},
	})
	// This will start the cron at (or as close to as possible) 0 seconds of the next minute
	go schedule.Start()
}

func (*Tab) ForceStart

func (s *Tab) ForceStart()

ForceStart will start the schedule immediately. This can have the undesired effect of jobs running at most 60 seconds later than they would if you used `Start`.

This method blocks.

func (*Tab) Start

func (s *Tab) Start()

Start will wait the next minute (up to 60 seconds) and then start the tab. This is the optimal way to start the tab since jobs will run at the start of the minute.

This method blocks.

func (*Tab) StopSoon

func (s *Tab) StopSoon()

StopSoon will stop the tab in no more than 60 seconds

Jump to

Keyboard shortcuts

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