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.
|
2018-10-22 16:53:43 +08:00
|
|
|
package gcron
|
|
|
|
|
2019-01-18 22:02:17 +08:00
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"math"
|
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/os/gtimer"
|
2019-01-18 22:02:17 +08:00
|
|
|
)
|
2018-10-25 10:08:08 +08:00
|
|
|
|
2019-01-16 22:34:22 +08:00
|
|
|
const (
|
2019-06-19 09:06:52 +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
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
gDEFAULT_TIMES = math.MaxInt32
|
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
|
|
|
)
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// SetLogPath sets the logging folder path for default cron object.
|
2019-03-19 13:58:18 +08:00
|
|
|
func SetLogPath(path string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
defaultCron.SetLogPath(path)
|
2019-03-19 13:58:18 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// GetLogPath returns the logging folder path of default cron object.
|
2019-03-19 13:58:18 +08:00
|
|
|
func GetLogPath() string {
|
2019-06-19 09:06:52 +08:00
|
|
|
return defaultCron.GetLogPath()
|
2019-03-19 13:58:18 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// SetLogLevel sets the logging level for default cron object.
|
2019-03-19 13:58:18 +08:00
|
|
|
func SetLogLevel(level int) {
|
2019-06-19 09:06:52 +08:00
|
|
|
defaultCron.SetLogLevel(level)
|
2019-03-19 13:58:18 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// GetLogLevel returns the logging level for default cron object.
|
2019-03-19 13:58:18 +08:00
|
|
|
func GetLogLevel() int {
|
2019-06-19 09:06:52 +08:00
|
|
|
return defaultCron.GetLogLevel()
|
2019-03-19 13:58:18 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// Add adds a timed task to default cron object.
|
|
|
|
// A unique <name> can be bound with the timed task.
|
|
|
|
// It returns and error if the <name> is already used.
|
2019-06-19 09:06:52 +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
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// 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.
|
|
|
|
// A unique <name> can be bound with the timed task.
|
|
|
|
// It returns and error if the <name> is already used.
|
2019-06-19 09:06:52 +08:00
|
|
|
func AddSingleton(pattern string, job func(), name ...string) (*Entry, error) {
|
|
|
|
return defaultCron.AddSingleton(pattern, job, name...)
|
2018-12-30 11:08:07 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// AddOnce adds a timed task which can be run only once, to default cron object.
|
|
|
|
// A unique <name> can be bound with the timed task.
|
|
|
|
// It returns and error if the <name> is already used.
|
2019-06-19 09:06:52 +08:00
|
|
|
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-05-05 22:57:13 +08:00
|
|
|
// AddTimes adds a timed task which can be run specified times, to default cron object.
|
|
|
|
// A unique <name> can be bound with the timed task.
|
|
|
|
// It returns and error if the <name> is already used.
|
2019-06-19 09:06:52 +08:00
|
|
|
func AddTimes(pattern string, times int, job func(), name ...string) (*Entry, error) {
|
|
|
|
return defaultCron.AddTimes(pattern, times, job, name...)
|
2019-01-16 22:34:22 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +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
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +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
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +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
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +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
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// Search returns a scheduled task with the specified <name>.
|
|
|
|
// It returns nil if no found.
|
2018-10-25 10:08:08 +08:00
|
|
|
func Search(name string) *Entry {
|
2019-06-19 09:06:52 +08:00
|
|
|
return defaultCron.Search(name)
|
2018-10-24 09:06:29 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// Remove deletes scheduled task which named <name>.
|
2018-10-24 09:06:29 +08:00
|
|
|
func Remove(name string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
defaultCron.Remove(name)
|
2018-10-22 16:53:43 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +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
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// Entries return all timed tasks as slice.
|
2018-10-25 10:08:08 +08:00
|
|
|
func Entries() []*Entry {
|
2019-06-19 09:06:52 +08:00
|
|
|
return defaultCron.Entries()
|
2018-10-25 10:08:08 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// Start starts running the specified timed task named <name>.
|
2018-10-25 10:08:08 +08:00
|
|
|
func Start(name string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
defaultCron.Start(name)
|
2018-10-25 10:08:08 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// Stop stops running the specified timed task named <name>.
|
2018-10-25 10:08:08 +08:00
|
|
|
func Stop(name string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
defaultCron.Stop(name)
|
2018-10-22 16:53:43 +08:00
|
|
|
}
|