2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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 (
|
2021-10-30 15:36:10 +08:00
|
|
|
"context"
|
2019-06-19 09:06:52 +08:00
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-11-15 20:26:31 +08:00
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/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 (
|
2021-01-22 23:09:42 +08:00
|
|
|
StatusReady = gtimer.StatusReady
|
|
|
|
StatusRunning = gtimer.StatusRunning
|
|
|
|
StatusStopped = gtimer.StatusStopped
|
|
|
|
StatusClosed = gtimer.StatusClosed
|
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
|
|
|
)
|
|
|
|
|
2021-08-20 14:09:15 +08:00
|
|
|
// SetLogger sets the logger for cron.
|
2022-06-28 15:47:16 +08:00
|
|
|
func SetLogger(logger glog.ILogger) {
|
2021-08-20 14:09:15 +08:00
|
|
|
defaultCron.SetLogger(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLogger returns the logger in the cron.
|
2022-06-28 15:47:16 +08:00
|
|
|
func GetLogger() glog.ILogger {
|
2021-08-20 14:09:15 +08:00
|
|
|
return defaultCron.GetLogger()
|
|
|
|
}
|
|
|
|
|
2019-05-05 22:57:13 +08:00
|
|
|
// Add adds a timed task to default cron object.
|
2021-08-20 11:52:22 +08:00
|
|
|
// A unique `name` can be bound with the timed task.
|
|
|
|
// It returns and error if the `name` is already used.
|
2021-10-30 15:36:10 +08:00
|
|
|
func Add(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error) {
|
|
|
|
return defaultCron.Add(ctx, 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.
|
2021-08-20 11:52:22 +08:00
|
|
|
// A unique `name` can be bound with the timed task.
|
|
|
|
// It returns and error if the `name` is already used.
|
2021-10-30 15:36:10 +08:00
|
|
|
func AddSingleton(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error) {
|
|
|
|
return defaultCron.AddSingleton(ctx, 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.
|
2021-08-20 11:52:22 +08:00
|
|
|
// A unique `name` can be bound with the timed task.
|
|
|
|
// It returns and error if the `name` is already used.
|
2021-10-30 15:36:10 +08:00
|
|
|
func AddOnce(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error) {
|
|
|
|
return defaultCron.AddOnce(ctx, 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.
|
2021-08-20 11:52:22 +08:00
|
|
|
// A unique `name` can be bound with the timed task.
|
|
|
|
// It returns and error if the `name` is already used.
|
2021-10-30 15:36:10 +08:00
|
|
|
func AddTimes(ctx context.Context, pattern string, times int, job JobFunc, name ...string) (*Entry, error) {
|
|
|
|
return defaultCron.AddTimes(ctx, pattern, times, job, name...)
|
2019-01-16 22:34:22 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:52:22 +08:00
|
|
|
// DelayAdd adds a timed task to default cron object after `delay` time.
|
2021-10-30 15:36:10 +08:00
|
|
|
func DelayAdd(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string) {
|
|
|
|
defaultCron.DelayAdd(ctx, delay, pattern, job, name...)
|
2018-12-30 11:08:07 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:52:22 +08:00
|
|
|
// DelayAddSingleton adds a singleton timed task after `delay` time to default cron object.
|
2021-10-30 15:36:10 +08:00
|
|
|
func DelayAddSingleton(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string) {
|
|
|
|
defaultCron.DelayAddSingleton(ctx, delay, pattern, job, name...)
|
2018-12-30 11:08:07 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:52:22 +08:00
|
|
|
// DelayAddOnce adds a timed task after `delay` time to default cron object.
|
2019-05-05 22:57:13 +08:00
|
|
|
// This timed task can be run only once.
|
2021-10-30 15:36:10 +08:00
|
|
|
func DelayAddOnce(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string) {
|
|
|
|
defaultCron.DelayAddOnce(ctx, delay, pattern, job, name...)
|
2018-11-05 10:29:58 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:52:22 +08:00
|
|
|
// DelayAddTimes adds a timed task after `delay` time to default cron object.
|
2019-05-05 22:57:13 +08:00
|
|
|
// This timed task can be run specified times.
|
2021-10-30 15:36:10 +08:00
|
|
|
func DelayAddTimes(ctx context.Context, delay time.Duration, pattern string, times int, job JobFunc, name ...string) {
|
|
|
|
defaultCron.DelayAddTimes(ctx, delay, pattern, times, job, name...)
|
2019-01-16 22:34:22 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:52:22 +08:00
|
|
|
// Search returns a scheduled task with the specified `name`.
|
2019-05-05 22:57:13 +08:00
|
|
|
// It returns nil if no found.
|
2021-05-26 09:55:33 +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
|
|
|
}
|
|
|
|
|
2021-08-20 11:52:22 +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.
|
2021-05-26 09:55:33 +08:00
|
|
|
func Entries() []*Entry {
|
2019-06-19 09:06:52 +08:00
|
|
|
return defaultCron.Entries()
|
2018-10-25 10:08:08 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:52:22 +08:00
|
|
|
// Start starts running the specified timed task named `name`.
|
2021-08-27 20:16:29 +08:00
|
|
|
// If no`name` specified, it starts the entire cron.
|
|
|
|
func Start(name ...string) {
|
|
|
|
defaultCron.Start(name...)
|
2018-10-25 10:08:08 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:52:22 +08:00
|
|
|
// Stop stops running the specified timed task named `name`.
|
2021-08-27 20:16:29 +08:00
|
|
|
// If no`name` specified, it stops the entire cron.
|
|
|
|
func Stop(name ...string) {
|
|
|
|
defaultCron.Stop(name...)
|
2018-10-22 16:53:43 +08:00
|
|
|
}
|