mirror of
https://gitee.com/johng/gf.git
synced 2024-12-04 05:07:44 +08:00
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
||
//
|
||
// 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://gitee.com/johng/gf.
|
||
|
||
// Package gcron implements a cron pattern parser and job runner.
|
||
//
|
||
// 定时任务.
|
||
package gcron
|
||
|
||
import "math"
|
||
|
||
const (
|
||
STATUS_READY = 0
|
||
STATUS_RUNNING = 1
|
||
STATUS_STOPPED = 2
|
||
STATUS_CLOSED = -1
|
||
|
||
gDEFAULT_TIMES = math.MaxInt32
|
||
)
|
||
|
||
var (
|
||
// 默认的cron管理对象
|
||
defaultCron = New()
|
||
)
|
||
|
||
// 添加执行方法,可以给定名字,以便于后续执行删除
|
||
func Add(pattern string, job func(), name ... string) (*Entry, error) {
|
||
return defaultCron.Add(pattern, job, name...)
|
||
}
|
||
|
||
// 添加单例运行定时任务
|
||
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...)
|
||
}
|
||
|
||
// 添加运行指定次数的定时任务
|
||
func AddTimes(pattern string, times int, job func(), name ... string) (*Entry, error) {
|
||
return defaultCron.AddTimes(pattern, times, job, name...)
|
||
}
|
||
|
||
// 延迟添加定时任务,delay参数单位为秒
|
||
func DelayAdd(delay int, pattern string, job func(), name ... string) {
|
||
defaultCron.DelayAdd(delay, pattern, job, name...)
|
||
}
|
||
|
||
// 延迟添加单例定时任务,delay参数单位为秒
|
||
func DelayAddSingleton(delay int, pattern string, job func(), name ... string) {
|
||
defaultCron.DelayAddSingleton(delay, pattern, job, name...)
|
||
}
|
||
|
||
// 延迟添加只运行一次的定时任务,delay参数单位为秒
|
||
func DelayAddOnce(delay int, pattern string, job func(), name ... string) {
|
||
defaultCron.DelayAddOnce(delay, pattern, job, name...)
|
||
}
|
||
|
||
// 延迟添加运行指定次数的定时任务,delay参数单位为秒
|
||
func DelayAddTimes(delay int, pattern string, times int, job func(), name ... string) {
|
||
defaultCron.DelayAddTimes(delay, pattern, times, job, name...)
|
||
}
|
||
|
||
// 检索指定名称的定时任务
|
||
func Search(name string) *Entry {
|
||
return defaultCron.Search(name)
|
||
}
|
||
|
||
// 根据指定名称删除定时任务
|
||
func Remove(name string) {
|
||
defaultCron.Remove(name)
|
||
}
|
||
|
||
// 获取所有已注册的定时任务数量
|
||
func Size() int {
|
||
return defaultCron.Size()
|
||
}
|
||
|
||
// 获取所有已注册的定时任务项
|
||
func Entries() []*Entry {
|
||
return defaultCron.Entries()
|
||
}
|
||
|
||
// 启动指定的定时任务
|
||
func Start(name string) {
|
||
defaultCron.Start(name)
|
||
}
|
||
|
||
// 停止指定的定时任务
|
||
func Stop(name string) {
|
||
defaultCron.Stop(name)
|
||
}
|