cron

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cron implements a small, dependency-free standard 5-field cron parser and next-run evaluator for the `zero cron` scheduler.

Index

Constants

View Source
const (
	StatusActive = "active"
	StatusPaused = "paused"
)

Variables

View Source
var ErrJobNotFound = errors.New("cron job not found")

ErrJobNotFound is returned (wrapped) by Get when a job's metadata file is absent — a genuinely removed job. Callers use errors.Is to distinguish this from a transient read failure (IO error, permission), which must NOT be treated as "job removed".

Functions

func DefaultRoot

func DefaultRoot(env map[string]string) string

DefaultRoot mirrors sessions.DefaultRoot: <XDG_DATA_HOME|~/.local/share>/zero/cron.

func ResolveLoopPrompt

func ResolveLoopPrompt(cwd, home string) (string, error)

ResolveLoopPrompt returns the loop prompt for a scheduled job: the first readable loop.md found in <cwd>/.zero, <cwd>/.agents, <home>/.zero, <home>/.agents, else the built-in fallback. Files over maxLoopPromptBytes return an error; symlinks are skipped (never read) to prevent exfiltration.

Types

type Job

type Job struct {
	ID        string    `json:"id"`
	Expr      string    `json:"expr"`
	Prompt    string    `json:"prompt"`
	Cwd       string    `json:"cwd,omitempty"`
	Model     string    `json:"model,omitempty"`
	Status    string    `json:"status"`
	FireCount int       `json:"fireCount"`
	NextRunAt time.Time `json:"nextRunAt"`
	CreatedAt time.Time `json:"createdAt"`
}

Job is a stored scheduled job.

type RecipeDef

type RecipeDef struct {
	ID     string
	Expr   string
	Prompt string
}

RecipeDef is a named preset job (cron expression + prompt).

func Recipe

func Recipe(id string) (RecipeDef, bool)

Recipe returns the preset with the given id.

func Recipes

func Recipes() []RecipeDef

Recipes returns the built-in preset recipes.

type RunRecord

type RunRecord struct {
	JobID        string    `json:"jobId"`
	At           time.Time `json:"at"`
	ExitCode     int       `json:"exitCode"`
	SessionTitle string    `json:"sessionTitle,omitempty"`
	Error        string    `json:"error,omitempty"`
}

RunRecord is one fire's outcome, appended to the job's runs.jsonl.

type Schedule

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

Schedule is a parsed 5-field cron expression. Field sets are bitsets over the field's value range. domStar/dowStar record whether the day-of-month / day-of-week field was "*", which selects the standard Vixie OR semantics.

func Parse

func Parse(expr string) (Schedule, error)

Parse parses a standard 5-field cron expression: minute hour day-of-month month day-of-week. Supports *, lists (a,b), ranges (a-b), steps (*/s, a-b/s, a/s), 3-letter month/weekday names, and 7 as Sunday.

func (Schedule) Next

func (s Schedule) Next(after time.Time) time.Time

Next returns the first scheduled instant strictly after `after`, evaluated in after's location. It returns the zero time.Time if no match occurs within nextSearchYears (an impossible schedule such as Feb 30). It is robust to DST gaps: a per-iteration forward-progress guard prevents stalling on a non-existent local instant (e.g. 02:30 on a spring-forward day).

func (Schedule) String

func (s Schedule) String() string

type Store

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

func NewStore

func NewStore(opts StoreOptions) *Store

func (*Store) Add

func (s *Store) Add(job Job) (Job, error)

Add assigns an ID + CreatedAt and writes the job's metadata.json.

func (*Store) AppendRun

func (s *Store) AppendRun(id string, rec RunRecord) error

func (*Store) Get

func (s *Store) Get(id string) (Job, error)

func (*Store) List

func (s *Store) List() ([]Job, error)

func (*Store) Mutate

func (s *Store) Mutate(id string, mutate func(current Job, readErr error) (Job, error)) (Job, error)

Mutate atomically updates job id under the per-job cross-process lock, closing the read-modify-write race between concurrent schedulers (and against Update/ Remove). It re-reads the CURRENT on-disk job and passes it to mutate along with any read error; mutate returns the job to persist. A removed job aborts with ErrJobNotFound (no recreate). A transient read error (not removal) is surfaced via readErr so the caller can still persist a best-effort state — the fire path advances the schedule regardless, to avoid a re-fire.

func (*Store) Remove

func (s *Store) Remove(id string) error

func (*Store) Runs

func (s *Store) Runs(id string) ([]RunRecord, error)

func (*Store) Update

func (s *Store) Update(job Job) error

type StoreOptions

type StoreOptions struct {
	RootDir string
	Now     func() time.Time
}

StoreOptions configures a Store. RootDir defaults to DefaultRoot(os env); Now defaults to time.Now. Both injectable for tests.

Jump to

Keyboard shortcuts

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