gcron.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "github.com/gogf/gf/os/glog"
  10. "time"
  11. "github.com/gogf/gf/os/gtimer"
  12. )
  13. const (
  14. StatusReady = gtimer.StatusReady
  15. StatusRunning = gtimer.StatusRunning
  16. StatusStopped = gtimer.StatusStopped
  17. StatusClosed = gtimer.StatusClosed
  18. )
  19. var (
  20. // Default cron object.
  21. defaultCron = New()
  22. )
  23. // SetLogger sets the logger for cron.
  24. func SetLogger(logger *glog.Logger) {
  25. defaultCron.SetLogger(logger)
  26. }
  27. // GetLogger returns the logger in the cron.
  28. func GetLogger() *glog.Logger {
  29. return defaultCron.GetLogger()
  30. }
  31. // SetLogPath sets the logging folder path for default cron object.
  32. // Deprecated, use SetLogger instead.
  33. func SetLogPath(path string) {
  34. defaultCron.SetLogPath(path)
  35. }
  36. // GetLogPath returns the logging folder path of default cron object.
  37. // Deprecated, use GetLogger instead.
  38. func GetLogPath() string {
  39. return defaultCron.GetLogPath()
  40. }
  41. // SetLogLevel sets the logging level for default cron object.
  42. // Deprecated, use SetLogger instead.
  43. func SetLogLevel(level int) {
  44. defaultCron.SetLogLevel(level)
  45. }
  46. // GetLogLevel returns the logging level for default cron object.
  47. // Deprecated, use GetLogger instead.
  48. func GetLogLevel() int {
  49. return defaultCron.GetLogLevel()
  50. }
  51. // Add adds a timed task 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 Add(pattern string, job func(), name ...string) (*Entry, error) {
  55. return defaultCron.Add(pattern, job, name...)
  56. }
  57. // AddSingleton adds a singleton timed task, to default cron object.
  58. // A singleton timed task is that can only be running one single instance at the same time.
  59. // A unique `name` can be bound with the timed task.
  60. // It returns and error if the `name` is already used.
  61. func AddSingleton(pattern string, job func(), name ...string) (*Entry, error) {
  62. return defaultCron.AddSingleton(pattern, job, name...)
  63. }
  64. // AddOnce adds a timed task which can be run only once, to default cron object.
  65. // A unique `name` can be bound with the timed task.
  66. // It returns and error if the `name` is already used.
  67. func AddOnce(pattern string, job func(), name ...string) (*Entry, error) {
  68. return defaultCron.AddOnce(pattern, job, name...)
  69. }
  70. // AddTimes adds a timed task which can be run specified times, to default cron object.
  71. // A unique `name` can be bound with the timed task.
  72. // It returns and error if the `name` is already used.
  73. func AddTimes(pattern string, times int, job func(), name ...string) (*Entry, error) {
  74. return defaultCron.AddTimes(pattern, times, job, name...)
  75. }
  76. // DelayAdd adds a timed task to default cron object after `delay` time.
  77. func DelayAdd(delay time.Duration, pattern string, job func(), name ...string) {
  78. defaultCron.DelayAdd(delay, pattern, job, name...)
  79. }
  80. // DelayAddSingleton adds a singleton timed task after `delay` time to default cron object.
  81. func DelayAddSingleton(delay time.Duration, pattern string, job func(), name ...string) {
  82. defaultCron.DelayAddSingleton(delay, pattern, job, name...)
  83. }
  84. // DelayAddOnce adds a timed task after `delay` time to default cron object.
  85. // This timed task can be run only once.
  86. func DelayAddOnce(delay time.Duration, pattern string, job func(), name ...string) {
  87. defaultCron.DelayAddOnce(delay, pattern, job, name...)
  88. }
  89. // DelayAddTimes adds a timed task after `delay` time to default cron object.
  90. // This timed task can be run specified times.
  91. func DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ...string) {
  92. defaultCron.DelayAddTimes(delay, pattern, times, job, name...)
  93. }
  94. // Search returns a scheduled task with the specified `name`.
  95. // It returns nil if no found.
  96. func Search(name string) *Entry {
  97. return defaultCron.Search(name)
  98. }
  99. // Remove deletes scheduled task which named `name`.
  100. func Remove(name string) {
  101. defaultCron.Remove(name)
  102. }
  103. // Size returns the size of the timed tasks of default cron.
  104. func Size() int {
  105. return defaultCron.Size()
  106. }
  107. // Entries return all timed tasks as slice.
  108. func Entries() []*Entry {
  109. return defaultCron.Entries()
  110. }
  111. // Start starts running the specified timed task named `name`.
  112. // If no`name` specified, it starts the entire cron.
  113. func Start(name ...string) {
  114. defaultCron.Start(name...)
  115. }
  116. // Stop stops running the specified timed task named `name`.
  117. // If no`name` specified, it stops the entire cron.
  118. func Stop(name ...string) {
  119. defaultCron.Stop(name...)
  120. }