cron

package module
v0.0.0-...-36bf715 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: Apache-2.0 Imports: 6 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Golang Cron

Go Report Card LICENSE LICENSE

Cron Expression

FOSSA Status

The reference documentation is found at https://en.wikipedia.org/wiki/Cron#CRON_expression

Field name     Mandatory   Allowed values    Allowed special characters
----------     ----------   --------------    --------------------------
Seconds        No           0-59              * / , -
Minutes        Yes          0-59              * / , -
Hours          Yes          0-23              * / , -
Day of month   Yes          1-31              * / , - L W
Month          Yes          1-12 or JAN-DEC   * / , -
Day of week    Yes          0-6 or SUN-SAT    * / , - L #
Year           No           1970–2099         * / , -

Install

    go get -u github.com/flyaways/cron

Usage

package main

import (
	"fmt"
	"time"

	"github.com/flyaways/cron"
)

type task struct {
	cron.Job
	id   string
	expr string
}

func (t *task) Run() {
	fmt.Println(time.Now().Format(time.RFC3339Nano))
}

func (t *task) ID() string {
	return t.id
}

func (t *task) Cronexpr() string {
	return t.expr
}

func main() {
	c := cron.New()
	c.Start()

	c.Add(&task{
		id:   "AF0783D4-D16A-4CAE-8866-4E0648AE3651",
		expr: "0/2 * * * * ?",
	})
	time.Sleep(time.Second * 10)

	c.Update(&task{
		id:   "AF0783D4-D16A-4CAE-8866-4E0648AE3651",
		expr: "0/5 * * * * ?",
	})
	time.Sleep(time.Second * 10)

	c.Del(&task{
		id: "AF0783D4-D16A-4CAE-8866-4E0648AE3651",
	})
	time.Sleep(time.Second * 10)

	c.Stop()
}

Reference

License

FOSSA Status

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrJobIDNotExists = errors.New("job id not exits")
	ErrJobIDExists    = errors.New("job id exists")
)

Functions

This section is empty.

Types

type Daemon

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

func New

func New() *Daemon

func NewWithLocation

func NewWithLocation(location *time.Location) *Daemon

func (*Daemon) Add

func (c *Daemon) Add(j Job) error

func (*Daemon) Del

func (c *Daemon) Del(j Job) error

func (*Daemon) SnapShot

func (c *Daemon) SnapShot() Schedules

func (*Daemon) Start

func (c *Daemon) Start()

func (*Daemon) Stop

func (c *Daemon) Stop()

func (*Daemon) Update

func (c *Daemon) Update(j Job) error

type Job

type Job interface {
	Run()
	ID() string
	Cronexpr() string
}

type Schedule

type Schedule struct {
	Expression *cronexpr.Expression
	Next       time.Time
	Prev       time.Time
	Job
}

type Schedules

type Schedules []*Schedule

func (Schedules) Len

func (s Schedules) Len() int

func (Schedules) Less

func (s Schedules) Less(i, j int) bool

func (Schedules) Pos

func (s Schedules) Pos(id string) int

func (Schedules) Swap

func (s Schedules) Swap(i, j int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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