cron

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildTools added in v0.7.0

func BuildTools(scheduler *Scheduler, defaultDeliverTo []string) []*agent.Tool

BuildTools creates tools for managing scheduled cron jobs.

Types

type AgentRunner

type AgentRunner interface {
	Run(ctx context.Context, sessionKey string, prompt string) (string, error)
}

AgentRunner is the interface for executing agent turns. This avoids import cycles -- wiring.go will provide the concrete implementation.

type ChannelSender

type ChannelSender interface {
	SendMessage(ctx context.Context, channel string, message string) error
}

ChannelSender sends a message to a specific channel. This avoids import cycles -- wiring.go will provide the concrete implementation.

type Delivery

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

Delivery handles dispatching job results to configured channels.

func NewDelivery

func NewDelivery(sender ChannelSender, typing TypingIndicator, logger *zap.SugaredLogger) *Delivery

NewDelivery creates a new Delivery instance. If sender is nil, delivery will be a no-op (results are only stored in history).

func (*Delivery) Deliver

func (d *Delivery) Deliver(ctx context.Context, result *JobResult, targets []string) error

Deliver sends a job result to the specified target channels.

func (*Delivery) DeliverStart

func (d *Delivery) DeliverStart(ctx context.Context, jobName string, targets []string)

DeliverStart sends a notification that a cron job has started execution.

func (*Delivery) StartTyping

func (d *Delivery) StartTyping(ctx context.Context, targets []string) func()

StartTyping starts a typing indicator on all target channels. The returned stop function ends all typing indicators. It is always non-nil.

type EntStore

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

EntStore implements Store using the Ent ORM client.

func NewEntStore

func NewEntStore(client *ent.Client) *EntStore

NewEntStore creates a new EntStore backed by the given Ent client.

func (*EntStore) Create

func (s *EntStore) Create(ctx context.Context, job Job) error

Create persists a new cron job.

func (*EntStore) Delete

func (s *EntStore) Delete(ctx context.Context, id string) error

Delete removes a cron job by ID.

func (*EntStore) Get

func (s *EntStore) Get(ctx context.Context, id string) (*Job, error)

Get retrieves a cron job by ID.

func (*EntStore) GetByName

func (s *EntStore) GetByName(ctx context.Context, name string) (*Job, error)

GetByName retrieves a cron job by its unique name.

func (*EntStore) List

func (s *EntStore) List(ctx context.Context) ([]Job, error)

List returns all cron jobs.

func (*EntStore) ListAllHistory

func (s *EntStore) ListAllHistory(ctx context.Context, limit int) ([]HistoryEntry, error)

ListAllHistory returns execution history across all jobs, ordered by most recent first.

func (*EntStore) ListEnabled

func (s *EntStore) ListEnabled(ctx context.Context) ([]Job, error)

ListEnabled returns only enabled cron jobs.

func (*EntStore) ListHistory

func (s *EntStore) ListHistory(ctx context.Context, jobID string, limit int) ([]HistoryEntry, error)

ListHistory returns execution history for a specific job, ordered by most recent first.

func (*EntStore) SaveHistory

func (s *EntStore) SaveHistory(ctx context.Context, entry HistoryEntry) error

SaveHistory persists a job execution history entry.

func (*EntStore) Update

func (s *EntStore) Update(ctx context.Context, job Job) error

Update modifies an existing cron job.

func (*EntStore) Upsert added in v0.6.0

func (s *EntStore) Upsert(ctx context.Context, job Job) (*Job, bool, error)

Upsert creates a new cron job or updates an existing one by name. Returns the persisted job, whether it was an update, and any error.

type Executor

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

Executor runs cron jobs by delegating to an AgentRunner and persisting results.

func NewExecutor

func NewExecutor(runner AgentRunner, delivery *Delivery, store Store, logger *zap.SugaredLogger) *Executor

NewExecutor creates a new Executor.

func (*Executor) Execute

func (e *Executor) Execute(ctx context.Context, job Job) *JobResult

Execute runs a single cron job and returns the result. It persists the execution history and delivers results to configured channels.

type HistoryEntry

type HistoryEntry struct {
	ID           string
	JobID        string
	JobName      string
	Status       string // "running" | "completed" | "failed"
	Prompt       string
	Result       string
	ErrorMessage string
	TokensUsed   int
	StartedAt    time.Time
	CompletedAt  *time.Time
}

HistoryEntry represents a persisted execution record for a cron job.

type Job

type Job struct {
	ID           string
	Name         string
	ScheduleType string // "at" | "every" | "cron"
	Schedule     string
	Prompt       string
	SessionMode  string // "isolated" | "main"
	DeliverTo    []string
	Timezone     string
	Enabled      bool
	Timeout      time.Duration // per-job timeout (0 = use default)
	LastRunAt    *time.Time
	NextRunAt    *time.Time
	CreatedAt    time.Time
}

Job represents a scheduled cron job in the domain layer.

type JobResult

type JobResult struct {
	JobID     string
	JobName   string
	Response  string
	Error     error
	StartedAt time.Time
	Duration  time.Duration
}

JobResult holds the outcome of a single job execution.

type Scheduler

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

Scheduler manages cron job registration, lifecycle, and concurrent execution.

func New

func New(store Store, executor *Executor, cfg SchedulerConfig) *Scheduler

New creates a new Scheduler.

func (*Scheduler) AddJob

func (s *Scheduler) AddJob(ctx context.Context, job Job) (bool, error)

AddJob creates or updates a job in the store and registers it with the scheduler. Returns true if an existing job was updated, false if a new one was created.

func (*Scheduler) AllHistory

func (s *Scheduler) AllHistory(ctx context.Context, limit int) ([]HistoryEntry, error)

AllHistory returns execution history across all jobs.

func (*Scheduler) History

func (s *Scheduler) History(ctx context.Context, jobID string, limit int) ([]HistoryEntry, error)

History returns execution history for a specific job.

func (*Scheduler) ListJobs

func (s *Scheduler) ListJobs(ctx context.Context) ([]Job, error)

ListJobs returns all cron jobs from the store.

func (*Scheduler) PauseJob

func (s *Scheduler) PauseJob(ctx context.Context, id string) error

PauseJob disables a job so it no longer fires.

func (*Scheduler) RemoveJob

func (s *Scheduler) RemoveJob(ctx context.Context, id string) error

RemoveJob removes a job from the scheduler and deletes it from the store.

func (*Scheduler) ResolveJobID added in v0.6.0

func (s *Scheduler) ResolveJobID(ctx context.Context, nameOrID string) (string, error)

ResolveJobID resolves a name-or-ID string to a job UUID. If the input is a valid UUID it is returned as-is; otherwise it is looked up by name via the store.

func (*Scheduler) ResumeJob

func (s *Scheduler) ResumeJob(ctx context.Context, id string) error

ResumeJob re-enables a paused job and registers it with the scheduler.

func (*Scheduler) Start

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

Start loads all enabled jobs from the database, registers them with the cron scheduler, and starts the scheduler. The provided context is used for the initial load; the scheduler itself runs until Stop is called.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop gracefully shuts down the scheduler and waits for running jobs to drain. It is safe to call Stop multiple times.

type SchedulerConfig added in v0.6.0

type SchedulerConfig struct {
	Timezone       string
	MaxJobs        int
	DefaultTimeout time.Duration
	Logger         *zap.SugaredLogger
}

SchedulerConfig holds optional configuration for the Scheduler.

type Store

type Store interface {
	Create(ctx context.Context, job Job) error
	Get(ctx context.Context, id string) (*Job, error)
	GetByName(ctx context.Context, name string) (*Job, error)
	List(ctx context.Context) ([]Job, error)
	ListEnabled(ctx context.Context) ([]Job, error)
	Update(ctx context.Context, job Job) error
	Upsert(ctx context.Context, job Job) (stored *Job, updated bool, err error)
	Delete(ctx context.Context, id string) error
	SaveHistory(ctx context.Context, entry HistoryEntry) error
	ListHistory(ctx context.Context, jobID string, limit int) ([]HistoryEntry, error)
	ListAllHistory(ctx context.Context, limit int) ([]HistoryEntry, error)
}

Store defines the persistence interface for cron jobs and their history.

type TypingIndicator

type TypingIndicator interface {
	StartTyping(ctx context.Context, channel string) (stop func(), err error)
}

TypingIndicator starts a typing indicator on a channel.

Jump to

Keyboard shortcuts

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