xcron

package
v0.25.15 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

GoKit - xcron

Cron kits for Golang development.

Features

  • Thread safe and Easy to use
  • Fractional precision to Seconds
  • Compatible with Standard cron expression
  • Support Nonstandard macros definitions
  • Extense Support @every N duration
  • Dynamic add、update、remove、empty cron job

Installation

go get -u github.com/likexian/gokit

Importing

import (
    "github.com/likexian/gokit/xcron"
)

Documentation

Visit the docs on GoDoc

Field of rule

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            | * / , -
Month        | Yes        | 1–12 or JAN–DEC | * / , -
Day of week  | Yes        | 0–6 or SUN–SAT  | * / , -

Predefined rule

Entry                  | Description                                | Equivalent To
---------------------- | ------------------------------------------ | -------------
@yearly (or @annually) | Run once a year, midnight, Jan. 1st        | 0 0 0 1 1 *
@monthly               | Run once a month, midnight, first of month | 0 0 0 1 * *
@weekly                | Run once a week, midnight between Sat/Sun  | 0 0 0 * * 0
@daily (or @midnight)  | Run once a day, midnight                   | 0 0 0 * * *
@hourly                | Run once an hour, beginning of hour        | 0 0 * * * *

Example

Cron service
// start a cron service
service := xcron.New()

// add a job to service, specify the rule and loop func
id, err := service.Add("@every second", func(){fmt.Println("add a echo")})

// update exists job by job id
err = service.Set(id, "@every second", func(){fmt.Println("set a echo")})

// delete exists job from service, job will stop
service.Del(id)

// clear all jobs, jobs will stop
service.Empty()

// wait for all job exit
service.Wait()
Parse cron rule
// standard cron rule, every hour
rule, err := xcron.Parse("0 * * * *")

// standard cron rule, every half hour
rule, err := xcron.Parse("0,30 * * * *")

// standard cron rule, every half hour
rule, err := xcron.Parse("0/30 * * * *")

// every second, six fields
rule, err := xcron.Parse("* * * * * *")

// every hour
rule, err := xcron.Parse("@every hour")

// every 6 hour
rule, err := xcron.Parse("@every 6 hour")

License

Copyright 2012-2024 Li Kexian

Licensed under the Apache License 2.0

Donation

If this project is helpful, please share it with friends.

If you want to thank me, you can give me a cup of coffee.

Documentation

Index

Constants

View Source
const (
	Second = iota
	Minute
	Hour
	DayOfMonth
	Month
	DayOfWeek
)

Field type of rule

Variables

View Source
var (
	// MonthsMap is month string to int map
	MonthsMap = map[string]int{
		"jan": 1,
		"feb": 2,
		"mar": 3,
		"apr": 4,
		"may": 5,
		"jun": 6,
		"jul": 7,
		"aug": 8,
		"sep": 9,
		"oct": 10,
		"nov": 11,
		"dec": 12,
	}
	// DayOfWeekMap is day of week string to int map
	DayOfWeekMap = map[string]int{
		"sun": 0,
		"mon": 1,
		"tue": 2,
		"wed": 3,
		"thu": 4,
		"fri": 5,
		"sat": 6,
	}
)

Functions

func Author

func Author() string

Author returns package author

func License

func License() string

License returns package license

func Version

func Version() string

Version returns package version

Types

type Job

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

Job is a cron job

type Rule

type Rule struct {
	Second     []int
	Minute     []int
	Hour       []int
	DayOfMonth []int
	Month      []int
	DayOfWeek  []int
}

Rule is parsed cron rule

func MustParse

func MustParse(s string) Rule

MustParse do parse and returns rule, panic if error

func Parse

func Parse(s string) (r Rule, err error)

Parse parse single cron rule Base on https://en.wikipedia.org/wiki/Cron and extensed Fields: second minute hour dayOfMonth month dayOfWeek

  • * * * * *

type Service

type Service struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Service is cron service

func New

func New() *Service

New returns new cron service

func (*Service) Add

func (s *Service) Add(rule string, loop func(), tidy ...func()) (string, error)

Add add new cron job to service

func (*Service) Del

func (s *Service) Del(id string)

Del del cron job from service by id

func (*Service) Empty

func (s *Service) Empty()

Empty empty the cron job service

func (*Service) Has

func (s *Service) Has(id string) bool

Has returns if cron job is running

func (*Service) Len

func (s *Service) Len() int

Len returns running cron job number

func (*Service) Set

func (s *Service) Set(id, rule string, loop func(), tidy ...func()) error

Set update service cron job

func (*Service) Wait

func (s *Service) Wait()

Wait wait for all cron job exit

Jump to

Keyboard shortcuts

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