gf/os/gcron/gcron.go

132 lines
4.2 KiB
Go
Raw Normal View History

// 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,
// 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
)
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
)
// 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
}
// 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
}
// 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
}
// 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
}
// 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...)
}
// 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
}
// 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...)
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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)
}
// 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()
}
// Start starts running the specified timed task named <name>.
func Start(name string) {
2019-06-19 09:06:52 +08:00
defaultCron.Start(name)
}
// Stop stops running the specified timed task named <name>.
func Stop(name string) {
2019-06-19 09:06:52 +08:00
defaultCron.Stop(name)
2018-10-22 16:53:43 +08:00
}