croner

package module
v0.0.0-...-a503739 Latest Latest
Warning

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

Go to latest
Published: May 30, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

README

DON'T USE IT IN PROD

抱歉, 这个程序是刚接触golang写的。 很多地方完全错误。 请不要在生产中使用

croner

Manage crontab in golang, Star is the best praise and gift...

Function

In development, we often need to manage time tasks. Golang Cron provides some good basic schedule functions. But in some complex scenarios. It's not enough. Like:

  • Any task could fail. If the task fails. try again or stop this task?
  • The user set @every 5s execution. But It need 10 sec to finish this task. How to ensure a task is performed only once per unit time.
  • I wanna watch all tasks' status. Is it running? What's the next execution time.
  • I wanna know how many times the task executed and how many times it was successful
  • I wanna limit the number of concurrent tasks
  • I can return some results when the task is complete. And I can add some hook functions to handle those results

View the test to know more...

Example

Run example

go get -u "github.com/OhBonsai/croner"
go get -u "github.com/gin-gonic/gin"
go get -u "github.com/gorilla/websocket"
cd $GOPATH/src/github.com/OhBonsai/croner/example

go run server.go
# Open localhost:8000

image

  1. Define some time task
  2. View all task status
  3. Each task will send data to terminal periodically

中文

360度托马斯回旋平沙落雁五体投地雪地跪求点赞

在开发中,经常遇到一些需要定时任务的场景。各个语言都有定时语言的库,Golang Cron 提供了Crontab Golang语言版本。这个库非常不错,提供最基本的定时任务编排的功能。但是一些复杂需求无法满足,比如

  • 任何定时任务都有可能失败,失败了就panic了,这样非常不友好。最起码能够让我控制,失败是重试还是停止
  • 某些任务执行周期要10s, 而用户设置的5s一执行,我能不能保证任何时间这个任务只执行一次
  • 我想实时的看到任务的状态,比如是不是在运行?下次运行时间?上次运行时间?
  • 我想看到任务执行了多少次,成功了多少次
  • 我想要限制最大任务数量,比如超过10个任务在执行,不运行新的任务执行
  • 任务执行完了可以告诉我逻辑上有错误,还是有结果。我还可以加上一些钩子函数来处理任务执行的结果

详细请查看博客

Documentation

Index

Constants

View Source
const (
	IDLE = iota
	RUNNING
	FAIL
	STOP
)

Variables

This section is empty.

Functions

func Next

func Next(spec string) (time.Time, error)

func OnJobReturn

func OnJobReturn(f func(runReturn *JobRunReturnWithEid), order ...int)

func SetDefaultManager

func SetDefaultManager(manager *CronManager)

func Validate

func Validate(spec string) bool

Types

type CronHook

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

type CronHooks

type CronHooks []CronHook

func (CronHooks) Add

func (h CronHooks) Add(fn func(runReturn *JobRunReturnWithEid), order ...int) CronHooks

func (CronHooks) Len

func (h CronHooks) Len() int

Sorting function

func (CronHooks) Less

func (h CronHooks) Less(i, j int) bool

Sorting function

func (CronHooks) Run

func (h CronHooks) Run(runReturn *JobRunReturnWithEid)

func (CronHooks) Swap

func (h CronHooks) Swap(i, j int)

Sorting function

type CronManager

type CronManager struct {
	JobMap   map[int]*WrappedJob
	MainCron *cron.Cron
	// contains filtered or unexported fields
}
var DefaultManager *CronManager

func NewCronManager

func NewCronManager(c CronManagerConfig) *CronManager

func (*CronManager) Add

func (r *CronManager) Add(spec string, j JobInf, info interface{}) (int, error)

func (*CronManager) DisActive

func (r *CronManager) DisActive(id int)

func (*CronManager) Job

func (r *CronManager) Job(id int) (*WrappedJob, bool)

func (*CronManager) Remove

func (r *CronManager) Remove(id int)

func (*CronManager) RemoveAll

func (r *CronManager) RemoveAll()

func (*CronManager) SetConfig

func (r *CronManager) SetConfig(cfg CronManagerConfig)

func (*CronManager) Start

func (r *CronManager) Start()

func (*CronManager) Stop

func (r *CronManager) Stop()

type CronManagerConfig

type CronManagerConfig struct {
	IgnorePanic   bool
	OnlyOne       bool
	PoolSize      uint
	TimeInterrupt uint
}

type JobInf

type JobInf interface {
	Run() JobRunReturn
}

type JobRunError

type JobRunError struct {
	Message string
}

func (JobRunError) Error

func (err JobRunError) Error() string

type JobRunReturn

type JobRunReturn struct {
	Value interface{}
	Error error
}

type JobRunReturnWithEid

type JobRunReturnWithEid struct {
	JobRunReturn
	Eid int
}

type WrappedJob

type WrappedJob struct {
	Id    int
	Name  string
	Inner JobInf

	SuccessCount uint32
	TotalCount   uint32

	Info interface{}
	Next time.Time
	// contains filtered or unexported fields
}

func NewWrappedJob

func NewWrappedJob(job JobInf, r *CronManager) *WrappedJob

func (*WrappedJob) Now

func (j *WrappedJob) Now()

func (*WrappedJob) Run

func (j *WrappedJob) Run()

func (*WrappedJob) Status

func (j *WrappedJob) Status() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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