gf/os/gcron/gcron.go

122 lines
4.0 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2018-10-22 16:53:43 +08:00
//
// 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.
2018-10-22 16:53:43 +08:00
2019-01-15 23:27:47 +08:00
// Package gcron implements a cron pattern parser and job runner.
2018-10-22 16:53:43 +08:00
package gcron
2019-01-18 22:02:17 +08:00
import (
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/glog"
2019-06-19 09:06:52 +08:00
"time"
2019-07-29 21:01:19 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gtimer"
2019-01-18 22:02:17 +08:00
)
2019-01-16 22:34:22 +08:00
const (
2021-01-22 23:09:42 +08:00
StatusReady = gtimer.StatusReady
StatusRunning = gtimer.StatusRunning
StatusStopped = gtimer.StatusStopped
StatusClosed = gtimer.StatusClosed
2018-12-30 11:08:07 +08:00
)
2018-10-22 16:53:43 +08:00
var (
2019-06-19 09:06:52 +08:00
// Default cron object.
defaultCron = New()
2018-10-22 16:53:43 +08:00
)
2021-08-20 14:09:15 +08:00
// SetLogger sets the logger for cron.
func SetLogger(logger *glog.Logger) {
defaultCron.SetLogger(logger)
}
// GetLogger returns the logger in the cron.
func GetLogger() *glog.Logger {
return defaultCron.GetLogger()
}
// Add adds a timed task to default cron object.
2021-08-20 11:52:22 +08:00
// A unique `name` can be bound with the timed task.
// It returns and error if the `name` is already used.
func Add(pattern string, job func(), name ...string) (*Entry, error) {
2019-06-19 09:06:52 +08:00
return defaultCron.Add(pattern, job, name...)
}
// AddSingleton adds a singleton timed task, to default cron object.
// A singleton timed task is that can only be running one single instance at the same time.
2021-08-20 11:52:22 +08:00
// A unique `name` can be bound with the timed task.
// It returns and error if the `name` is already used.
func AddSingleton(pattern string, job func(), name ...string) (*Entry, error) {
2019-06-19 09:06:52 +08:00
return defaultCron.AddSingleton(pattern, job, name...)
2018-12-30 11:08:07 +08:00
}
// AddOnce adds a timed task which can be run only once, to default cron object.
2021-08-20 11:52:22 +08:00
// A unique `name` can be bound with the timed task.
// It returns and error if the `name` is already used.
func AddOnce(pattern string, job func(), name ...string) (*Entry, error) {
2019-06-19 09:06:52 +08:00
return defaultCron.AddOnce(pattern, job, name...)
}
// AddTimes adds a timed task which can be run specified times, to default cron object.
2021-08-20 11:52:22 +08:00
// A unique `name` can be bound with the timed task.
// It returns and error if the `name` is already used.
func AddTimes(pattern string, times int, job func(), name ...string) (*Entry, error) {
2019-06-19 09:06:52 +08:00
return defaultCron.AddTimes(pattern, times, job, name...)
2019-01-16 22:34:22 +08:00
}
2021-08-20 11:52:22 +08:00
// DelayAdd adds a timed task to default cron object after `delay` time.
2019-06-19 09:06:52 +08:00
func DelayAdd(delay time.Duration, pattern string, job func(), name ...string) {
defaultCron.DelayAdd(delay, pattern, job, name...)
2018-12-30 11:08:07 +08:00
}
2021-08-20 11:52:22 +08:00
// DelayAddSingleton adds a singleton timed task after `delay` time to default cron object.
2019-06-19 09:06:52 +08:00
func DelayAddSingleton(delay time.Duration, pattern string, job func(), name ...string) {
defaultCron.DelayAddSingleton(delay, pattern, job, name...)
2018-12-30 11:08:07 +08:00
}
2021-08-20 11:52:22 +08:00
// DelayAddOnce adds a timed task after `delay` time to default cron object.
// This timed task can be run only once.
2019-06-19 09:06:52 +08:00
func DelayAddOnce(delay time.Duration, pattern string, job func(), name ...string) {
defaultCron.DelayAddOnce(delay, pattern, job, name...)
2018-11-05 10:29:58 +08:00
}
2021-08-20 11:52:22 +08:00
// DelayAddTimes adds a timed task after `delay` time to default cron object.
// This timed task can be run specified times.
2019-06-19 09:06:52 +08:00
func DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ...string) {
defaultCron.DelayAddTimes(delay, pattern, times, job, name...)
2019-01-16 22:34:22 +08:00
}
2021-08-20 11:52:22 +08:00
// Search returns a scheduled task with the specified `name`.
// It returns nil if no found.
func Search(name string) *Entry {
2019-06-19 09:06:52 +08:00
return defaultCron.Search(name)
}
2021-08-20 11:52:22 +08:00
// Remove deletes scheduled task which named `name`.
func Remove(name string) {
2019-06-19 09:06:52 +08:00
defaultCron.Remove(name)
2018-10-22 16:53:43 +08:00
}
// Size returns the size of the timed tasks of default cron.
2018-12-31 17:46:04 +08:00
func Size() int {
2019-06-19 09:06:52 +08:00
return defaultCron.Size()
2018-12-31 17:46:04 +08:00
}
// Entries return all timed tasks as slice.
func Entries() []*Entry {
2019-06-19 09:06:52 +08:00
return defaultCron.Entries()
}
2021-08-20 11:52:22 +08:00
// Start starts running the specified timed task named `name`.
2021-08-27 20:16:29 +08:00
// If no`name` specified, it starts the entire cron.
func Start(name ...string) {
defaultCron.Start(name...)
}
2021-08-20 11:52:22 +08:00
// Stop stops running the specified timed task named `name`.
2021-08-27 20:16:29 +08:00
// If no`name` specified, it stops the entire cron.
func Stop(name ...string) {
defaultCron.Stop(name...)
2018-10-22 16:53:43 +08:00
}