cron

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cron provides SQLite-backed scheduled task management for bc.

Cron jobs trigger agent prompts or shell commands on a 5-field cron schedule. The scheduler itself runs inside bcd; this package provides the storage layer and cron expression utilities used by both the CLI and the daemon.

Usage

store, err := cron.Open("/path/to/workspace")
if err != nil {
    return err
}
defer store.Close()

err = store.AddJob(ctx, &cron.Job{
    Name:      "daily-lint",
    Schedule:  "0 9 * * *",
    AgentName: "qa-01",
    Prompt:    "Run make lint and report results",
    Enabled:   true,
})

Index

Constants

View Source
const (
	// DefaultPollInterval is how often the scheduler checks for due jobs.
	DefaultPollInterval = 30 * time.Second

	// DefaultJobTimeout is the maximum time a single job execution may take.
	DefaultJobTimeout = 5 * time.Minute
)

Variables

This section is empty.

Functions

func NextRun

func NextRun(expr string, from time.Time) (time.Time, error)

NextRun returns the next time after `from` that matches the cron expression. Returns an error if the expression is invalid or no match is found within 4 years.

func ValidateSchedule

func ValidateSchedule(expr string) error

ValidateSchedule validates a 5-field cron expression. Returns an error describing the problem, or nil if valid.

Types

type Job

type Job struct {
	LastRun   *time.Time `json:"last_run,omitempty"`
	NextRun   *time.Time `json:"next_run,omitempty"`
	CreatedAt time.Time  `json:"created_at"`
	Name      string     `json:"name"`
	Schedule  string     `json:"schedule"`
	AgentName string     `json:"agent_name,omitempty"`
	Prompt    string     `json:"prompt,omitempty"`
	Command   string     `json:"command,omitempty"`
	RunCount  int        `json:"run_count"`
	Enabled   bool       `json:"enabled"`
	Running   bool       `json:"running"`
}

Job represents a scheduled cron task.

type LogEntry

type LogEntry struct {
	RunAt      time.Time `json:"run_at"`
	JobName    string    `json:"job_name"`
	Status     string    `json:"status"` // success, failed, timeout
	Output     string    `json:"output,omitempty"`
	ID         int64     `json:"id"`
	DurationMS int64     `json:"duration_ms"`
	CostUSD    float64   `json:"cost_usd,omitempty"`
}

LogEntry records one execution of a cron job.

type PostgresStore

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

PostgresStore provides Postgres-backed cron job storage.

func NewPostgresStore

func NewPostgresStore(db *sql.DB) *PostgresStore

NewPostgresStore creates a PostgresStore from an existing *sql.DB connection.

func (*PostgresStore) AddJob

func (p *PostgresStore) AddJob(ctx context.Context, job *Job) error

AddJob inserts a new cron job.

func (*PostgresStore) Close

func (p *PostgresStore) Close() error

Close is a no-op — the shared DB is owned by the caller.

func (*PostgresStore) DeleteJob

func (p *PostgresStore) DeleteJob(ctx context.Context, name string) error

DeleteJob removes a cron job and its logs by name.

func (*PostgresStore) GetJob

func (p *PostgresStore) GetJob(ctx context.Context, name string) (*Job, error)

GetJob returns a job by name. Returns nil, nil if not found.

func (*PostgresStore) GetLogs

func (p *PostgresStore) GetLogs(ctx context.Context, jobName string, last int) ([]*LogEntry, error)

GetLogs returns execution history for a job.

func (*PostgresStore) InitSchema

func (p *PostgresStore) InitSchema() error

InitSchema creates the cron tables in Postgres if they don't exist.

func (*PostgresStore) ListJobs

func (p *PostgresStore) ListJobs(ctx context.Context) ([]*Job, error)

ListJobs returns all cron jobs ordered by name.

func (*PostgresStore) RecordManualTrigger

func (p *PostgresStore) RecordManualTrigger(ctx context.Context, name string) error

RecordManualTrigger marks a job as manually triggered.

func (*PostgresStore) RecordRun

func (p *PostgresStore) RecordRun(ctx context.Context, entry *LogEntry) error

RecordRun records a job execution result and updates run stats.

func (*PostgresStore) SetEnabled

func (p *PostgresStore) SetEnabled(ctx context.Context, name string, enabled bool) error

SetEnabled enables or disables a job. Recomputes next_run when enabling.

type Scheduler

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

Scheduler polls the cron store and executes due jobs in the background.

func NewScheduler

func NewScheduler(store *Store, logDir string) *Scheduler

NewScheduler creates a Scheduler that polls at DefaultPollInterval.

func NewSchedulerWithConfig

func NewSchedulerWithConfig(store *Store, logDir string, pollIntervalSec, jobTimeoutSec int) *Scheduler

NewSchedulerWithConfig creates a Scheduler with configurable poll interval and job timeout. Zero values use defaults (30s poll, 5m timeout).

func (*Scheduler) IsRunning

func (s *Scheduler) IsRunning(name string) bool

IsRunning returns true if the named job is currently executing.

func (*Scheduler) LogFilePath

func (s *Scheduler) LogFilePath(name string) string

LogFilePath returns the live log file path for a job.

func (*Scheduler) Run

func (s *Scheduler) Run(ctx context.Context)

Run blocks until ctx is canceled, polling for due jobs on each tick.

func (*Scheduler) RunningJobs

func (s *Scheduler) RunningJobs() []string

RunningJobs returns the names of all currently executing jobs.

type Store

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

Store is a cron job store backed by SQLite or TimescaleDB (Postgres).

func Open

func Open(workspacePath string) (*Store, error)

Open opens the cron store using the shared workspace database. Returns an error if no shared database is available.

func OpenStore

func OpenStore(workspacePath string) (*Store, error)

OpenStore opens the cron store using the shared workspace database. Uses the shared driver type to determine the backend (timescale or sqlite).

func (*Store) AddJob

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

AddJob inserts a new cron job. Returns an error if the name already exists. Note: commands that kill the bcd process will terminate the cron scheduler itself. Use an external supervisor (systemd, launchd) for bcd restarts.

func (*Store) Close

func (s *Store) Close() error

Close is a no-op — the shared DB is owned by the caller.

func (*Store) DeleteJob

func (s *Store) DeleteJob(ctx context.Context, name string) error

DeleteJob removes a cron job and its logs by name.

func (*Store) GetJob

func (s *Store) GetJob(ctx context.Context, name string) (*Job, error)

GetJob returns a job by name. Returns nil, nil if not found.

func (*Store) GetLogs

func (s *Store) GetLogs(ctx context.Context, jobName string, last int) ([]*LogEntry, error)

GetLogs returns execution history for a job. If last > 0, limits to that many entries.

func (*Store) ListJobs

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

ListJobs returns all cron jobs ordered by name.

func (*Store) RecordManualTrigger

func (s *Store) RecordManualTrigger(ctx context.Context, name string) error

RecordManualTrigger marks a job as manually triggered (updates last_run + next_run).

func (*Store) RecordRun

func (s *Store) RecordRun(ctx context.Context, entry *LogEntry) error

RecordRun records a job execution result and updates run stats.

func (*Store) SetEnabled

func (s *Store) SetEnabled(ctx context.Context, name string, enabled bool) error

SetEnabled enables or disables a job. Recomputes next_run when enabling.

Jump to

Keyboard shortcuts

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