Documentation
¶
Overview ¶
Package cron provides cron scheduling service for gorp framework. This file defines Prometheus metrics for cron job monitoring. Includes execution count, duration, in-progress count, and scheduled count.
Cron 调度服务包,提供 gorp 框架的定时任务调度能力。 本文件定义用于 cron 任务监控的 Prometheus 指标。 包括执行次数、耗时、当前执行数和已调度数。
Package cron provides cron scheduling service for gorp framework. Implements framework-level wrapper around robfig/cron with timeout control and Prometheus metrics. Supports second-level cron expressions with automatic panic recovery.
Cron 调度服务包,提供 gorp 框架的定时任务调度能力。 对 robfig/cron 做 framework 级封装,包含超时控制和 Prometheus 指标采集。 支持秒级 cron 表达式,自动 panic 恢复。
Eg:
// 注册 Provider
app.Register(cron.NewProvider())
// 添加定时任务
cronSvc := c.MustMake(runtimecontract.CronKey).(runtimecontract.Cron)
cronSvc.AddNamed("cleanup", "0 0 3 * * *", func(ctx context.Context) error {
return cleanupOldData(ctx)
})
cronSvc.Start()
Index ¶
- type CronJobMetrics
- type Provider
- type Service
- func (s *Service) Add(spec string, fn func(ctx context.Context) error) (int, error)
- func (s *Service) AddNamed(name, spec string, fn func(ctx context.Context) error) (int, error)
- func (s *Service) Jobs() []runtimecontract.CronJobEntry
- func (s *Service) Start()
- func (s *Service) Stop() context.Context
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CronJobMetrics ¶
type CronJobMetrics struct {
JobName string
}
CronJobMetrics 包装 cron 任务并收集指标。
中文说明: - jobName: 任务名称,用于标识不同的任务; - 记录任务执行次数、耗时、当前执行数; - 区分成功和失败的任务(status 标签)。
type Provider ¶
type Provider struct{}
Provider registers the cron service contract. Exposes runtimecontract.CronKey for unified task registration. Core logic: Create Service instance with second-level parser, bind to container.
Provider 注册 Cron 服务契约。 统一暴露 runtimecontract.CronKey,用于定时任务注册。 核心逻辑:创建支持秒级表达式的 Service 实例、绑定到容器。
func NewProvider ¶
func NewProvider() *Provider
NewProvider creates a new cron provider instance.
NewProvider 创建新的 Cron Provider 实例。
func (*Provider) Boot ¶
func (p *Provider) Boot(runtimecontract.Container) error
Boot is a no-op for cron provider.
Boot Cron Provider 无启动逻辑(启动由 Start 调用触发)。
func (*Provider) DependsOn ¶
DependsOn returns the keys this provider depends on. Cron provider has no dependencies.
DependsOn 返回该 provider 依赖的 key。 Cron provider 无依赖。
func (*Provider) IsDefer ¶
IsDefer returns false, cron should be initialized immediately for service registration.
IsDefer 返回 false,Cron 应立即初始化以便任务注册。
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service wraps robfig/cron with framework-level enhancements. Features: timeout control, Prometheus metrics, panic recovery, job introspection. Core logic: Encapsulate robfig/cron, add timeout and metrics hooks, track execution history.
Service 对 robfig/cron 做 framework 级封装。 特性:超时控制、Prometheus 指标采集、panic 恢复、任务内省。 核心逻辑:封装 robfig/cron,添加超时和指标 hooks,追踪执行历史。
func NewService ¶
func NewService() *Service
NewService creates a cron service with second-level expression support. Includes automatic panic recovery chain.
NewService 创建支持秒级表达式的 Cron 调度器。 包含自动 panic 恢复链。
func (*Service) Add ¶
Add registers a cron job without Prometheus metrics labels. Suitable for simple scheduled tasks. Core logic: Wrap function with 5-minute timeout, add to scheduler, track execution history.
Add 注册不带指标标签的定时任务。 适用于简单定时任务场景。 核心逻辑:用 5 分钟超时包装函数、添加到调度器、追踪执行历史。
func (*Service) AddNamed ¶
AddNamed registers a cron job with Prometheus metrics labels. Suitable for tasks requiring execution monitoring by name. Core logic: Wrap function with timeout, add metrics recording, add to scheduler, track execution history.
AddNamed 注册带 Prometheus 指标标签的定时任务。 适用于需要按任务名观测执行情况的场景。 核心逻辑:用超时包装函数、添加指标记录、添加到调度器、追踪执行历史。
func (*Service) Jobs ¶
func (s *Service) Jobs() []runtimecontract.CronJobEntry
Jobs returns information about all registered cron jobs, including schedule, last/next run time, and execution status.
Jobs 返回所有已注册 cron 任务的信息,包括调度表达式、上次/下次执行时间及执行状态。