cron

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Start

func Start()

Start 启动全局调度器

func Stop

func Stop()

Stop 停止全局调度器

Types

type CronSchedule

type CronSchedule struct {
	Minute string // 分钟: "*" 或具体值如 "0,15,30"
	Hour   string // 小时: "*" 或具体值如 "8,12"
}

CronSchedule 简化 Cron 调度(仅分钟和小时)

func Cron

func Cron(minute, hour string) *CronSchedule

Cron 创建类 Cron 调度

func (*CronSchedule) Next

func (s *CronSchedule) Next(now time.Time) time.Time

Next 计算下次运行时间

type DailySchedule

type DailySchedule struct {
	Hour   int
	Minute int
}

DailySchedule 每日定时调度

func Daily

func Daily(hour, minute int) *DailySchedule

Daily 每日指定时间运行

func (*DailySchedule) Next

func (s *DailySchedule) Next(now time.Time) time.Time

Next 计算下次运行时间

type FullCronSchedule

type FullCronSchedule struct {
	Minute  string // 分钟: 0-59, "*", "*/n", "a-b", "a,b,c"
	Hour    string // 小时: 0-23, "*", "*/n", "a-b", "a,b,c"
	Day     string // 日: 1-31, "*", "*/n", "a-b", "a,b,c"
	Month   string // 月: 1-12, "*", "*/n", "a-b", "a,b,c"
	Weekday string // 星期: 0-6 (周日=0), "*", "*/n", "a-b", "a,b,c"
}

FullCronSchedule 完整 Cron 表达式调度 格式: "分钟 小时 日 月 星期" (5字段) 示例: "0 12 * * *" 每天12点

"0 0 1 * *" 每月1号凌晨
"0 9-17 * * 1-5" 周一到周五 9点到17点每小时

func ParseCron

func ParseCron(expr string) *FullCronSchedule

ParseCron 解析完整 Cron 表达式 格式: "分钟 小时 日 月 星期" 示例:

"0 12 * * *"       - 每天12:00
"*/15 * * * *"     - 每15分钟
"0 9-17 * * 1-5"   - 工作日9-17点每小时
"0 0 1 * *"        - 每月1号凌晨
"0 0 * * 0"        - 每周日凌晨

func (*FullCronSchedule) Next

func (s *FullCronSchedule) Next(now time.Time) time.Time

Next 计算下次运行时间

type IntervalSchedule

type IntervalSchedule struct {
	Interval time.Duration
}

IntervalSchedule 固定间隔调度

func Every

func Every(interval time.Duration) *IntervalSchedule

Every 每隔指定时间运行

func (*IntervalSchedule) Next

func (s *IntervalSchedule) Next(now time.Time) time.Time

Next 计算下次运行时间

type Schedule

type Schedule interface {
	Next(now time.Time) time.Time
}

Schedule 调度接口

type Scheduler

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

Scheduler 调度器

func GetScheduler

func GetScheduler() *Scheduler

GetScheduler 获取全局调度器

func NewScheduler

func NewScheduler() *Scheduler

NewScheduler 创建调度器

func (*Scheduler) AddTask

func (s *Scheduler) AddTask(name string, schedule Schedule, handler TaskHandler) *Task

AddTask 添加任务

func (*Scheduler) DisableTask

func (s *Scheduler) DisableTask(name string) error

DisableTask 禁用任务

func (*Scheduler) EnableTask

func (s *Scheduler) EnableTask(name string) error

EnableTask 启用任务

func (*Scheduler) GetTask

func (s *Scheduler) GetTask(name string) (*Task, error)

GetTask 获取任务

func (*Scheduler) ListTasks

func (s *Scheduler) ListTasks() []*Task

ListTasks 获取所有任务

func (*Scheduler) RemoveTask

func (s *Scheduler) RemoveTask(name string)

RemoveTask 移除任务

func (*Scheduler) RunTask

func (s *Scheduler) RunTask(name string) error

RunTask 立即运行任务

func (*Scheduler) Start

func (s *Scheduler) Start()

Start 启动调度器

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop 停止调度器

type Task

type Task struct {
	Name     string      // 任务名称
	Schedule Schedule    // 调度规则
	Handler  TaskHandler // 任务处理函数
	Enabled  bool        // 是否启用
	LastRun  time.Time   // 上次运行时间
	NextRun  time.Time   // 下次运行时间
	RunCount int         // 运行次数
}

Task 定时任务

func AddTask

func AddTask(name string, schedule Schedule, handler TaskHandler) *Task

AddTask 添加任务到全局调度器

type TaskHandler

type TaskHandler func(ctx context.Context) error

TaskHandler 任务处理函数

type WeeklySchedule

type WeeklySchedule struct {
	Day    time.Weekday
	Hour   int
	Minute int
}

WeeklySchedule 每周定时调度

func Weekly

func Weekly(day time.Weekday, hour, minute int) *WeeklySchedule

Weekly 每周指定时间运行

func (*WeeklySchedule) Next

func (s *WeeklySchedule) Next(now time.Time) time.Time

Next 计算下次运行时间

Jump to

Keyboard shortcuts

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