2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// 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.
|
2019-01-16 13:35:16 +08:00
|
|
|
|
//
|
2019-01-15 23:27:47 +08:00
|
|
|
|
// 定时任务.
|
2018-10-22 16:53:43 +08:00
|
|
|
|
package gcron
|
|
|
|
|
|
2019-01-18 22:02:17 +08:00
|
|
|
|
import (
|
2019-02-02 16:18:25 +08:00
|
|
|
|
"github.com/gogf/gf/g/os/gtimer"
|
2019-01-18 22:02:17 +08:00
|
|
|
|
"math"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2018-10-25 10:08:08 +08:00
|
|
|
|
|
2019-01-16 22:34:22 +08:00
|
|
|
|
const (
|
2019-01-21 22:09:51 +08:00
|
|
|
|
STATUS_READY = gtimer.STATUS_READY
|
|
|
|
|
STATUS_RUNNING = gtimer.STATUS_RUNNING
|
|
|
|
|
STATUS_STOPPED = gtimer.STATUS_STOPPED
|
|
|
|
|
STATUS_CLOSED = gtimer.STATUS_CLOSED
|
2019-01-16 22:34:22 +08:00
|
|
|
|
|
|
|
|
|
gDEFAULT_TIMES = math.MaxInt32
|
2018-12-30 11:08:07 +08:00
|
|
|
|
)
|
2018-10-22 16:53:43 +08:00
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
// 默认的cron管理对象
|
2018-10-25 10:08:08 +08:00
|
|
|
|
defaultCron = New()
|
2018-10-22 16:53:43 +08:00
|
|
|
|
)
|
|
|
|
|
|
2019-03-19 13:58:18 +08:00
|
|
|
|
// 设置日志输出路径
|
|
|
|
|
func SetLogPath(path string) {
|
|
|
|
|
defaultCron.SetLogPath(path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取设置的日志输出路径
|
|
|
|
|
func GetLogPath() string {
|
|
|
|
|
return defaultCron.GetLogPath()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置日志输出等级。
|
|
|
|
|
func SetLogLevel(level int) {
|
|
|
|
|
defaultCron.SetLogLevel(level)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取日志输出等级。
|
|
|
|
|
func GetLogLevel() int {
|
|
|
|
|
return defaultCron.GetLogLevel()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加定时任务,可以给定名字,以便于后续执行删除
|
2018-12-30 11:08:07 +08:00
|
|
|
|
func Add(pattern string, job func(), name ... string) (*Entry, error) {
|
|
|
|
|
return defaultCron.Add(pattern, job, name...)
|
2018-10-24 09:06:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-30 11:08:07 +08:00
|
|
|
|
// 添加单例运行定时任务
|
|
|
|
|
func AddSingleton(pattern string, job func(), name ... string) (*Entry, error) {
|
|
|
|
|
return defaultCron.AddSingleton(pattern, job, name...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加只运行一次的定时任务
|
|
|
|
|
func AddOnce(pattern string, job func(), name ... string) (*Entry, error) {
|
|
|
|
|
return defaultCron.AddOnce(pattern, job, name...)
|
2018-10-24 09:06:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 22:34:22 +08:00
|
|
|
|
// 添加运行指定次数的定时任务
|
|
|
|
|
func AddTimes(pattern string, times int, job func(), name ... string) (*Entry, error) {
|
|
|
|
|
return defaultCron.AddTimes(pattern, times, job, name...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 22:02:17 +08:00
|
|
|
|
// 延迟添加定时任务
|
|
|
|
|
func DelayAdd(delay time.Duration, pattern string, job func(), name ... string) {
|
2018-12-30 11:08:07 +08:00
|
|
|
|
defaultCron.DelayAdd(delay, pattern, job, name...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 延迟添加单例定时任务,delay参数单位为秒
|
2019-01-18 22:02:17 +08:00
|
|
|
|
func DelayAddSingleton(delay time.Duration, pattern string, job func(), name ... string) {
|
2018-12-30 11:08:07 +08:00
|
|
|
|
defaultCron.DelayAddSingleton(delay, pattern, job, name...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 延迟添加只运行一次的定时任务,delay参数单位为秒
|
2019-01-18 22:02:17 +08:00
|
|
|
|
func DelayAddOnce(delay time.Duration, pattern string, job func(), name ... string) {
|
2018-12-30 11:08:07 +08:00
|
|
|
|
defaultCron.DelayAddOnce(delay, pattern, job, name...)
|
2018-11-05 10:29:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 22:34:22 +08:00
|
|
|
|
// 延迟添加运行指定次数的定时任务,delay参数单位为秒
|
2019-01-18 22:02:17 +08:00
|
|
|
|
func DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ... string) {
|
2019-01-16 22:34:22 +08:00
|
|
|
|
defaultCron.DelayAddTimes(delay, pattern, times, job, name...)
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 09:06:29 +08:00
|
|
|
|
// 检索指定名称的定时任务
|
2018-10-25 10:08:08 +08:00
|
|
|
|
func Search(name string) *Entry {
|
|
|
|
|
return defaultCron.Search(name)
|
2018-10-24 09:06:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据指定名称删除定时任务
|
|
|
|
|
func Remove(name string) {
|
2018-10-25 10:08:08 +08:00
|
|
|
|
defaultCron.Remove(name)
|
2018-10-22 16:53:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 17:46:04 +08:00
|
|
|
|
// 获取所有已注册的定时任务数量
|
|
|
|
|
func Size() int {
|
|
|
|
|
return defaultCron.Size()
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 16:53:43 +08:00
|
|
|
|
// 获取所有已注册的定时任务项
|
2018-10-25 10:08:08 +08:00
|
|
|
|
func Entries() []*Entry {
|
|
|
|
|
return defaultCron.Entries()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 启动指定的定时任务
|
|
|
|
|
func Start(name string) {
|
|
|
|
|
defaultCron.Start(name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 停止指定的定时任务
|
|
|
|
|
func Stop(name string) {
|
|
|
|
|
defaultCron.Stop(name)
|
2018-10-22 16:53:43 +08:00
|
|
|
|
}
|