cron

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 8 Imported by: 0

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

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

Boot is a no-op for cron provider.

Boot Cron Provider 无启动逻辑(启动由 Start 调用触发)。

func (*Provider) DependsOn

func (p *Provider) DependsOn() []string

DependsOn returns the keys this provider depends on. Cron provider has no dependencies.

DependsOn 返回该 provider 依赖的 key。 Cron provider 无依赖。

func (*Provider) IsDefer

func (p *Provider) IsDefer() bool

IsDefer returns false, cron should be initialized immediately for service registration.

IsDefer 返回 false,Cron 应立即初始化以便任务注册。

func (*Provider) Name

func (p *Provider) Name() string

Name returns the provider name "cron".

Name 返回 Provider 名称 "cron"。

func (*Provider) Provides

func (p *Provider) Provides() []string

Provides returns the cron service contract key.

Provides 返回 Cron 服务契约键。

func (*Provider) Register

func (p *Provider) Register(c runtimecontract.Container) error

Register binds the cron service factory to the container. Core logic: Create Service with second-level parser, bind factory.

Register 将 Cron 服务工厂绑定到容器。 核心逻辑:创建支持秒级表达式的 Service、绑定工厂。

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

func (s *Service) Add(spec string, fn func(ctx context.Context) error) (int, error)

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

func (s *Service) AddNamed(name, spec string, fn func(ctx context.Context) error) (int, error)

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 任务的信息,包括调度表达式、上次/下次执行时间及执行状态。

func (*Service) Start

func (s *Service) Start()

Start begins the cron scheduler. Registered jobs will start executing according to their schedules.

Start 启动 Cron 调度器。 已注册的任务将按调度表达式开始执行。

func (*Service) Stop

func (s *Service) Stop() context.Context

Stop stops the cron scheduler and returns context for waiting on running jobs.

Stop 停止 Cron 调度器,返回 context 用于等待正在执行的任务完成。

Jump to

Keyboard shortcuts

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