mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
fix https://github.com/gogf/gf/issues/3999 1. fix jobWaiter sync.WaitGroup data race 2. fix logger glog.ILogger data race --------- Co-authored-by: hailaz <739476267@qq.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gcron_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gcron"
|
|
"github.com/gogf/gf/v2/os/glog"
|
|
)
|
|
|
|
func ExampleCron_AddSingleton() {
|
|
gcron.AddSingleton(ctx, "* * * * * *", func(ctx context.Context) {
|
|
glog.Print(context.TODO(), "doing")
|
|
time.Sleep(2 * time.Second)
|
|
})
|
|
select {}
|
|
}
|
|
|
|
func ExampleCron_gracefulShutdown() {
|
|
_, err := gcron.Add(ctx, "*/2 * * * * *", func(ctx context.Context) {
|
|
g.Log().Debug(ctx, "Every 2s job start")
|
|
time.Sleep(5 * time.Second)
|
|
g.Log().Debug(ctx, "Every 2s job after 5 second end")
|
|
}, "MyCronJob1")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-quit
|
|
glog.Printf(ctx, "Signal received: %s, stopping cron", sig)
|
|
|
|
glog.Print(ctx, "Waiting for all cron jobs to complete...")
|
|
gcron.StopGracefully()
|
|
glog.Print(ctx, "All cron jobs completed")
|
|
}
|
|
|
|
func ExampleCron_StopGracefullyNonBlocking() {
|
|
_, err := gcron.Add(ctx, "*/2 * * * * *", func(ctx context.Context) {
|
|
g.Log().Debug(ctx, "Every 2s job start")
|
|
time.Sleep(5 * time.Second)
|
|
g.Log().Debug(ctx, "Every 2s job after 5 second end")
|
|
}, "MyCronJob2")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-quit
|
|
glog.Printf(ctx, "Signal received: %s, stopping cron", sig)
|
|
|
|
glog.Print(ctx, "Waiting for all cron jobs to complete...")
|
|
ctx := gcron.StopGracefullyNonBlocking()
|
|
<-ctx.Done()
|
|
glog.Print(ctx, "All cron jobs completed")
|
|
}
|