注册任务到调度器里,当任务要执行的时候会使用goroutines调用,这样每个任务都不会发生阻塞。
Golang不仅仅是兼容了linux标准的crontab格式,而且扩展了秒。也就是说正常的crontab是 分 时 小时 月 星期,而robfig cron是 秒 分 时 日 月 星期。
c := cron.New()
c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly", func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.Start()
//这些任务都是异步执行的.
c.AddFunc("@daily", func() { fmt.Println("Every day") })
//获取他下次执行的时间.
inspect(c.Entries())
//关闭着计划任务, 但是不能关闭已经在执行中的任务.
c.Stop()
CRON Expression Format
A cron expression represents a set of times, using 6 space-separated fields.
Field name | Mandatory? | Allowed values | Allowed special characters
---------- | ---------- | -------------- | --------------------------
Seconds | Yes | 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 | * / , - ?
任务多的情况下搭载 https://www.oschina.net/p/workq
本文介绍如何在Golang中使用cron库来安排和执行定时任务,包括兼容并扩展crontab格式,支持秒级调度。通过示例展示了如何注册、启动、停止任务及获取下一次执行时间。
965

被折叠的 条评论
为什么被折叠?



