基本使用
package main
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/os/gctx"
"time"
)
func main() {
var (
err error
ctx = gctx.New()
)
_, err = gcron.Add(ctx, "* * * * * *", func(ctx context.Context) {
g.Log().Print(ctx, "Every second")
}, "MySecondCronJob")
if err != nil {
panic(err)
}
_, err = gcron.Add(ctx, "0 30 * * * *", func(ctx context.Context) {
g.Log().Print(ctx, "Every hour on the half hour")
})
if err != nil {
panic(err)
}
_, err = gcron.Add(ctx, "@hourly", func(ctx context.Context) {
g.Log().Print(ctx, "Every hour")
})
if err != nil {
panic(err)
}
_, err = gcron.Add(ctx, "@every 1h30m", func(ctx context.Context) {
g.Log().Print(ctx, "Every hour thirty")
})
if err != nil {
panic(err)
}
g.Dump(gcron.Entries())
time.Sleep(3 * time.Second)
g.Log().Print(ctx, `stop cronjob "MySecondCronJob"`)
gcron.Stop("MySecondCronJob")
time.Sleep(3 * time.Second)
g.Log().Print(ctx, `start cronjob "MySecondCronJob"`)
gcron.Start("MySecondCronJob")
time.Sleep(3 * time.Second)
}
执行后,输出结果为:
[
{
Name: "MySecondCronJob",
Job: 0x14077e0,
Time: "2021-11-14 12:13:53.445132 +0800 CST m=+0.006167069",
},
{
Name: "cron-1",
Job: 0x14078a0,
Time: "2021-11-14 12:13:53.44515 +0800 CST m=+0.006185688",
},
{
Name: "cron-2",
Job: 0x1407960,
Time: "2021-11-14 12:13:53.445161 +0800 CST m=+0.006196483",
},
{
Name: "cron-3",
Job: 0x1407a20,
Time: "2021-11-14 12:13:53.445218 +0800 CST m=+0.006252937",
},
]
2021-11-14 12:13:54.442 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:13:55.441 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:13:56.440 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:13:56.445 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} stop cronjob "MySecondCronJob"
2021-11-14 12:13:59.445 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} start cronjob "MySecondCronJob"
2021-11-14 12:14:00.443 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:14:01.442 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:14:02.443 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
单例定时任务
单例定时任务,即同时只能有一个该任务正在运行。当第二个相同的定时任务触发执行时,如果发现已有该任务正在执行,第二个任务将会退出不执行,定时器将会继续等待下一次定时任务的触发检测,以此类推。可以使用 AddSingleton
添加单例定时任务。
package main
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/os/gctx"
"time"
)
func main() {
var (
err error
ctx = gctx.New()
)
_, err = gcron.AddSingleton(ctx, "* * * * * *", func(ctx context.Context) {
g.Log().Print(ctx, "doing")
time.Sleep(2 * time.Second)
})
if err != nil {
panic(err)
}
select {}
}
执行后,输出结果为:
2021-11-14 12:16:54.073 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:16:57.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:00.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:03.071 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:06.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:09.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
...
单次定时任务
单次定时任务, AddOnce
方法用于添加只运行一次的定时任务,当运行一次数后该定时任务自动销毁,Size
方法可以查看运行状态,相关方法:
func main() {
var (
ctx = gctx.New()
)
cron := gcron.New()
array := garray.New(true)
cron.AddOnce(ctx, "@every 2s", func(ctx context.Context) {
array.Append(1)
})
fmt.Println(cron.Size(),array.Len())
time.Sleep(3000 * time.Millisecond)
fmt.Println(cron.Size(),array.Len())
}
执行后,输出结果为:
1 0
0 1
指定次数的定时任务
指定次数的定时任务, AddTimes
方法用于添加运行指定次数的定时任务,当运行 times
次数后该定时任务自动销毁, Size
方法可以查看运行状态,相关方法:
func main() {
var (
ctx = gctx.New()
)
cron := gcron.New()
array := garray.New(true)
cron.AddTimes(ctx, "@every 2s", 2,func(ctx context.Context) {
array.Append(1)
})
fmt.Println(cron.Size(), array.Len())
time.Sleep(3000 * time.Millisecond)
fmt.Println(cron.Size(), array.Len())
time.Sleep(3000 * time.Millisecond)
fmt.Println(cron.Size(), array.Len())
}
执行后,输出结果为:
1 0
1 1
0 2