workflow

package
v0.5.2 Latest Latest
Warning

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

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

Documentation

Overview

Package workflow runs DAGs of cron.Jobs.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDuplicateStep = errors.New("workflow: duplicate step")
	ErrUnknownDep    = errors.New("workflow: unknown dependency")
	ErrCycle         = errors.New("workflow: cycle detected")
	ErrNilJob        = errors.New("workflow: step has no job")
)

Functions

This section is empty.

Types

type Condition

type Condition uint8

Condition selects which upstream outcome triggers a Step.

const (
	OnSuccess  Condition = iota // upstream succeeded
	OnFailure                   // upstream failed
	OnSkipped                   // upstream was skipped
	OnComplete                  // any terminal state
)

type ConfigError

type ConfigError struct {
	Err  error
	Step string
	Dep  string // ErrUnknownDep / ErrCycle only
}

func (*ConfigError) Error

func (e *ConfigError) Error() string

func (*ConfigError) Unwrap

func (e *ConfigError) Unwrap() error

type Dep

type Dep struct {
	Name string
	When Condition
}

Dep is one DAG edge.

func After

func After(name string, when Condition) Dep

type Execution

type Execution struct {
	ID        string
	StartedAt time.Time
	Duration  time.Duration
	Results   map[string]Result
	Errors    map[string]error
	Steps     map[string]StepReport
}

Execution is the result of one Workflow run.

func (*Execution) Err

func (e *Execution) Err() error

Err joins recorded Step errors.

type Inputs added in v0.3.0

type Inputs map[string]any

Inputs holds dependency outputs keyed by step name. Plain-Job, failed, and skipped dependencies yield nil.

type Result

type Result uint8

Result is a Step outcome.

const (
	ResultPending Result = iota // never appears in a finished Execution
	ResultSuccess
	ResultFailure // Job returned an error or panicked
	ResultSkipped // a dep didn't satisfy this step's Condition, or ctx was cancelled
)

func (Result) String

func (r Result) String() string

type Step

type Step struct {
	Name    string
	Job     cron.Job
	Fn      StepFunc // alternative to Job; receives dependency outputs
	Deps    []Dep
	Timeout time.Duration    // per-run cap; zero means none
	Retry   cron.RetryPolicy // per-run retry; zero means none
}

Step is one node in the DAG. Exactly one of Job or Fn must be set.

func NewStep

func NewStep(name string, job cron.Job, deps ...Dep) Step

func NewStepFunc added in v0.3.0

func NewStepFunc(name string, fn StepFunc, deps ...Dep) Step

NewStepFunc is NewStep for a StepFunc whose output feeds dependent steps.

func (Step) WithRetry added in v0.3.0

func (s Step) WithRetry(p cron.RetryPolicy) Step

WithRetry returns a copy of s retried per p on error.

func (Step) WithTimeout added in v0.3.0

func (s Step) WithTimeout(d time.Duration) Step

WithTimeout returns a copy of s capped at d per run, with cron.ErrJobTimeout as the cancellation cause.

type StepFunc added in v0.3.0

type StepFunc func(ctx context.Context, in Inputs) (any, error)

StepFunc is a step body that consumes dependency outputs and produces its own, letting data flow through the DAG.

type StepReport added in v0.3.0

type StepReport struct {
	Result    Result
	Err       error
	Output    any       // StepFunc return value; nil for plain Jobs
	StartedAt time.Time // zero if the step never ran
	Duration  time.Duration
}

StepReport is one step's outcome with timing and output.

type Workflow

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

Workflow is a DAG of Steps.

func MustNew

func MustNew(steps ...Step) *Workflow

MustNew panics on configuration error.

func New

func New(steps ...Step) (*Workflow, error)

New constructs a Workflow. It copies Step.Deps and returns *ConfigError for duplicate steps, unknown dependencies, or cycles.

func (*Workflow) Run

func (w *Workflow) Run(ctx context.Context) error

Run executes the DAG once and returns Execution.Err.

func (*Workflow) WithOnComplete

func (w *Workflow) WithOnComplete(cb func(*Execution)) *Workflow

WithOnComplete returns a shallow copy with cb installed. cb runs before Run returns.

Jump to

Keyboard shortcuts

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