scheduler

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SessionReuse reuses the same session across job executions (default).
	SessionReuse = "reuse"
	// SessionNew creates a fresh session for each execution.
	SessionNew = "new"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatFunc

type ChatFunc func(ctx context.Context, sessionID, message, model string) <-chan runner.Event

ChatFunc streams runner events for heartbeat decision/execution prompts.

type Decision

type Decision struct {
	Action string `json:"action"`
	Reason string `json:"reason,omitempty"`
}

Decision is the gate-keeper response from the LLM.

type HeartbeatConfig

type HeartbeatConfig struct {
	File      string
	FastModel string
}

HeartbeatConfig holds heartbeat-specific settings.

type Job

type Job struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	Schedule    Schedule  `json:"schedule"`
	Message     string    `json:"message"`
	SessionMode string    `json:"session_mode"` // "reuse" (default) or "new"
	Enabled     bool      `json:"enabled"`
	CreatedAt   time.Time `json:"created_at"`
}

Job is the persisted job definition.

func (Job) SessionID

func (j Job) SessionID() string

SessionID returns the session identifier for this job execution. In "reuse" mode, the ID is stable across executions. In "new" mode, a timestamp suffix ensures each execution gets a fresh session.

type OnJobFunc

type OnJobFunc func(ctx context.Context, job Job)

OnJobFunc is called when a scheduled job fires.

type Schedule

type Schedule struct {
	Cron  string `json:"cron,omitempty"`  // "0 9 * * 1-5"
	Every string `json:"every,omitempty"` // "30m", "2h"
	At    string `json:"at,omitempty"`    // RFC3339: "2024-01-15T14:30:00+08:00"
}

Schedule defines when a job runs. Exactly one field must be set.

type SchedulerTool

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

SchedulerTool exposes scheduler management as an agent tool.

func NewTool

func NewTool(service *Service) *SchedulerTool

NewTool creates a SchedulerTool backed by the given service.

func (*SchedulerTool) Definition

func (t *SchedulerTool) Definition() toolspec.Definition

Definition returns the tool definition for the LLM.

func (*SchedulerTool) Execute

func (t *SchedulerTool) Execute(_ context.Context, args map[string]any) (string, error)

Execute runs the scheduler tool action.

type Service

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

Service manages scheduled jobs backed by gocron/v2 with database persistence.

func New

func New(db *sql.DB) (*Service, error)

New creates a scheduler service backed by the given database. Call Start to load persisted jobs and begin scheduling.

func NewFromPath

func NewFromPath(dbPath string) (*Service, error)

NewFromPath creates a scheduler service that opens its own SQLite database at the given path. The database is closed when Stop is called.

func (*Service) AddJob

func (s *Service) AddJob(name, message string, sched Schedule, sessionMode string) (Job, error)

AddJob creates, persists, and schedules a new job. sessionMode controls session reuse: "reuse" (default) or "new".

func (*Service) ListJobs

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

ListJobs returns all jobs.

func (*Service) RemoveJob

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

RemoveJob unschedules and removes a job.

func (*Service) ScheduleEvery

func (s *Service) ScheduleEvery(ctx context.Context, every string, fn TaskFunc) error

ScheduleEvery registers a non-persisted recurring task on the existing scheduler.

func (*Service) SetHeartbeat

func (s *Service) SetHeartbeat(cfg HeartbeatConfig, chat ChatFunc, notifier channel.Notifier)

SetHeartbeat configures the heartbeat on the scheduler service.

func (*Service) SetLegacyDataPath

func (s *Service) SetLegacyDataPath(path string)

SetLegacyDataPath sets the directory where the legacy jobs.json file may exist. If set, Start will attempt a one-time migration from file to DB.

func (*Service) SetOnJob

func (s *Service) SetOnJob(fn OnJobFunc)

SetOnJob sets the callback invoked when a job fires.

func (*Service) Start

func (s *Service) Start(ctx context.Context) error

Start loads persisted jobs and starts the scheduler.

func (*Service) StartEphemeral

func (s *Service) StartEphemeral(ctx context.Context) error

StartEphemeral starts the shared scheduler without loading persisted jobs. Use this when the scheduler is only needed for internal tasks such as heartbeat.

func (*Service) StartHeartbeat

func (s *Service) StartHeartbeat(ctx context.Context, every string) error

StartHeartbeat schedules the heartbeat poll on the shared scheduler.

func (*Service) Stop

func (s *Service) Stop() error

Stop shuts down the scheduler and closes the database if owned.

type TaskFunc

type TaskFunc func(ctx context.Context)

TaskFunc is a lightweight scheduled callback that is not persisted as a scheduled job.

Jump to

Keyboard shortcuts

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