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