cron

package
v0.35.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

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

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

⚠️ 与标准 cron 的方言差异([#186]):dow 支持 7≡Sunday;不支持 MON/JAN 名字、不支持 L/W/# 扩展;不可满足表达式(2 月 31 日)Next 返回零值 time.Time 而非报错——从系统 crontab 迁移时注意。

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 个调度时间。

n <= 0 返回空切片(不做任何调度计算)——负数此前会传给 make([]time.Time, 0, n) 直接 panic(makeslice: cap out of range)。

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。

⚠️ 语义三件套([#184](https://github.com/oekit/pkg/issues/184),此前零声明):

  • duration < 1s **静默钳到 1s**(本调度器秒级粒度,ConstantDelaySchedule 沿用 robfig 形态)——配 Every(500ms) 期望半秒节奏会得到 1s,调度密度 减半无任何信号;
  • 亚秒部分**截断**(1500ms ⇒ 1s);
  • Next 按传入时间的秒对齐,实际触发间隔 = delay 但不保证与「上次执行 完成」挂钩(固定时刻表,非完成后间隔;后者用 oekit/concurrency/loop 的 IntervalAfterCompletion)。

需要亚秒级高频节奏不该用 cron——直接 go-timer/loop。

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