gcron

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 10 Imported by: 1

README

定时器

用法

// Seconds field, required
gcron.New(gcron.WithSeconds())

// Seconds field, optional
gcron.New(cron.WithParser(gcron.NewParser(
gcron.SecondOptional | gcron.Minute | gcron.Hour | gcron.Dom | gcron.Month | gcron.Dow | gcron.Descriptor,
)))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chain

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

Chain 是一个 JobWrappers 序列,用于装饰提交的任务。 cross-cutting behaviors like logging or synchronization.

func NewChain

func NewChain(c ...JobWrapper) Chain

NewChain 横切行为,如日志记录或同步。

func (Chain) Then

func (c Chain) Then(j Job) Job

Then 用链中的所有 JobWrapper 装饰指定的任务。

这个:

NewChain(m1, m2, m3).Then(job)

相当于:

m1(m2(m3(job)))

type ConstantDelaySchedule

type ConstantDelaySchedule struct {
	Delay time.Duration
}

ConstantDelaySchedule 表示一个简单的循环占空比,例如 “每 5 分钟”。 它不支持比每秒一次更频繁的任务。

func Every

func Every(duration time.Duration) ConstantDelaySchedule

Every 返回每个持续时间激活一次的 crontab 计划。 不支持小于一秒的延迟(将四舍五入到 1 秒)。 任何小于秒的字段都会被截断。

func (ConstantDelaySchedule) Next

func (schedule ConstantDelaySchedule) Next(t time.Time) time.Time

Next 返回下次应该的运行时间。

type Cron

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

Cron 跟踪任意数量的条目,调用调度指定的关联函数。 Cron 可以启动、停止,并且可以在运行时检查条目。

func New

func New(opts ...Option) *Cron

New 返回由给定选项修改的新 Cron 任务运行器。

可用设置

TimeZone
  描述: 解释时间表的时区
  默认: time.Local

Parser
  描述: Parser 将 cron 规范字符串转换为 cron.Schedules。
  默认: 接受此规范:https://en.wikipedia.org/wiki/Cron

Chain
  描述: 以自定义行为包装提交任务。
  默认: 恢复 panic 并将其记录到标准错误 stderr。

请参阅“cron.With*”以修改默认行为。

func (*Cron) AddFunc

func (c *Cron) AddFunc(spec string, cmd func()) (EntryID, error)

AddFunc 向 Cron 添加一个函数,按给定的时间表运行。 使用此 Cron 实例的时区作为默认值解析规范。 返回一个不透明的 ID,以后可以使用它来移除它。

func (*Cron) AddJob

func (c *Cron) AddJob(spec string, cmd Job) (EntryID, error)

AddJob 添加任务到 Cron 以按给定的时间表运行。 使用此 Cron 实例的时区作为默认值来解析规范。 返回一个 ID,可用于稍后将其删除。

func (*Cron) Entries

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

Entries 返回 cron 条目的快照。

func (*Cron) Entry

func (c *Cron) Entry(id EntryID) Entry

Entry 返回给定条目的快照,如果找不到,则返回 nil。

func (*Cron) Location

func (c *Cron) Location() *time.Location

Location 获取时区。

func (*Cron) Remove

func (c *Cron) Remove(id EntryID)

Remove 移除将要运行的条目。

func (*Cron) Run

func (c *Cron) Run()

Run 运行 cron 调度程序, 如果已经运行则无需操作。

func (*Cron) Schedule

func (c *Cron) Schedule(schedule Schedule, cmd Job) EntryID

Schedule 将任务添加至 Cron 按指定的时间表运行。 该任务使用配置的 Chain 进行包装。

func (*Cron) Start

func (c *Cron) Start()

Start 启动 cron 调度程序,如果已经启动则无需操作。

func (*Cron) Stop

func (c *Cron) Stop(ctx context.Context) context.Context

Stop 如果 cron 调度程序正在运行,则停止它; 否则它什么也不做。 传入一个上下文,以便调用者可以等待正在运行的作业完成。

type Entry

type Entry struct {
	// ID 此条目的 cron 分配的 ID,可用于查找快照或删除它。
	ID EntryID

	// Schedule 运行此任务的计划。
	Schedule Schedule

	// Next 任务将运行的时间,或者如果 Cron 尚未启动或此条目的计划无法满足,则为零时间。
	Next time.Time

	// Prev 上次运行此作业的时间,如果从未运行,则为零时间。
	Prev time.Time

	// WrappedJob 激活调度时要运行的任务。
	WrappedJob Job

	// Job 提交给 cron 的任务。
	Job Job
}

Entry 由一个时间表和在该时间表上执行的函数组成。

func (Entry) Valid

func (e Entry) Valid() bool

Valid 如果这不是零条目,则返回 true。

type EntryID

type EntryID int

EntryID 标识 Cron 实例中的条目。

type FuncJob

type FuncJob func()

FuncJob 是将 func() 转换为 cron.Job 的包装器

func (FuncJob) Run

func (f FuncJob) Run()

type Job

type Job interface {
	Run()
}

Job 提交 cron 任务的接口。

type JobWrapper

type JobWrapper func(Job) Job

JobWrapper 用一些行为装饰指定的 Job。

func DelayIfStillRunning

func DelayIfStillRunning() JobWrapper

DelayIfStillRunning 序列化作业,延迟后续运行,直到前一个完成。延迟超过一分钟后运行的作业会在信息中记录延迟。

func Recover

func Recover() JobWrapper

Recover 使用日志记录器,记录包装任务中的 panic。

func SkipIfStillRunning

func SkipIfStillRunning() JobWrapper

SkipIfStillRunning 如果先前的调用仍在运行,则跳过对 Job 的调用。它记录跳转到信息级别的给定记录器。

type Option

type Option func(*Cron)

Option 表示对 Cron 的默认行为的修改。

func WithChain

func WithChain(wrappers ...JobWrapper) Option

WithChain 指定要应用于添加到此 cron 的所有任务的包装器。 有关提供的包装器,请参阅此包中的 Chain* 函数。

func WithLocation

func WithLocation(loc *time.Location) Option

WithLocation 覆盖 cron 实例的时区。

func WithLogger

func WithLogger(logger *glog.Helper) Option

WithLogger 使用提供的日志记录器。

func WithParser

func WithParser(p ScheduleParser) Option

WithParser 覆盖用于解析任务计划的解析器。

func WithSeconds

func WithSeconds() Option

WithSeconds 覆盖用于解析任务计划的解析器以包含秒字段作为第一个字段。

type ParseOption

type ParseOption int

ParseOption 用于创建解析器的配置选项。 大多数选项指定应包含哪些字段,而其他选项则启用功能。 如果未包含字段,则解析器将采用默认值。 这些选项不会更改解析的顺序字段。

const (
	Second         ParseOption = 1 << iota // 秒,默认 0
	SecondOptional                         // 可选秒,默认0
	Minute                                 // 分,默认 0
	Hour                                   // 时,默认 0
	Dom                                    // 日,默认 *
	Month                                  // 月,默认 *
	Dow                                    // 周,默认 *
	DowOptional                            // 可选周,默认 *
	Descriptor                             // 允许使用 @monthly、@weekly 等描述符。
)

type Parser

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

Parser 可以配置的自定义解析器。

func NewParser

func NewParser(options ParseOption) Parser

NewParser 使用自定义选项创建解析器。

如果给出了多个 Optional,它会出现 panic,因为通常无法正确推断提供或缺少哪个可选。

例子

// 没有描述符的标准解析器
specParser := NewParser(Minute | Hour | Dom | Month | Dow)
sched, err := specParser.Parse("0 0 15 */3 *")

// 同上,只是不包括时间字段
specParser := NewParser(Dom | Month | Dow)
sched, err := specParser.Parse("15 */3 *")

// 同上,只是使 Dow 可选
specParser := NewParser(Dom | Month | DowOptional)
sched, err := specParser.Parse("15 */3")

func (Parser) Parse

func (p Parser) Parse(spec string) (Schedule, error)

Parse 返回代表指定规范的新 crontab 计划。 如果规范无效,则返回描述性错误。 它接受由 NewParser 配置的 crontab 规范和特性。

type Schedule

type Schedule interface {
	// Next 返回下一个激活时间,晚于给定时间。
	// Next 最初调用,然后每次运行作业时调用。
	Next(time.Time) time.Time
}

Schedule 描述一个任务的工作周期。

func ParseStandard

func ParseStandard(standardSpec string) (Schedule, error)

ParseStandard 返回代表指定的新 crontab 计划。 标准规范 (https://en.wikipedia.org/wiki/Cron)。 它需要 5 个条目,依次代表:分钟、小时、月中的某天、月和周中的某天。 如果规范无效,它会返回描述性错误。

它接受 - 标准 crontab 规范,例如 “* * * * ?” - 描述符,例如 “@midnight”、“@每 1 小时 30 分”

type ScheduleParser

type ScheduleParser interface {
	Parse(spec string) (Schedule, error)
}

ScheduleParser 返回 Schedule 的调度规范解析器的接口。

type SpecSchedule

type SpecSchedule struct {
	Second, Minute, Hour, Dom, Month, Dow uint64

	// 覆盖此计划的时区。
	Location *time.Location
}

SpecSchedule 根据传统的 crontab 规范指定占空比(到第二个粒度)。 它最初被计算并存储为位集。

func (*SpecSchedule) Next

func (s *SpecSchedule) 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
JackTT - Gopher 🇻🇳