cron

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 11 Imported by: 1

README

framework-cron

Go framework package for cron.

Installation

go get github.com/go-anyway/framework-cron@v1.0.0

Usage

See documentation for usage examples.

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Enabled     bool   `yaml:"enabled" env:"CRON_ENABLED" default:"true"`
	Location    string `yaml:"location" env:"CRON_LOCATION" default:"Local"`
	EnableTrace bool   `yaml:"enable_trace" env:"CRON_ENABLE_TRACE" default:"true"`
}

Config Cron 配置结构体(用于从配置文件创建)

func (*Config) ToOptions

func (c *Config) ToOptions() (*Options, error)

ToOptions 转换为 Options

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证 Cron 配置

type NamedTask

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

NamedTask 命名任务

func NewNamedTask

func NewNamedTask(name string, fn TaskFunc) *NamedTask

NewNamedTask 创建命名任务

func (*NamedTask) Name

func (t *NamedTask) Name() string

Name 返回任务名称

func (*NamedTask) Run

func (t *NamedTask) Run(ctx context.Context) error

Run 执行任务

type Options

type Options struct {
	Location    *time.Location // 时区
	EnableTrace bool           // 是否启用追踪
}

Options Cron 配置选项(内部使用)

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions 返回默认配置选项

type Scheduler

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

Scheduler Cron 调度器

func NewScheduler

func NewScheduler(opts *Options) *Scheduler

NewScheduler 创建新的调度器

func (*Scheduler) AddTask

func (s *Scheduler) AddTask(spec string, task Task) (cron.EntryID, error)

AddTask 添加任务

func (*Scheduler) GetAllTasks

func (s *Scheduler) GetAllTasks() []*TaskInfo

GetAllTasks 获取所有任务信息

func (*Scheduler) GetTaskInfo

func (s *Scheduler) GetTaskInfo(id cron.EntryID) *TaskInfo

GetTaskInfo 获取任务信息

func (*Scheduler) RemoveTask

func (s *Scheduler) RemoveTask(id cron.EntryID)

RemoveTask 移除任务

func (*Scheduler) Start

func (s *Scheduler) Start()

Start 启动调度器

func (*Scheduler) Stop

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

Stop 停止调度器

type Task

type Task interface {
	// Name 返回任务名称
	Name() string
	// Run 执行任务
	Run(ctx context.Context) error
}

Task 定时任务接口

type TaskFunc

type TaskFunc func(ctx context.Context) error

TaskFunc 任务函数类型

func (TaskFunc) Name

func (f TaskFunc) Name() string

Name 返回任务名称

func (TaskFunc) Run

func (f TaskFunc) Run(ctx context.Context) error

Run 执行任务

type TaskInfo

type TaskInfo struct {
	ID         cron.EntryID
	Name       string
	Spec       string
	Task       Task
	LastRun    time.Time
	NextRun    time.Time
	RunCount   int64
	ErrorCount int64
	LastError  error
}

TaskInfo 任务信息

Jump to

Keyboard shortcuts

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