A lightweight distributed cron job library for distributed system.
用于分布式系统的轻量分布式定时任务库。
从 libi/dcron 衍生而来,使用一段时间后发现这个框架存在一些缺陷。
- 不支持本地定时任务
- 节点退出后不能取消注册,在特定场景下会发生任务分配到了已退出的节点上,导致任务无法执行
特性:
在 libi/dcron 基础上,支持:
- 支持本地定时任务
- 支持节点退出取消注册
- 支持懒选举
- 替换robfig/cron为tovenja/cron库,完全兼容且性能更高
Todo:
- 任务执行二次确认
- 完善驱动
- 完善单元测试
- 支持自定义logger
import "github.com/zukadong/crond"
package main
import (
"fmt"
"github.com/zukadong/crond"
"github.com/zukadong/crond/driver/redis"
"time"
)
func main() {
stop := cronJob()
defer stop()
}
func cronJob() func() {
driver := redis.NewDriver(clientRedis())
cron := crond.NewCrond("test-service", driver, crond.WithLazyPick(true))
// 分布式任务
_ = cron.AddFunc("job1", crond.JobDistributed, "*/1 * * * *", func() {
fmt.Println("执行job1: ", time.Now().Format("15:04:05"))
})
// 本地任务
_ = cron.AddFunc("job2", crond.JobLocaled, "*/1 * * * *", func() {
fmt.Println("执行job2: ", time.Now().Format("15:04:05"))
})
cron.Start()
return func() { cron.Stop() }
}
-
兼容 tovenja/cron Option
- WithLocation(loc *time.Location) Option
- WithSeconds() Option
- WithParser(p cron.ScheduleParser) Option
- WithChain(wrappers ...cron.JobWrapper) Option
- WithLogger(logger cron.Logger) Option
-
自定义节点刷新间隔
- WithNodeUpdateInterval(dur time.Duration) Option
-
懒选举(任务执行时,拉取节点列表并选举)
- WithLazyPick(lazy bool) Option