定时cron类

package
v0.0.0-...-2910145 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

包gcron实现了cron模式解析器和任务执行器。

Index

Constants

View Source
const (
	StatusReady   = 定时类.StatusReady
	StatusRunning = 定时类.StatusRunning
	StatusStopped = 定时类.StatusStopped
	StatusClosed  = 定时类.StatusClosed
)

Variables

This section is empty.

Functions

func DelayAdd

func DelayAdd(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string)

DelayAdd 在 `delay` 时间后向默认的 cron 对象添加一个定时任务。

func DelayAddOnce

func DelayAddOnce(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string)

DelayAddOnce在`delay`时间后向默认的cron对象添加一个定时任务。 这个定时任务只能运行一次。

func DelayAddSingleton

func DelayAddSingleton(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string)

DelayAddSingleton 在`delay`时间后向默认的cron对象添加一个单例定时任务。

func DelayAddTimes

func DelayAddTimes(ctx context.Context, delay time.Duration, pattern string, times int, job JobFunc, name ...string)

DelayAddTimes 在`delay`时间后向默认的cron对象添加一个定时任务。 这个定时任务可以运行指定次数。

func GetLogger

func GetLogger() 日志类.ILogger

GetLogger 返回cron中的日志器。

func Remove

func Remove(name string)

Remove 删除名为 `name` 的已计划任务。

func SetLogger

func SetLogger(logger 日志类.ILogger)

SetLogger 用于设置cron的日志记录器。

func Size

func Size() int

Size 返回默认Cron定时任务的数量。

func Start

func Start(name ...string)

Start 开始运行指定的定时任务,名为 `name`。 如果未指定 `name`,则启动整个 cron。

func Stop

func Stop(name ...string)

Stop 停止运行指定的定时任务,该任务名为 `name`。 如果未指定 `name`,则停止整个 cron。

Types

type Cron

type Cron struct {
	// contains filtered or unexported fields
}

func New

func New() *Cron

New 函数返回一个使用默认设置的新 Cron 对象。

func (*Cron) Add

func (c *Cron) Add(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error)

Add 添加一个定时任务。 可以用唯一的 `name` 与定时任务进行绑定。 如果 `name` 已经被使用,则返回错误。

func (*Cron) AddEntry

func (c *Cron) AddEntry(ctx context.Context, pattern string, job JobFunc, times int, isSingleton bool, name ...string) (*Entry, error)

AddEntry 创建并返回一个新的 Entry 对象。

func (*Cron) AddOnce

func (c *Cron) AddOnce(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error)

AddOnce 添加一个仅能运行一次的定时任务。 可以为定时任务绑定一个唯一的 `name`。 如果 `name` 已被使用,则返回错误。

func (*Cron) AddSingleton

func (c *Cron) AddSingleton(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error)

AddSingleton 添加一个单例定时任务。 单例定时任务是指在同一时刻只能运行一个实例的任务。 可以使用唯一的 `name` 与定时任务绑定。 如果 `name` 已被使用,则返回错误。

func (*Cron) AddTimes

func (c *Cron) AddTimes(ctx context.Context, pattern string, times int, job JobFunc, name ...string) (*Entry, error)

AddTimes 添加一个可运行指定次数的定时任务。 可以使用唯一的 `name` 与定时任务关联绑定。 如果 `name` 已被使用,则返回错误。

func (*Cron) Close

func (c *Cron) Close()

Close 停止并关闭当前的cron任务。

func (*Cron) DelayAdd

func (c *Cron) DelayAdd(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string)

DelayAdd 在 `delay` 时间后添加一个定时任务。

func (*Cron) DelayAddEntry

func (c *Cron) DelayAddEntry(ctx context.Context, delay time.Duration, pattern string, job JobFunc, times int, isSingleton bool, name ...string)

DelayAddEntry 在 `delay` 时间后添加一个定时任务。

func (*Cron) DelayAddOnce

func (c *Cron) DelayAddOnce(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string)

DelayAddOnce在`delay`时间后添加一个定时任务。 这个定时任务只能运行一次。

func (*Cron) DelayAddSingleton

func (c *Cron) DelayAddSingleton(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string)

DelayAddSingleton 在`delay`时间后添加一个单例定时任务。

func (*Cron) DelayAddTimes

func (c *Cron) DelayAddTimes(ctx context.Context, delay time.Duration, pattern string, times int, job JobFunc, name ...string)

DelayAddTimes 在 `delay` 时间后添加一个定时任务。 此定时任务可以运行指定次数。

func (*Cron) Entries

func (c *Cron) Entries() []*Entry

Entries 返回所有按注册时间升序排列的定时任务切片。

func (*Cron) GetLogger

func (c *Cron) GetLogger() 日志类.ILogger

GetLogger 返回cron中的日志器。

func (*Cron) Remove

func (c *Cron) Remove(name string)

Remove 删除名为 `name` 的已计划任务。

func (*Cron) Search

func (c *Cron) Search(name string) *Entry

Search 返回具有指定`name`的已计划任务。 如果未找到,则返回nil。

func (*Cron) SetLogger

func (c *Cron) SetLogger(logger 日志类.ILogger)

SetLogger 用于设置cron的日志记录器。

func (*Cron) Size

func (c *Cron) Size() int

Size 返回定时任务的数量。

func (*Cron) Start

func (c *Cron) Start(name ...string)

Start 开始运行指定的定时任务,名为 `name`。 如果未指定 `name`,则启动整个 cron。

func (*Cron) Stop

func (c *Cron) Stop(name ...string)

Stop 停止运行指定的定时任务,该任务名为 `name`。 如果未指定 `name`,则停止整个 cron。

type Entry

type Entry struct {
	Name string    // Entry name.
	Job  JobFunc   `json:"-"` // 回调函数。
	Time time.Time // Registered time.
	// contains filtered or unexported fields
}

Entry 是定时任务的入口。

func Add

func Add(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error)

Add 向默认的 cron 对象添加一个定时任务。 可以用唯一的 `name` 与定时任务关联。 如果 `name` 已经被使用,则返回错误。

func AddOnce

func AddOnce(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error)

AddOnce 向默认的cron对象添加一个仅能执行一次的定时任务。 可以为定时任务绑定一个唯一的`name`标识。 如果`name`已被使用,则返回错误。

func AddSingleton

func AddSingleton(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error)

AddSingleton 添加单例定时任务到默认的cron对象。 单例定时任务是指在同一时间只能运行一个实例的任务。 可以通过唯一的`name`与定时任务关联绑定。 如果`name`已经被使用,则返回错误。

func AddTimes

func AddTimes(ctx context.Context, pattern string, times int, job JobFunc, name ...string) (*Entry, error)

AddTimes 向默认的 cron 对象添加一个可运行指定次数的定时任务。 可以为定时任务绑定一个唯一的 `name`。 如果 `name` 已经被使用,将会返回错误。

func Entries

func Entries() []*Entry

Entries 返回所有定时任务作为一个切片。

func Search(name string) *Entry

Search 返回具有指定名称 `name` 的已计划任务。 如果未找到,则返回 nil。

func (*Entry) Close

func (entry *Entry) Close()

Close 停止该任务并从 cron 中移除该条目。

func (*Entry) IsSingleton

func (entry *Entry) IsSingleton() bool

IsSingleton 返回该条目是否为单例定时任务。

func (*Entry) SetSingleton

func (entry *Entry) SetSingleton(enabled bool)

SetSingleton 设置入口以单例模式运行。

func (*Entry) SetStatus

func (entry *Entry) SetStatus(status int) int

SetStatus 设置条目的状态。

func (*Entry) SetTimes

func (entry *Entry) SetTimes(times int)

SetTimes 设置条目可以运行的时间。

func (*Entry) Start

func (entry *Entry) Start()

Start 启动运行入口。

func (*Entry) Status

func (entry *Entry) Status() int

Status 返回 entry 的状态。

func (*Entry) Stop

func (entry *Entry) Stop()

Stop 停止运行 entry。

type JobFunc

type JobFunc = 定时类.JobFunc

JobFunc 是 cron 中被定时调用的任务函数。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL