gf/g/os/gcron/gcron.go

101 lines
2.8 KiB
Go
Raw Normal View History

2018-10-22 16:53:43 +08:00
// 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.
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-01-21 22:09:51 +08:00
"gitee.com/johng/gf/g/os/gtimer"
2019-01-18 22:02:17 +08:00
"math"
"time"
)
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管理对象
defaultCron = New()
2018-10-22 16:53:43 +08:00
)
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-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...)
}
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...)
}
// 检索指定名称的定时任务
func Search(name string) *Entry {
return defaultCron.Search(name)
}
// 根据指定名称删除定时任务
func Remove(name string) {
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
// 获取所有已注册的定时任务项
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
}