Documentation
¶
Index ¶
- Variables
- func JobIDFromContext(ctx context.Context) (string, bool)
- func JobOwnerIDFromContext(ctx context.Context) (string, bool)
- func WithJobContext(ctx context.Context, job *CronJob) context.Context
- type ContextJobHandler
- type CronAgentOption
- func WithCronJobTimeout(timeout time.Duration) CronAgentOption
- func WithCronLocker(locker Locker) CronAgentOption
- func WithCronMemory(provider memory.MemoryProvider) CronAgentOption
- func WithCronMiddlewares(mw ...adk.TypedChatModelAgentMiddleware[*schema.AgenticMessage]) CronAgentOption
- func WithCronName(name string) CronAgentOption
- func WithCronStore(store Store) CronAgentOption
- func WithCronSystemPrompt(prompt string) CronAgentOption
- func WithDisableJobProcessing() CronAgentOption
- func WithExtraTools(tools ...tool.BaseTool) CronAgentOption
- func WithFileStore(path string) CronAgentOption
- func WithJobContextBuilder(builder JobContextBuilder) CronAgentOption
- func WithMaxJobs(max int) CronAgentOption
- func WithMaxJobsPerUser(max int) CronAgentOption
- func WithOnJobProcessed(fn func(job *CronJob, response string, err error)) CronAgentOption
- func WithOnJobProcessedContext(fn func(ctx context.Context, job *CronJob, response string, err error)) CronAgentOption
- func WithOnJobTriggered(fn func(job *CronJob)) CronAgentOption
- func WithOnJobTriggeredContext(fn func(ctx context.Context, job *CronJob)) CronAgentOption
- type CronAgentResult
- type CronJob
- type CronJobState
- type CronPayload
- type CronSchedule
- type CronService
- func (cs *CronService) AddJob(name string, schedule CronSchedule, message string, userID string) (*CronJob, error)
- func (cs *CronService) EnableJob(jobID string, enabled bool) *CronJobdeprecated
- func (cs *CronService) EnableJobForUser(jobID, userID string, enabled bool) (*CronJob, error)
- func (cs *CronService) ListJobs(includeDisabled bool) []CronJobdeprecated
- func (cs *CronService) ListJobsByUser(userID string, includeDisabled bool) ([]CronJob, error)
- func (cs *CronService) RemoveJob(jobID string) booldeprecated
- func (cs *CronService) RemoveJobForUser(jobID, userID string) error
- func (cs *CronService) SetJobContextBuilder(builder JobContextBuilder)
- func (cs *CronService) SetLockTimeouts(lock, unlock time.Duration)
- func (cs *CronService) SetLocker(locker Locker)
- func (cs *CronService) SetMaxJobs(max int)
- func (cs *CronService) SetMaxJobsPerUser(max int)
- func (cs *CronService) SetOnJob(handler JobHandler)
- func (cs *CronService) SetOnJobContext(handler ContextJobHandler)
- func (cs *CronService) Start() error
- func (cs *CronService) Status() map[string]any
- func (cs *CronService) Stop()
- func (cs *CronService) StopContext(ctx context.Context) error
- type FileStore
- type GormStore
- type JobContextBuilder
- type JobHandler
- type JobLifecycle
- type Locker
- type Store
Constants ¶
This section is empty.
Variables ¶
var ErrJobNotFound = errors.New("cron: job not found or not owned by caller")
ErrJobNotFound 表示任务不存在,或任务存在但不属于当前调用者。 两种情况返回同一个错误,避免向未授权调用者泄漏任务是否存在。
Functions ¶
func JobIDFromContext ¶ added in v0.3.3
JobIDFromContext 读取任务 ID。
func JobOwnerIDFromContext ¶ added in v0.3.3
JobOwnerIDFromContext 读取任务归属者 ID。
Types ¶
type ContextJobHandler ¶ added in v0.3.3
ContextJobHandler 带任务上下文的触发处理器。 ctx 由 JobContextBuilder 构建(默认注入任务 OwnerID 与 JobID)。
type CronAgentOption ¶ added in v0.2.0
type CronAgentOption func(*cronConfig)
CronAgentOption 配置选项
func WithCronJobTimeout ¶ added in v0.3.0
func WithCronJobTimeout(timeout time.Duration) CronAgentOption
WithCronJobTimeout caps the model call used to process triggered jobs.
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.TypedChatModelAgentMiddleware[*schema.AgenticMessage]) 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 WithDisableJobProcessing ¶ added in v0.3.3
func WithDisableJobProcessing() CronAgentOption
WithDisableJobProcessing 显式关闭任务触发后的模型处理。 关闭后任务触发只执行 OnTriggered/OnProcessed 回调(response 为空), 不会调用模型。不要通过“是否设置回调”来推断是否需要模型处理。
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 WithJobContextBuilder ¶ added in v0.3.3
func WithJobContextBuilder(builder JobContextBuilder) CronAgentOption
WithJobContextBuilder 设置任务执行 context 构建器, 用于把 trace、超时或租户信息带进任务回调与模型调用。
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 设置任务被模型处理完成后的回调(无论成功或失败)。 模型处理始终执行;如需关闭,请显式使用 WithDisableJobProcessing。
func WithOnJobProcessedContext ¶ added in v0.3.3
func WithOnJobProcessedContext(fn func(ctx context.Context, job *CronJob, response string, err error)) CronAgentOption
WithOnJobProcessedContext 设置带任务上下文的处理完成回调
func WithOnJobTriggered ¶ added in v0.2.0
func WithOnJobTriggered(fn func(job *CronJob)) CronAgentOption
WithOnJobTriggered 设置任务触发回调(观测用,不拦截模型处理)。 回调在任务触发后、模型调用前执行。
func WithOnJobTriggeredContext ¶ added in v0.3.3
func WithOnJobTriggeredContext(fn func(ctx context.Context, job *CronJob)) CronAgentOption
WithOnJobTriggeredContext 设置带任务上下文的触发回调。 ctx 由 JobContextBuilder 构建,至少携带任务 OwnerID 与 JobID。
type CronAgentResult ¶ added in v0.2.0
type CronAgentResult struct {
Agent adk.TypedAgent[*schema.AgenticMessage]
Service *CronService
}
CronAgentResult 定时任务 Agent 创建结果
func NewCronAgent ¶ added in v0.2.0
func NewCronAgent(ctx context.Context, cm model.AgenticModel, 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 表示一个定时任务
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
deprecated
func (cs *CronService) EnableJob(jobID string, enabled bool) *CronJob
EnableJob 启用/禁用任务(不校验归属)。
管理员接口:仅供后台管理/运维场景使用;面向普通用户的调用请使用 EnableJobForUser。
Deprecated: 该方法无法验证调用者身份,将在下一个主版本移除。
func (*CronService) EnableJobForUser ¶ added in v0.3.3
func (cs *CronService) EnableJobForUser(jobID, userID string, enabled bool) (*CronJob, error)
EnableJobForUser 启用/禁用 userID 拥有的任务。 userID 为空、任务不存在或任务属于其他用户时返回 ErrJobNotFound; 状态持久化失败时返回错误。
func (*CronService) ListJobs
deprecated
func (cs *CronService) ListJobs(includeDisabled bool) []CronJob
ListJobs 列出所有任务(不校验归属)。
管理员接口:仅供后台管理/运维场景使用;面向普通用户的调用请使用 ListJobsByUser。
Deprecated: 该方法会把所有用户的任务返回给调用方,将在下一个主版本移除。
func (*CronService) ListJobsByUser ¶ added in v0.3.3
func (cs *CronService) ListJobsByUser(userID string, includeDisabled bool) ([]CronJob, error)
ListJobsByUser 列出 userID 拥有的任务。userID 为空时返回错误, 不会退化为返回全量任务。
func (*CronService) RemoveJob
deprecated
func (cs *CronService) RemoveJob(jobID string) bool
RemoveJob 删除任务(不校验归属)。
管理员接口:仅供后台管理/运维场景使用;面向普通用户的调用请使用 RemoveJobForUser,避免横向越权删除其他用户的任务。
Deprecated: 该方法无法验证调用者身份,将在下一个主版本移除。
func (*CronService) RemoveJobForUser ¶ added in v0.3.3
func (cs *CronService) RemoveJobForUser(jobID, userID string) error
RemoveJobForUser 删除 userID 拥有的任务。 userID 为空、任务不存在或任务属于其他用户时都返回 ErrJobNotFound。
func (*CronService) SetJobContextBuilder ¶ added in v0.3.3
func (cs *CronService) SetJobContextBuilder(builder JobContextBuilder)
SetJobContextBuilder 设置任务执行 context 构建器。 builder 返回的 context 会注入任务 OwnerID 与 JobID 后传给任务回调。
func (*CronService) SetLockTimeouts ¶ added in v0.3.3
func (cs *CronService) SetLockTimeouts(lock, unlock time.Duration)
SetLockTimeouts 设置获取/释放分布式锁的超时。 两者独立计时:任务执行超过 lockTimeout 后,解锁仍使用新的有效 context。
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) SetOnJobContext ¶ added in v0.3.3
func (cs *CronService) SetOnJobContext(handler ContextJobHandler)
SetOnJobContext 设置带任务上下文的触发回调
func (*CronService) StopContext ¶ added in v0.3.3
func (cs *CronService) StopContext(ctx context.Context) error
StopContext 停止调度循环,并最多等待 ctx 到期让正在执行的任务退出。 返回等待超时错误时,任务回调可能仍在后台执行。
type FileStore ¶
type FileStore struct {
// contains filtered or unexported fields
}
func NewFileStoreWithError ¶ added in v0.3.3
NewFileStoreWithError 创建文件存储,加载已有数据失败时返回错误。 文件不存在视为首次运行,返回空 store 与 nil 错误。
type GormStore ¶
type GormStore struct {
// contains filtered or unexported fields
}
GormStore 基于 GORM 的数据库存储实现 支持 MySQL、PostgreSQL、SQLite
type JobContextBuilder ¶ added in v0.3.3
JobContextBuilder 为单次任务执行构建 context。 返回 nil 时回退到默认 context。builder 不应修改任务本身。
type JobHandler ¶
JobHandler 任务触发回调(兼容旧签名)。 新代码建议使用 ContextJobHandler,以便关联任务上下文与 trace。
type JobLifecycle ¶ added in v0.3.3
type JobLifecycle struct {
// OnTriggered 在任务触发后、模型处理前调用。
OnTriggered func(ctx context.Context, job *CronJob)
// OnProcessed 在模型处理结束后调用(无论成功或失败)。
OnProcessed func(ctx context.Context, job *CronJob, response string, err error)
}
JobLifecycle 描述任务触发与处理完成两个阶段的观测回调。 两个回调都只用于观测(日志、埋点、通知),不影响模型处理是否执行; 如需禁用模型处理,请显式使用 WithDisableJobProcessing。
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 分布式锁接口,便于后续扩展