Basic Usage

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gcron"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "time"
  8. )
  9. func main() {
  10. var (
  11. err error
  12. ctx = gctx.New()
  13. )
  14. _, err = gcron.Add(ctx, "* * * * * *", func(ctx context.Context) {
  15. g.Log().Print(ctx, "Every second")
  16. }, "MySecondCronJob")
  17. if err != nil {
  18. panic(err)
  19. }
  20. _, err = gcron.Add(ctx, "0 30 * * * *", func(ctx context.Context) {
  21. g.Log().Print(ctx, "Every hour on the half hour")
  22. })
  23. if err != nil {
  24. panic(err)
  25. }
  26. _, err = gcron.Add(ctx, "@hourly", func(ctx context.Context) {
  27. g.Log().Print(ctx, "Every hour")
  28. })
  29. if err != nil {
  30. panic(err)
  31. }
  32. _, err = gcron.Add(ctx, "@every 1h30m", func(ctx context.Context) {
  33. g.Log().Print(ctx, "Every hour thirty")
  34. })
  35. if err != nil {
  36. panic(err)
  37. }
  38. g.Dump(gcron.Entries())
  39. time.Sleep(3 * time.Second)
  40. g.Log().Print(ctx, `stop cronjob "MySecondCronJob"`)
  41. gcron.Stop("MySecondCronJob")
  42. time.Sleep(3 * time.Second)
  43. g.Log().Print(ctx, `start cronjob "MySecondCronJob"`)
  44. gcron.Start("MySecondCronJob")
  45. time.Sleep(3 * time.Second)
  46. }

After execution, the output is:

  1. [
  2. {
  3. Name: "MySecondCronJob",
  4. Job: 0x14077e0,
  5. Time: "2021-11-14 12:13:53.445132 +0800 CST m=+0.006167069",
  6. },
  7. {
  8. Name: "cron-1",
  9. Job: 0x14078a0,
  10. Time: "2021-11-14 12:13:53.44515 +0800 CST m=+0.006185688",
  11. },
  12. {
  13. Name: "cron-2",
  14. Job: 0x1407960,
  15. Time: "2021-11-14 12:13:53.445161 +0800 CST m=+0.006196483",
  16. },
  17. {
  18. Name: "cron-3",
  19. Job: 0x1407a20,
  20. Time: "2021-11-14 12:13:53.445218 +0800 CST m=+0.006252937",
  21. },
  22. ]
  23. 2021-11-14 12:13:54.442 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
  24. 2021-11-14 12:13:55.441 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
  25. 2021-11-14 12:13:56.440 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
  26. 2021-11-14 12:13:56.445 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} stop cronjob "MySecondCronJob"
  27. 2021-11-14 12:13:59.445 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} start cronjob "MySecondCronJob"
  28. 2021-11-14 12:14:00.443 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
  29. 2021-11-14 12:14:01.442 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
  30. 2021-11-14 12:14:02.443 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second

AddSingleton Add Singleton Cron Job

A singleton cron job is one that only allows one instance of the job to run at any time. If a second instance of the job is triggered while the first is still running, it will exit without executing and wait for the next scheduled trigger. Use AddSingleton to add singleton cron jobs.

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gcron"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "time"
  8. )
  9. func main() {
  10. var (
  11. err error
  12. ctx = gctx.New()
  13. )
  14. _, err = gcron.AddSingleton(ctx, "* * * * * *", func(ctx context.Context) {
  15. g.Log().Print(ctx, "doing")
  16. time.Sleep(2 * time.Second)
  17. })
  18. if err != nil {
  19. panic(err)
  20. }
  21. select {}
  22. }

After execution, the output is:

  1. 2021-11-14 12:16:54.073 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
  2. 2021-11-14 12:16:57.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
  3. 2021-11-14 12:17:00.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
  4. 2021-11-14 12:17:03.071 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
  5. 2021-11-14 12:17:06.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
  6. 2021-11-14 12:17:09.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
  7. ...

AddOnce Add One-Time Cron Job

A one-time cron job is added using the AddOnce method, which runs just once and then automatically destroys itself. The Size method allows you to check its status. The related methods are:

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gcron"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "time"
  8. )
  9. func main() {
  10. var (
  11. ctx = gctx.New()
  12. )
  13. cron := gcron.New()
  14. array := garray.New(true)
  15. cron.AddOnce(ctx, "@every 2s", func(ctx context.Context) {
  16. array.Append(1)
  17. })
  18. fmt.Println(cron.Size(),array.Len())
  19. time.Sleep(3000 * time.Millisecond)
  20. fmt.Println(cron.Size(),array.Len())
  21. }

After execution, the output is:

  1. 1 0
  2. 0 1

AddTimes Add Specified Number of Cron Jobs

The AddTimes method adds a cron job that runs a specified number of times. Once it completes the times count, the job auto-destructs. The Size method shows its status. Related methods:

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gcron"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "time"
  8. )
  9. func main() {
  10. var (
  11. ctx = gctx.New()
  12. )
  13. cron := gcron.New()
  14. array := garray.New(true)
  15. cron.AddTimes(ctx, "@every 2s", 2,func(ctx context.Context) {
  16. array.Append(1)
  17. })
  18. fmt.Println(cron.Size(), array.Len())
  19. time.Sleep(3000 * time.Millisecond)
  20. fmt.Println(cron.Size(), array.Len())
  21. time.Sleep(3000 * time.Millisecond)
  22. fmt.Println(cron.Size(), array.Len())
  23. }

After execution, the output is:

  1. 1 0
  2. 1 1
  3. 0 2

Entries Get Registered Cron Job List

The Entries method fetches information on all registered cron jobs and returns them as a slice (sorted by registration time asc). Related methods:

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gcron"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "time"
  8. )
  9. func main() {
  10. var (
  11. ctx = gctx.New()
  12. )
  13. cron := gcron.New()
  14. array := garray.New(true)
  15. cron.AddTimes(ctx, "@every 1s", 2,func(ctx context.Context) {
  16. array.Append(1)
  17. },"cron1")
  18. cron.AddOnce(ctx, "@every 1s",func(ctx context.Context) {
  19. array.Append(1)
  20. },"cron2")
  21. entries := cron.Entries()
  22. for k, v := range entries{
  23. fmt.Println(k,v.Name,v.Time)
  24. }
  25. time.Sleep(3000 * time.Millisecond)
  26. fmt.Println(array.Len())
  27. }

After execution, the output is:

  1. 0 cron2 2022-02-09 10:11:47.2421345 +0800 CST m=+0.159116501
  2. 1 cron1 2022-02-09 10:11:47.2421345 +0800 CST m=+0.159116501
  3. 3

Search Search for a Cron Job

The Search function returns a scheduled task with a specified “name” (returning a *Entry pointer). If not found, it returns nil. Related methods:

  1. func main() {
  2. var (
  3. ctx = gctx.New()
  4. )
  5. cron := gcron.New()
  6. array := garray.New(true)
  7. cron.AddTimes(ctx, "@every 1s", 2,func(ctx context.Context) {
  8. array.Append(1)
  9. },"cron1")
  10. cron.AddOnce(ctx, "@every 1s",func(ctx context.Context) {
  11. array.Append(1)
  12. },"cron2")
  13. search := cron.Search("cron2")
  14. g.Log().Print(ctx, search)
  15. time.Sleep(3000 * time.Millisecond)
  16. fmt.Println(array.Len())
  17. // Output:
  18. // 3
  19. }

After execution, the output is:

  1. 2022-02-09 10:52:30.011 {18a909957cfed11680c1b145da1ef096} {"Name":"cron2","Time":"2022-02-09T10:52:29.9972842+08:00"}

Stop Pause a Cron Job

The Stop method stops a cron job (Stop will pause but not delete). You can specify the task name with the name parameter. If name is not specified, it will stop the entire cron. Related methods:

  1. func main() {
  2. var (
  3. ctx = gctx.New()
  4. )
  5. cron := gcron.New()
  6. array := garray.New(true)
  7. cron.AddTimes(ctx, "@every 2s", 1,func(ctx context.Context) {
  8. array.Append(1)
  9. },"cron1")
  10. cron.AddOnce(ctx, "@every 2s",func(ctx context.Context) {
  11. array.Append(1)
  12. },"cron2")
  13. fmt.Println(array.Len(),cron.Size())
  14. cron.Stop("cron2")
  15. fmt.Println(array.Len(),cron.Size())
  16. time.Sleep(3000 * time.Millisecond)
  17. fmt.Println(array.Len(),cron.Size())
  18. // Output:
  19. // 1
  20. // 1
  21. }

After execution, the output is:

  1. 0 2
  2. 0 2
  3. 1 1

Cron Job - Usage - 图1info

The Stop method marks a cron job as paused and returns immediately without waiting for the current execution to end. In some scenarios, you might need to wait for the current execution to finish before exiting. Starting from version v2.8, the StopGracefully method has been introduced to gracefully pause cron jobs. This method blocks until the current execution completes before returning.

Remove Stop and Delete a Job

The Remove method deletes a cron job based on name name (stopping and deleting it). Related methods:

  1. func main() {
  2. var (
  3. ctx = gctx.New()
  4. )
  5. cron := gcron.New()
  6. array := garray.New(true)
  7. cron.AddTimes(ctx, "@every 2s", 1,func(ctx context.Context) {
  8. array.Append(1)
  9. },"cron1")
  10. cron.AddOnce(ctx, "@every 2s",func(ctx context.Context) {
  11. array.Append(1)
  12. },"cron2")
  13. fmt.Println(array.Len(),cron.Size())
  14. cron.Remove("cron2")
  15. fmt.Println(array.Len(),cron.Size())
  16. time.Sleep(3000 * time.Millisecond)
  17. fmt.Println(array.Len(),cron.Size())
  18. // Output:
  19. // 0 2
  20. // 0 1
  21. // 1 0
  22. }

After execution, the output is:

  1. 0 2
  2. 0 1
  3. 1 0

Start Start a Cron Job

The Start method initiates a cron job (automatically initiated after Add). You can specify the task name with the name parameter. If name is not specified, it will start the entire cron. Related methods:

  1. func main() {
  2. var (
  3. ctx = gctx.New()
  4. )
  5. cron := gcron.New()
  6. array := garray.New(true)
  7. cron.AddOnce(ctx, "@every 2s",func(ctx context.Context) {
  8. array.Append(1)
  9. },"cron2")
  10. cron.Stop("cron2")
  11. time.Sleep(3000 * time.Millisecond)
  12. fmt.Println(array.Len(),cron.Size())
  13. cron.Start("cron2")
  14. time.Sleep(3000 * time.Millisecond)
  15. fmt.Println(array.Len(),cron.Size())
  16. // Output:
  17. // 0 1
  18. // 1 0
  19. }

After execution, the output is:

  1. 0 1
  2. 1 0