cron

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CronAgentOption added in v0.2.0

type CronAgentOption func(*cronConfig)

CronAgentOption 配置选项

func WithCronLocker added in v0.2.0

func WithCronLocker(locker Locker) CronAgentOption

WithCronLocker 设置分布式锁

func WithCronMemory added in v0.2.0

func WithCronMemory(provider memory.MemoryProvider) CronAgentOption

WithCronMemory 设置 MemoryProvider

func WithCronMiddlewares added in v0.2.0

func WithCronMiddlewares(mw ...adk.ChatModelAgentMiddleware) CronAgentOption

WithCronMiddlewares 添加 Middleware

func WithCronName added in v0.2.0

func WithCronName(name string) CronAgentOption

WithCronName 设置 Agent 名称

func WithCronStore added in v0.2.0

func WithCronStore(store Store) CronAgentOption

WithCronStore 使用自定义存储实现

func WithCronSystemPrompt added in v0.2.0

func WithCronSystemPrompt(prompt string) CronAgentOption

WithCronSystemPrompt 设置系统提示词

func WithExtraTools added in v0.2.0

func WithExtraTools(tools ...tool.BaseTool) CronAgentOption

WithExtraTools 添加额外的工具

func WithFileStore added in v0.2.0

func WithFileStore(path string) CronAgentOption

WithFileStore 使用文件存储

func WithMaxJobs added in v0.2.0

func WithMaxJobs(max int) CronAgentOption

WithMaxJobs 设置最大任务总数限制

func WithMaxJobsPerUser added in v0.2.0

func WithMaxJobsPerUser(max int) CronAgentOption

WithMaxJobsPerUser 设置单用户最大任务数量限制

func WithOnJobProcessed added in v0.2.0

func WithOnJobProcessed(fn func(job *CronJob, response string, err error)) CronAgentOption

WithOnJobProcessed 设置任务被 Agent 自动处理后的回调

func WithOnJobTriggered added in v0.2.0

func WithOnJobTriggered(fn func(job *CronJob)) CronAgentOption

WithOnJobTriggered 设置自定义的任务触发回调

type CronAgentResult added in v0.2.0

type CronAgentResult struct {
	Agent   adk.Agent
	Service *CronService
}

CronAgentResult 定时任务 Agent 创建结果

func NewCronAgent added in v0.2.0

func NewCronAgent(ctx context.Context, cm model.ToolCallingChatModel, cronTools []tool.BaseTool, opts ...CronAgentOption) (*CronAgentResult, error)

NewCronAgent 创建定时任务 Agent tools 参数由调用方通过 cronTool.GetTools(service) 提供,避免循环导入

type CronJob

type CronJob struct {
	ID             string       `json:"id" gorm:"column:id;primaryKey;type:varchar(32)"`
	UserID         string       `json:"userId,omitempty" gorm:"column:user_id;type:varchar(64);index"`
	Name           string       `json:"name" gorm:"column:name;type:varchar(100)"`
	Enabled        bool         `json:"enabled" gorm:"column:enabled"`
	Schedule       CronSchedule `json:"schedule" gorm:"embedded"`
	Payload        CronPayload  `json:"payload" gorm:"embedded"`
	State          CronJobState `json:"state" gorm:"embedded"`
	CreatedAtMS    int64        `json:"createdAtMs" gorm:"column:created_at_ms"`
	UpdatedAtMS    int64        `json:"updatedAtMs" gorm:"column:updated_at_ms"`
	DeleteAfterRun bool         `json:"deleteAfterRun" gorm:"column:delete_after_run"`
}

CronJob 表示一个定时任务

func (CronJob) TableName

func (CronJob) TableName() string

TableName 指定 gorm 表名

type CronJobState

type CronJobState struct {
	NextRunAtMS *int64 `json:"nextRunAtMs,omitempty" gorm:"column:next_run_at_ms"`
	LastRunAtMS *int64 `json:"lastRunAtMs,omitempty" gorm:"column:last_run_at_ms"`
	LastStatus  string `json:"lastStatus,omitempty" gorm:"column:last_status;type:varchar(20)"`
	LastError   string `json:"lastError,omitempty" gorm:"column:last_error;type:text"`
}

CronJobState 定义任务的运行状态

type CronPayload

type CronPayload struct {
	Message string `json:"message" gorm:"column:message;type:text"`
}

CronPayload 定义任务触发时携带的信息

type CronSchedule

type CronSchedule struct {
	Kind    string `json:"kind" gorm:"column:kind;type:varchar(10);not null"`   // "at" | "every" | "cron"
	AtMS    *int64 `json:"atMs,omitempty" gorm:"column:at_ms"`                  // Kind=at 时的触发时间戳(毫秒)
	EveryMS *int64 `json:"everyMs,omitempty" gorm:"column:every_ms"`            // Kind=every 时的间隔(毫秒)
	Expr    string `json:"expr,omitempty" gorm:"column:expr;type:varchar(100)"` // Kind=cron 时的 cron 表达式
}

CronSchedule 定义任务的调度方式

type CronService

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

CronService 定时调度服务

func NewCronService

func NewCronService(store Store, onJob JobHandler) *CronService

NewCronService 创建定时调度服务

func (*CronService) AddJob

func (cs *CronService) AddJob(name string, schedule CronSchedule, message string, userID string) (*CronJob, error)

AddJob 添加定时任务

func (*CronService) EnableJob

func (cs *CronService) EnableJob(jobID string, enabled bool) *CronJob

EnableJob 启用/禁用任务

func (*CronService) ListJobs

func (cs *CronService) ListJobs(includeDisabled bool) []CronJob

ListJobs 列出任务

func (*CronService) RemoveJob

func (cs *CronService) RemoveJob(jobID string) bool

RemoveJob 删除任务

func (*CronService) SetLocker added in v0.0.16

func (cs *CronService) SetLocker(locker Locker)

SetLocker 设置分布式锁实现

func (*CronService) SetMaxJobs

func (cs *CronService) SetMaxJobs(max int)

SetMaxJobs 设置最大任务数量限制,0 表示不限制

func (*CronService) SetMaxJobsPerUser

func (cs *CronService) SetMaxJobsPerUser(max int)

SetMaxJobsPerUser 设置单用户最大任务数量限制,0 表示不限制

func (*CronService) SetOnJob

func (cs *CronService) SetOnJob(handler JobHandler)

SetOnJob 设置任务触发回调

func (*CronService) Start

func (cs *CronService) Start() error

Start 启动调度循环

func (*CronService) Status

func (cs *CronService) Status() map[string]any

Status 返回服务状态

func (*CronService) Stop

func (cs *CronService) Stop()

Stop 停止调度循环

type FileStore

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

func (*FileStore) Delete

func (s *FileStore) Delete(id string) error

Delete 删除任务

func (*FileStore) Get

func (s *FileStore) Get(id string) (*CronJob, error)

Get 根据 ID 获取任务

func (*FileStore) List

func (s *FileStore) List() ([]CronJob, error)

List 列出所有任务

func (*FileStore) Save

func (s *FileStore) Save(job *CronJob) error

Save 保存任务(新增或更新)

type GormStore

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

GormStore 基于 GORM 的数据库存储实现 支持 MySQL、PostgreSQL、SQLite

func (*GormStore) Delete

func (s *GormStore) Delete(id string) error

Delete 删除任务

func (*GormStore) Get

func (s *GormStore) Get(id string) (*CronJob, error)

Get 根据 ID 获取任务

func (*GormStore) List

func (s *GormStore) List() ([]CronJob, error)

List 列出所有任务

func (*GormStore) Save

func (s *GormStore) Save(job *CronJob) error

Save 保存任务(新增或更新)

type JobHandler

type JobHandler func(job *CronJob) (string, error)

JobHandler 任务触发回调

type Locker added in v0.0.16

type Locker interface {
	Lock(ctx context.Context, key string) error
	Unlock(ctx context.Context, key string) error
}

Locker 分布式锁接口,便于后续扩展

type Store

type Store interface {
	// List 列出所有任务
	List() ([]CronJob, error)

	// Get 根据 ID 获取任务
	Get(id string) (*CronJob, error)

	// Save 保存任务(新增或更新)
	Save(job *CronJob) error

	// Delete 根据 ID 删除任务
	Delete(id string) error
}

Store 定义定时任务的存储接口

func NewFileStore

func NewFileStore(path string) Store

NewFileStore 创建文件存储

func NewGormStore

func NewGormStore(db *gorm.DB) (Store, error)

NewGormStore 创建 GORM 数据库存储 自动建表(如不存在),支持 MySQL、PostgreSQL、SQLite

Jump to

Keyboard shortcuts

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