gcron.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gcron implements a cron pattern parser and job runner.
  7. package gcron
  8. import (
  9. "context"
  10. "time"
  11. "github.com/gogf/gf/v2/os/glog"
  12. "github.com/gogf/gf/v2/os/gtimer"
  13. )
  14. const (
  15. StatusReady = gtimer.StatusReady
  16. StatusRunning = gtimer.StatusRunning
  17. StatusStopped = gtimer.StatusStopped
  18. StatusClosed = gtimer.StatusClosed
  19. )
  20. var (
  21. // Default cron object.
  22. defaultCron = New()
  23. )
  24. // SetLogger sets the logger for cron.
  25. func SetLogger(logger glog.ILogger) {
  26. defaultCron.SetLogger(logger)
  27. }
  28. // GetLogger returns the logger in the cron.
  29. func GetLogger() glog.ILogger {
  30. return defaultCron.GetLogger()
  31. }
  32. // Add adds a timed task to default cron object.
  33. // A unique `name` can be bound with the timed task.
  34. // It returns and error if the `name` is already used.
  35. func Add(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error) {
  36. return defaultCron.Add(ctx, pattern, job, name...)
  37. }
  38. // AddSingleton adds a singleton timed task, to default cron object.
  39. // A singleton timed task is that can only be running one single instance at the same time.
  40. // A unique `name` can be bound with the timed task.
  41. // It returns and error if the `name` is already used.
  42. func AddSingleton(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error) {
  43. return defaultCron.AddSingleton(ctx, pattern, job, name...)
  44. }
  45. // AddOnce adds a timed task which can be run only once, to default cron object.
  46. // A unique `name` can be bound with the timed task.
  47. // It returns and error if the `name` is already used.
  48. func AddOnce(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error) {
  49. return defaultCron.AddOnce(ctx, pattern, job, name...)
  50. }
  51. // AddTimes adds a timed task which can be run specified times, to default cron object.
  52. // A unique `name` can be bound with the timed task.
  53. // It returns and error if the `name` is already used.
  54. func AddTimes(ctx context.Context, pattern string, times int, job JobFunc, name ...string) (*Entry, error) {
  55. return defaultCron.AddTimes(ctx, pattern, times, job, name...)
  56. }
  57. // DelayAdd adds a timed task to default cron object after `delay` time.
  58. func DelayAdd(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string) {
  59. defaultCron.DelayAdd(ctx, delay, pattern, job, name...)
  60. }
  61. // DelayAddSingleton adds a singleton timed task after `delay` time to default cron object.
  62. func DelayAddSingleton(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string) {
  63. defaultCron.DelayAddSingleton(ctx, delay, pattern, job, name...)
  64. }
  65. // DelayAddOnce adds a timed task after `delay` time to default cron object.
  66. // This timed task can be run only once.
  67. func DelayAddOnce(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string) {
  68. defaultCron.DelayAddOnce(ctx, delay, pattern, job, name...)
  69. }
  70. // DelayAddTimes adds a timed task after `delay` time to default cron object.
  71. // This timed task can be run specified times.
  72. func DelayAddTimes(ctx context.Context, delay time.Duration, pattern string, times int, job JobFunc, name ...string) {
  73. defaultCron.DelayAddTimes(ctx, delay, pattern, times, job, name...)
  74. }
  75. // Search returns a scheduled task with the specified `name`.
  76. // It returns nil if no found.
  77. func Search(name string) *Entry {
  78. return defaultCron.Search(name)
  79. }
  80. // Remove deletes scheduled task which named `name`.
  81. func Remove(name string) {
  82. defaultCron.Remove(name)
  83. }
  84. // Size returns the size of the timed tasks of default cron.
  85. func Size() int {
  86. return defaultCron.Size()
  87. }
  88. // Entries return all timed tasks as slice.
  89. func Entries() []*Entry {
  90. return defaultCron.Entries()
  91. }
  92. // Start starts running the specified timed task named `name`.
  93. // If no`name` specified, it starts the entire cron.
  94. func Start(name ...string) {
  95. defaultCron.Start(name...)
  96. }
  97. // Stop stops running the specified timed task named `name`.
  98. // If no`name` specified, it stops the entire cron.
  99. func Stop(name ...string) {
  100. defaultCron.Stop(name...)
  101. }