task

package
v0.0.0-beta Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package task provides scheduled task definitions, schedule parsing, and a scheduler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnvFromContext

func EnvFromContext(ctx context.Context) env.Env

EnvFromContext returns task-scoped environment variables, or nil if none set.

func ParseSchedule

func ParseSchedule(raw string) (gocron.JobDefinition, error)

ParseSchedule parses a schedule string with a prefix into a gocron JobDefinition.

Supported prefixes:

  • "cron: <expr>" → CronJob (standard 5-field crontab)
  • "every: <duration>" → DurationJob (e.g. "every: 30m", "every: 2h")
  • "daily: <HH:MM,...>" → DailyJob (e.g. "daily: 09:00,17:00")
  • "weekly: <days> <HH:MM>" → WeeklyJob (e.g. "weekly: mon,wed,fri 09:00")
  • "monthly: <days> <HH:MM>" → MonthlyJob (e.g. "monthly: 1,15 09:00")
  • "once: <RFC3339>" → OneTimeJob (e.g. "once: 2026-03-01T09:00:00Z")
  • "once: startup" → OneTimeJob that fires immediately when the scheduler starts

func WithEnv

func WithEnv(ctx context.Context, e env.Env) context.Context

WithEnv returns a context carrying task-scoped environment variables.

Types

type ForEach

type ForEach struct {
	File            string `yaml:"file"`              // Read lines from this file
	Shell           string `yaml:"shell"`             // Run command, read lines from stdout
	ContinueOnError bool   `yaml:"continue_on_error"` // Log per-item errors and continue instead of aborting
	Retries         int    `yaml:"retries"`           // Additional attempts per failed item (0 = no retry)
}

ForEach defines the iteration source for task commands.

type RunFunc

type RunFunc func(ctx context.Context, t Task) error

RunFunc is the callback invoked for each task execution. It receives the task and must handle the full execution lifecycle.

type Scheduler

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

Scheduler wraps gocron.Scheduler and manages task job registration.

func NewScheduler

func NewScheduler(tasks Tasks, concurrency uint, runFn RunFunc) (*Scheduler, error)

NewScheduler creates a Scheduler with concurrency controls, registers all scheduled tasks, but does not start. concurrency caps the maximum number of tasks running in parallel across all jobs.

func (*Scheduler) Shutdown

func (sc *Scheduler) Shutdown() error

Shutdown stops the scheduler and waits for running jobs to complete.

func (*Scheduler) Start

func (sc *Scheduler) Start()

Start begins the scheduler. Non-blocking.

type Task

type Task struct {
	Name        string
	Inherit     []string // parent names (for display)
	Description string
	Schedule    string
	Timeout     time.Duration
	Disabled    *bool
	Agent       string
	Mode        string
	Session     string
	Workdir     string
	Tools       tool.Filter
	Features    yaml.Node // raw YAML for feature overrides, decoded by consumer into config.Features
	Pre         []string
	Commands    []string
	Post        []string
	ForEach     *ForEach
	Finally     []string
	OnMaxSteps  []string
	Env         map[string]string
	EnvFile     []string
	Source      string // file path this task was loaded from
}

Task is a named scheduled task definition.

func (*Task) ApplyDefaults

func (t *Task) ApplyDefaults()

func (Task) IsDisabled

func (t Task) IsDisabled() bool

IsDisabled returns true if the task is explicitly disabled.

func (Task) IsManualOnly

func (t Task) IsManualOnly() bool

IsManualOnly returns true when no schedule is configured (run via CLI only).

func (Task) ScheduleDisplay

func (t Task) ScheduleDisplay() string

ScheduleDisplay returns the schedule string, or "manual" if unset.

func (Task) StatusDisplay

func (t Task) StatusDisplay() string

StatusDisplay returns "enabled" or "disabled".

func (Task) Validate

func (t Task) Validate() error

Validate checks the task definition for required fields.

type Tasks

type Tasks map[string]Task

Tasks maps task names to their definitions.

func (Tasks) Display

func (ts Tasks) Display() string

Display renders a table of all tasks with dynamic column widths and a header separator.

func (Tasks) Get

func (ts Tasks) Get(name string) *Task

Get returns a task by name, or nil if not found.

func (*Tasks) Load

func (ts *Tasks) Load(ff files.Files, vars map[string]string) error

Load reads YAML files and merges all task definitions into the map. Each file contains a map of task names to their definitions. Supports config inheritance via the `inherit` field within task definitions.

Task files use two delimiter systems:

  • {{ }} for load-time templates (structural generation, env vars, --set vars, sprig)
  • $[[ ]] for runtime templates (per-command vars like .Item, .Workdir)

Load-time templates are expanded before YAML parsing (Phase A). After parsing, convertRuntimeDelimiters replaces $[[ ... ]] → {{ ... }} on command-like fields so the runtime template engine sees standard {{ }} with full sprig support.

Command-like fields (pre, commands, post, foreach, finally, on_max_steps) are preserved from the initial parse so runtime template expressions survive Phase C's metadata expansion.

func (Tasks) Names

func (ts Tasks) Names() []string

Names returns a sorted slice of task names.

func (Tasks) Scheduled

func (ts Tasks) Scheduled() Tasks

Scheduled returns only tasks that have a schedule and are enabled.

Jump to

Keyboard shortcuts

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