cron

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CronCreateToolName = "cron_create"
	CronDeleteToolName = "cron_delete"
	CronListToolName   = "cron_list"
)
View Source
const (
	MaxJobs           = 50                      // 最大任务数限制
	DefaultMaxAgeDays = 7                       // 循环任务 7 天后自动过期
	DefaultMaxAgeMs   = 7 * 24 * 60 * 60 * 1000 // 7 days in ms
)

Variables

View Source
var DefaultJitterConfig = JitterConfig{
	RecurringFrac:    0.1,
	RecurringCapMs:   15 * 60 * 1000,
	OneShotMaxMs:     90 * 1000,
	OneShotFloorMs:   0,
	OneShotMinuteMod: 30,
}

Functions

func AddSessionTask

func AddSessionTask(task *CronTask) error

AddSessionTask adds a task to the session store

func BuildLoopSkillPrompt

func BuildLoopSkillPrompt() string

BuildLoopSkillPrompt returns the prompt for the loop skill

func CleanupExpiredTasks

func CleanupExpiredTasks(dir string) error

CleanupExpiredTasks removes expired tasks

func ClearSessionTasks

func ClearSessionTasks()

ClearSessionTasks clears all session tasks

func ComputeNextCronRun

func ComputeNextCronRun(fields *CronFields, from time.Time) *time.Time

ComputeNextCronRun computes the next Date strictly after `from` that matches the cron fields. Returns nil if no match within 366 days.

func CreateLoopTools

func CreateLoopTools(projectDir string) []interface {
	Info(ctx context.Context) (*schema.ToolInfo, error)
	InvokableRun(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (string, error)
}

CreateLoopTools creates all loop/cron tools

func CronToHuman

func CronToHuman(cron string) string

CronToHuman converts a cron expression to a human-readable string

func GenerateTaskID

func GenerateTaskID() (string, error)

GenerateTaskID generates a short task ID (8 hex chars)

func GetCronFilePath

func GetCronFilePath(dir string) string

GetCronFilePath returns the path to the cron tasks file

func HasTasks

func HasTasks(dir string) bool

HasTasks checks if there are any persistent tasks

func IntervalToCron

func IntervalToCron(interval string) string

IntervalToCron converts interval notation to cron expression Nm (N ≤ 59) → */N * * * * Nm (N ≥ 60) → 0 */H * * * (转为小时) Nh (N ≤ 23) → 0 */N * * * Nd → 0 0 */N * *

func MarkTasksFired

func MarkTasksFired(ids []string, firedAt int64, dir string) error

MarkTasksFired updates the LastFiredAt timestamp for tasks

func NextCronRunMs

func NextCronRunMs(cron string, fromMs int64) (int64, error)

NextCronRunMs computes the next fire time in epoch ms

func RemoveSessionTasks

func RemoveSessionTasks(ids []string) int

RemoveSessionTasks removes tasks by IDs from the session store

func RemoveTasks

func RemoveTasks(ids []string, dir string) error

RemoveTasks removes tasks by IDs

func WriteCronTasks

func WriteCronTasks(tasks []*CronTask, dir string) error

WriteCronTasks writes tasks to the persistent file

Types

type CronCreateInput

type CronCreateInput struct {
	Cron      string `json:"cron"`
	Prompt    string `json:"prompt"`
	Recurring *bool  `json:"recurring"`
	Durable   *bool  `json:"durable"`
}

CronCreateInput for cron_create tool

type CronCreateOutput

type CronCreateOutput struct {
	ID            string `json:"id"`
	HumanSchedule string `json:"human_schedule"`
	Recurring     bool   `json:"recurring"`
	Durable       bool   `json:"durable,omitempty"`
}

CronCreateOutput for cron_create tool

type CronCreateTool

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

CronCreateTool is the tool for creating scheduled tasks

func NewCronCreateTool

func NewCronCreateTool(projectDir string) *CronCreateTool

NewCronCreateTool creates a new CronCreateTool

func (*CronCreateTool) Info

func (*CronCreateTool) InvokableRun

func (t *CronCreateTool) InvokableRun(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (string, error)

InvokableRun executes the tool

func (*CronCreateTool) ValidateInput

func (t *CronCreateTool) ValidateInput(ctx context.Context, argumentsInJSON string) *ValidationResult

ValidateInput validates the input before execution

type CronDeleteInput

type CronDeleteInput struct {
	ID string `json:"id"`
}

CronDeleteInput for cron_delete tool

type CronDeleteOutput

type CronDeleteOutput struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
}

CronDeleteOutput for cron_delete tool

type CronDeleteTool

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

CronDeleteTool is the tool for deleting scheduled tasks

func NewCronDeleteTool

func NewCronDeleteTool(projectDir string) *CronDeleteTool

NewCronDeleteTool creates a new CronDeleteTool

func (*CronDeleteTool) Info

func (*CronDeleteTool) InvokableRun

func (t *CronDeleteTool) InvokableRun(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (string, error)

InvokableRun executes the tool

type CronFields

type CronFields struct {
	Minute     []int
	Hour       []int
	DayOfMonth []int
	Month      []int
	DayOfWeek  []int
}

CronFields represents expanded cron expression fields

func ParseCronExpression

func ParseCronExpression(expr string) *CronFields

ParseCronExpression parses a 5-field cron expression into expanded number arrays. Returns nil if invalid or unsupported syntax. Format: "M H DoM Mon DoW" (minute hour day-of-month month day-of-week)

type CronFile

type CronFile struct {
	Tasks []CronTask `json:"tasks"`
}

CronFile represents the on-disk format for persistent tasks

type CronListOutput

type CronListOutput struct {
	Tasks []CronTaskInfo `json:"tasks"`
}

CronListOutput for cron_list tool

type CronListTool

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

CronListTool is the tool for listing scheduled tasks

func NewCronListTool

func NewCronListTool(projectDir string) *CronListTool

NewCronListTool creates a new CronListTool

func (*CronListTool) Info

func (t *CronListTool) Info(ctx context.Context) (*schema.ToolInfo, error)

func (*CronListTool) InvokableRun

func (t *CronListTool) InvokableRun(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (string, error)

InvokableRun executes the tool

type CronTask

type CronTask struct {
	ID          string `json:"id"`
	Cron        string `json:"cron"`                    // 5-field cron expression
	Prompt      string `json:"prompt"`                  // Prompt to execute when task fires
	CreatedAt   int64  `json:"created_at"`              // Epoch ms when created
	LastFiredAt *int64 `json:"last_fired_at,omitempty"` // Epoch ms of most recent fire
	Recurring   bool   `json:"recurring"`               // Whether task reschedules after firing
	Permanent   bool   `json:"permanent,omitempty"`     // Exempt from auto-expiry (system use)
	Durable     bool   `json:"-"`                       // Runtime-only: false = session-scoped
	AgentID     string `json:"agent_id,omitempty"`      // Teammate agent ID (runtime-only)
}

CronTask represents a scheduled cron task

func AddTask

func AddTask(cron, prompt string, recurring, durable bool, agentID string) (*CronTask, error)

AddTask adds a new task

func FindExpiredTasks

func FindExpiredTasks(tasks []*CronTask) []*CronTask

FindExpiredTasks finds tasks that have exceeded max age

func GetSessionTasks

func GetSessionTasks() []*CronTask

GetSessionTasks returns all session tasks

func ListAllTasks

func ListAllTasks(dir string) []*CronTask

ListAllTasks returns all tasks (both file-based and session)

func ReadCronTasks

func ReadCronTasks(dir string) ([]*CronTask, error)

ReadCronTasks reads tasks from the persistent file

type CronTaskInfo

type CronTaskInfo struct {
	ID            string `json:"id"`
	Cron          string `json:"cron"`
	HumanSchedule string `json:"human_schedule"`
	Prompt        string `json:"prompt"`
	Recurring     bool   `json:"recurring"`
	Durable       bool   `json:"durable"`
	CreatedAt     int64  `json:"created_at"`
	NextFireAt    *int64 `json:"next_fire_at,omitempty"`
	LastFiredAt   *int64 `json:"last_fired_at,omitempty"`
}

CronTaskInfo represents a task in list output

type FieldRange

type FieldRange struct {
	Min int
	Max int
}

FieldRange represents min/max values for a cron field

type JitterConfig

type JitterConfig struct {
	RecurringFrac    float64 // Fraction of interval for recurring task delay
	RecurringCapMs   int64   // Max delay for recurring tasks
	OneShotMaxMs     int64   // Max early fire for one-shot tasks
	OneShotFloorMs   int64   // Min early fire for one-shot tasks
	OneShotMinuteMod int     // Minute mod gate for one-shot jitter
}

JitterConfig holds jitter configuration for task firing

type Scheduler

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

Scheduler manages cron task execution

func GetScheduler

func GetScheduler() *Scheduler

GetScheduler returns the default scheduler instance

func NewScheduler

func NewScheduler(projectDir string) *Scheduler

NewScheduler creates a new scheduler

func (*Scheduler) IsRunning

func (s *Scheduler) IsRunning() bool

IsRunning returns whether the scheduler is running

func (*Scheduler) SetHandler

func (s *Scheduler) SetHandler(handler TaskHandler)

SetHandler sets the task handler for when tasks fire

func (*Scheduler) Start

func (s *Scheduler) Start()

Start starts the scheduler

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop stops the scheduler

type TaskFiredFunc

type TaskFiredFunc func(taskID string, prompt string)

TaskFiredFunc is a function adapter for TaskHandler

func (TaskFiredFunc) OnTaskError

func (f TaskFiredFunc) OnTaskError(taskID string, err error)

OnTaskError implements TaskHandler

func (TaskFiredFunc) OnTaskFired

func (f TaskFiredFunc) OnTaskFired(taskID string, prompt string)

OnTaskFired implements TaskHandler

type TaskHandler

type TaskHandler interface {
	// OnTaskFired is called when a task fires
	OnTaskFired(taskID string, prompt string)
	// OnTaskError is called when a task execution fails
	OnTaskError(taskID string, err error)
}

TaskHandler handles task firing events

type ValidationResult

type ValidationResult struct {
	Valid     bool
	Message   string
	ErrorCode int
}

ValidationResult represents input validation result

Jump to

Keyboard shortcuts

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