cron

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cron 实现 Golem 的定时任务 (Cron Jobs) 调度系统。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	ID             string   `json:"id"`                         // 唯一标识符
	Name           string   `json:"name"`                       // 任务显示名称
	Enabled        bool     `json:"enabled"`                    // 是否处于启用状态
	Schedule       Schedule `json:"schedule"`                   // 调度计划
	Payload        Payload  `json:"payload"`                    // 待执行载荷
	State          JobState `json:"state"`                      // 运行状态
	CreatedAtMS    int64    `json:"created_at_ms"`              // 创建时间
	UpdatedAtMS    int64    `json:"updated_at_ms"`              // 更新时间
	DeleteAfterRun bool     `json:"delete_after_run,omitempty"` // 执行完成后是否自动从存储中删除
}

Job 表示一个完整的定时任务配置及其状态。

func NewJob

func NewJob(name string, schedule Schedule, payload Payload) *Job

NewJob 创建一个新的任务实例,并自动分配 ID 和时间戳。

func (*Job) ScheduleDescription

func (j *Job) ScheduleDescription() string

ScheduleDescription 返回任务调度计划的人类可读描述。

func (*Job) ShortID

func (j *Job) ShortID() string

ShortID 返回任务的简短 ID(前 8 位)。

type JobHandler

type JobHandler func(*Job) error

JobHandler 定义了定时任务触发时的处理函数。

type JobState

type JobState struct {
	NextRunAtMS *int64 `json:"next_run_at_ms,omitempty"` // 下次预定执行时间
	LastRunAtMS *int64 `json:"last_run_at_ms,omitempty"` // 最近一次执行时间
	LastStatus  string `json:"last_status,omitempty"`    // 最近执行状态 ("ok", "error")
	LastError   string `json:"last_error,omitempty"`     // 最近一次执行产生的错误消息
}

JobState 维护任务的实时运行状态。

type Payload

type Payload struct {
	Kind    string `json:"kind"`    // 载荷类型(如 "agent_turn")
	Message string `json:"message"` // 发送给 Agent 的指令内容
	Channel string `json:"channel"` // 目标回复通道名称
	ChatID  string `json:"chat_id"` // 目标聊天 ID
	Deliver bool   `json:"deliver"` // 是否直接交付结果而跳过 Agent 思考
}

Payload 描述定时任务触发时需要执行的具体操作。

type Schedule

type Schedule struct {
	Kind    string `json:"kind"`               // 调度类型:"at" (单次), "every" (间隔), "cron" (表达式)
	AtMS    *int64 `json:"at_ms,omitempty"`    // 单次执行的时间戳(毫秒)
	EveryMS *int64 `json:"every_ms,omitempty"` // 执行间隔时长(毫秒)
	Expr    string `json:"expr,omitempty"`     // 5 段式 Cron 表达式
}

Schedule 定义任务触发的计划规则。

type Service

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

Service 负责管理定时任务的生命周期,并使用轮询循环 (Ticker) 进行调度执行。

func NewService

func NewService(storePath string, handler JobHandler) *Service

NewService 创建并返回一个由指定文件路径支持的定时任务服务。

func (*Service) AddJob

func (s *Service) AddJob(name, message string, schedule Schedule, channel, chatID string, deliver bool) (*Job, error)

AddJob 创建并持久化一个新的定时任务。

func (*Service) EnableJob

func (s *Service) EnableJob(id string, enabled bool) (*Job, error)

EnableJob 启用或禁用指定的任务。

func (*Service) GetJob

func (s *Service) GetJob(id string) (*Job, bool)

GetJob 按 ID 获取任务详情。

func (*Service) ListJobs

func (s *Service) ListJobs(includeDisabled bool) []*Job

ListJobs 返回所有任务列表,可按创建时间排序。

func (*Service) RemoveJob

func (s *Service) RemoveJob(id string) error

RemoveJob 按 ID 删除指定的任务。

func (*Service) RunJob added in v0.2.1

func (s *Service) RunJob(id string) (*Job, error)

RunJob 立即运行指定的任务(忽略其预定的运行时间)。

func (*Service) Start

func (s *Service) Start() error

Start 从磁盘加载任务并启动调度轮询循环。

func (*Service) Status

func (s *Service) Status() map[string]any

Status 返回定时任务服务的运行摘要状态。

func (*Service) Stop

func (s *Service) Stop()

Stop 停止调度循环并优雅退出。

type Store

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

Store 将定时任务持久化为 JSON 文件。

func NewStore

func NewStore(path string) *Store

NewStore 创建一个由给定文件路径支持的存储。

func (*Store) All

func (s *Store) All() []*Job

All 返回所有任务的深拷贝。

func (*Store) Delete

func (s *Store) Delete(id string) bool

Delete 按 ID 删除任务。

func (*Store) Get

func (s *Store) Get(id string) (*Job, bool)

Get 返回具有给定 ID 的任务的深拷贝。

func (*Store) Load

func (s *Store) Load() error

Load 从磁盘读取任务。如果文件不存在,存储将为空。

func (*Store) Put

func (s *Store) Put(job *Job)

Put 存储任务的深拷贝。调用者可以继续修改原始任务, 而不会与 Save 或其他读取者竞争。

func (*Store) Save

func (s *Store) Save() error

Save 将所有任务写入磁盘。序列化在读锁下进行, 以便通过 Put 的并发修改不会与编码竞争。

Jump to

Keyboard shortcuts

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