cron_agent

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cron_agent 提供开箱即用的定时任务 Agent。

CronAgent 是一个预配置的 Agent,内置了 cron 工具,可以直接用于管理定时任务。 它封装了 CronService 的生命周期管理(Start/Stop),提供简单的创建和使用接口。

任务触发时,默认会将 job.Payload.Message 作为用户消息回送给 Agent 处理, 使得 Agent 可以在任务触发时执行更多操作(如创建新的定时任务)。 可通过 WithOnJobTriggered 覆盖此默认行为。

安全机制:

  • 单用户任务数上限(默认 10),防止单用户占用过多资源
  • 系统提示词禁止创建嵌套周期性任务

基本用法:

cronAgent, err := cron_agent.New(ctx, cm,
    cron_agent.WithFileStore("/path/to/cron_jobs.json"),
)
cronAgent.Start()
defer cronAgent.Stop()
resp, err := cronAgent.Generate(ctx, messages)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CronAgent

type CronAgent struct {
	*agent.Agent
	// contains filtered or unexported fields
}

CronAgent 定时任务 Agent

func New

New 创建定时任务 Agent

必须通过 WithFileStore、WithGormStore 或 WithStore 指定存储方式。 创建后需调用 Start() 启动调度循环,Stop() 停止。

默认行为:任务触发时,将 message 回送给 Agent 处理(支持嵌套任务创建)。 可通过 WithOnJobTriggered 覆盖为自定义处理逻辑。

func (*CronAgent) Service

func (ca *CronAgent) Service() *cronPkg.CronService

Service 返回内部的 CronService,用于直接操作

func (*CronAgent) Start

func (ca *CronAgent) Start() error

Start 启动定时调度服务

func (*CronAgent) Stop

func (ca *CronAgent) Stop()

Stop 停止定时调度服务

type Option

type Option func(*config)

Option 配置选项

func WithAgentOptions

func WithAgentOptions(opts ...agent.Option) Option

WithAgentOptions 设置 Agent 的附加选项(如 WithName、WithMemoryManager 等)

func WithExtraTools

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

WithExtraTools 添加额外的工具

func WithFileStore

func WithFileStore(path string) Option

WithFileStore 使用文件存储

func WithGormStore

func WithGormStore(db *gorm.DB) Option

WithGormStore 使用 GORM 数据库存储(支持 MySQL、PostgreSQL、SQLite)

func WithMaxJobs

func WithMaxJobs(max int) Option

WithMaxJobs 设置最大任务总数限制。默认不限制。

func WithMaxJobsPerUser

func WithMaxJobsPerUser(max int) Option

WithMaxJobsPerUser 设置单用户最大任务数量限制。默认 10。

func WithOnJobProcessed

func WithOnJobProcessed(fn func(job *cronPkg.CronJob, response string, err error)) Option

WithOnJobProcessed 设置任务被 Agent 自动处理后的回调。 仅在使用默认自动处理(未设置 WithOnJobTriggered)时生效。 可用于记录日志、发送通知等。

func WithOnJobTriggered

func WithOnJobTriggered(fn func(job *cronPkg.CronJob)) Option

WithOnJobTriggered 设置自定义的任务触发回调。 设置后将覆盖默认的自动回送 Agent 处理行为。

func WithStore

func WithStore(store cronPkg.Store) Option

WithStore 使用自定义存储实现

Jump to

Keyboard shortcuts

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