Help us shape the future of Go! Take the 2026 Go Developer Survey

cron

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cron 提供 Cron 表达式解析和调度功能。

支持标准 5 字段 Cron 表达式:分 时 日 月 周

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NextSchedule

func NextSchedule(s Schedule, from time.Time, n int) []time.Time

NextSchedule 返回最近的 N 个调度时间。

func RoundDuration

func RoundDuration(d time.Duration, precision time.Duration) time.Duration

RoundDuration 将时间向上取整到指定间隔。

func SortEntries

func SortEntries(entries []Entry)

SortEntries 按下次执行时间排序。

Types

type ConstantDelaySchedule

type ConstantDelaySchedule struct {
	Delay time.Duration
}

ConstantDelaySchedule 以固定时间间隔调度。

func Every

func Every(duration time.Duration) ConstantDelaySchedule

Every 返回一个以固定间隔调度的 Schedule。

func (ConstantDelaySchedule) Next

Next 返回下一个调度时间。

type DelaySchedule

type DelaySchedule struct {
	Delay time.Duration
}

DelaySchedule 在固定延迟后触发。

func NewDelaySchedule

func NewDelaySchedule(delay time.Duration) DelaySchedule

NewDelaySchedule 创建一个延迟调度器。

func (DelaySchedule) Next

func (d DelaySchedule) Next(t time.Time) time.Time

Next 返回当前时间加上延迟后的时间。

type Entry

type Entry struct {
	Schedule Schedule
	Next     time.Time
	Prev     time.Time
	Job      func()
}

Entry 表示一个 cron 条目。

type RandomDelaySchedule

type RandomDelaySchedule struct {
	Min, Max time.Duration
}

RandomDelaySchedule 在随机延迟后触发。

func NewRandomDelaySchedule

func NewRandomDelaySchedule(min, max time.Duration) RandomDelaySchedule

NewRandomDelaySchedule 创建一个随机延迟调度器。

func (RandomDelaySchedule) Next

Next 返回当前时间加上 [Min, Max] 范围内随机延迟后的时间。

type Schedule

type Schedule interface {
	Next(time.Time) time.Time
}

Schedule 调度接口。

func Parse

func Parse(spec string) (Schedule, error)

Parse 解析 cron 表达式并返回 Schedule。 支持标准 5 字段格式:分 时 日 月 周 也支持 6 字段格式:秒 分 时 日 月 周

type SpecSchedule

type SpecSchedule struct {
	Second, Minute, Hour, Dom, Month, Dow uint64
	Location                              *time.Location
}

SpecSchedule 根据 cron 表达式调度任务。

func (*SpecSchedule) Next

func (s *SpecSchedule) Next(t time.Time) time.Time

Next 返回下一个调度时间。

type WrapSchedule

type WrapSchedule struct {
	Schedule Schedule
	Delay    time.Duration
}

WrapSchedule 包装一个 Schedule,在每次触发后添加延迟。

func (WrapSchedule) Next

func (w WrapSchedule) Next(t time.Time) time.Time

Next 返回下一个调度时间。

Jump to

Keyboard shortcuts

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