Documentation
¶
Index ¶
- Constants
- func Output[In, Out any](run *TaskRun[In], def TaskDef[Out]) (Out, error)
- func Permanent(err error) error
- func WithCode(code string, err error) error
- type Cipher
- type CodedError
- type Config
- type Failure
- type Manager
- type None
- type PermanentError
- type Propagator
- type Registry
- type RunnerConfig
- type StartOption
- type Status
- type TaskDef
- type TaskOption
- type TaskRun
- type TaskSpec
- type Workflow
- type WorkflowDef
- func (d WorkflowDef[In]) Input(ctx context.Context, run *WorkflowRun) (in In, err error)
- func (d WorkflowDef[In]) Name() string
- func (d WorkflowDef[In]) Start(ctx context.Context, m *Manager, input In, opts ...StartOption) (*WorkflowRun, error)
- func (d WorkflowDef[In]) StartTx(ctx context.Context, m *Manager, tx pgx.Tx, input In, opts ...StartOption) (*WorkflowRun, error)
- type WorkflowRun
Constants ¶
const LevelAlert = slog.Level(16)
LevelAlert marks a run the engine parked and will not retry on its own. It is above slog.LevelError and equals go-libs alert.LevelAlert.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cipher ¶
type Cipher interface {
Encrypt(ctx context.Context, plaintext string) (string, error)
Decrypt(ctx context.Context, token string) (string, error)
}
Cipher seals a run's input and call context at rest.
type CodedError ¶
CodedError carries a caller-defined code the engine persists but never interprets.
func (*CodedError) Error ¶
func (e *CodedError) Error() string
func (*CodedError) Unwrap ¶
func (e *CodedError) Unwrap() error
type Config ¶
type Config struct {
DefaultTaskTimeout time.Duration // 60s
DefaultMaxAttempts int // 8
BackoffBase time.Duration // 1s
BackoffMax time.Duration // 5m
MaxInProcessBackoff time.Duration // 5s
SweepMaxAge time.Duration // 10m
MaxLocalRuns int // 64
Context Propagator[json.RawMessage] // built with Propagate; nil leaves a task with no calling context off the fast path
Cipher Cipher // nil stores input and call context as plain JSON
}
Config holds executor defaults; NewManager fills zero fields.
type Failure ¶
type Failure struct {
Task string
// Error is persisted; handlers must keep it PII-free.
Error string
// Code is the WithCode tag, empty when none was set or the engine parked the run.
Code string
Attempts int
}
Failure is why a run parked: the task that gave up and its last error.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager starts, executes, and observes workflow runs backed by the store.
func NewManager ¶
func NewManager(ctx context.Context, db *pgkit.DB, reg *Registry, cfg Config, log *slog.Logger) (*Manager, error)
NewManager freezes reg and returns a Manager over db. It fails unless the widest task timeout plus MaxInProcessBackoff is below SweepMaxAge, so the sweeper never reclaims a run inside a live task attempt.
func (*Manager) NewRunnable ¶
func (m *Manager) NewRunnable(cfg RunnerConfig) runnable.Runnable
NewRunnable returns the poll loop and, when cfg.SweepEnabled, the sweep loop. On shutdown it waits for every in-flight execution before returning.
type PermanentError ¶
type PermanentError struct {
Err error
}
PermanentError marks a task failure as non-retryable.
func (*PermanentError) Error ¶
func (e *PermanentError) Error() string
func (*PermanentError) Unwrap ¶
func (e *PermanentError) Unwrap() error
type Propagator ¶
type Propagator[T any] interface { Capture(ctx context.Context) (T, error) Restore(ctx context.Context, snapshot T) (context.Context, error) }
Propagator captures the caller's context when a run starts and restores it around each handler, so a run resumed in another process sees the same values. Implement it for your own snapshot type and pass it through Propagate; the engine stores the snapshot as JSON.
func Propagate ¶
func Propagate[T any](p Propagator[T]) Propagator[json.RawMessage]
Propagate adapts p to the JSON form Config.Context takes. A snapshot that does not decode into T parks the run.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds registered workflows. Register every workflow before NewManager, which freezes the registry.
type RunnerConfig ¶
type RunnerConfig struct {
PollInterval time.Duration
BatchSize int
Concurrency int
SweepEnabled bool
SweepInterval time.Duration
ClaimStrategy store.ClaimStrategy
}
RunnerConfig configures the poller and sweeper.
type StartOption ¶
type StartOption func(*startConfig)
StartOption configures Start and StartTx.
func WithIdempotencyKey ¶
func WithIdempotencyKey(key string) StartOption
WithIdempotencyKey returns the existing run for a duplicate key untouched, so a parked run stays parked.
type Status ¶
type Status = store.WorkflowStatus
Status is a workflow run's lifecycle state, aliased from the persisted values.
const ( StatusRunning Status = store.WorkflowStatusRunning StatusSucceeded Status = store.WorkflowStatusSucceeded StatusStuck Status = store.WorkflowStatusStuck // StatusCancelled is reserved; no code path produces it. StatusCancelled Status = store.WorkflowStatusCancelled )
type TaskDef ¶
type TaskDef[Out any] struct { // contains filtered or unexported fields }
TaskDef is a task's durable name paired with the type of output it records.
type TaskOption ¶
type TaskOption func(*taskConfig)
TaskOption configures one task's retry, timeout, or skip behavior.
func Backoff ¶
func Backoff(base, max time.Duration) TaskOption
Backoff sets exponential backoff min(base*2^(attempt-1), max). Mutually exclusive with BackoffSchedule.
func BackoffSchedule ¶
func BackoffSchedule(schedule []time.Duration) TaskOption
BackoffSchedule sets the delay after each failed attempt, reusing the last entry past the end. Mutually exclusive with Backoff; MaxAttempts still caps.
func MaxAttempts ¶
func MaxAttempts(n int) TaskOption
MaxAttempts caps the number of attempts before a task parks.
type TaskRun ¶
TaskRun is the handler's view of one task run. Execution is at-least-once: a handler must respect ctx cancellation and be idempotent.
func NewTaskRun ¶
NewTaskRun builds a single-attempt TaskRun for unit-testing a handler without a Manager. Key outputs by TaskDef.Name.
func (*TaskRun[In]) IdempotencyKey ¶
IdempotencyKey is stable across retries and crash-resumes of this task. Provider dedup windows are finite, so a run parked for weeks can still re-emit.
func (*TaskRun[In]) LastAttempt ¶
LastAttempt reports whether a failure now exhausts the attempt budget. A permanent failure parks earlier regardless.
type TaskSpec ¶
type TaskSpec[In any] struct { // contains filtered or unexported fields }
TaskSpec is one task, produced by Handle and consumed by Define.
type Workflow ¶
type Workflow struct {
// contains filtered or unexported fields
}
Workflow is a named, ordered set of tasks built by Define.
type WorkflowDef ¶
type WorkflowDef[In any] struct { // contains filtered or unexported fields }
WorkflowDef is a workflow's durable name paired with its input type.
func NewWorkflow ¶
func NewWorkflow[In any](name string) WorkflowDef[In]
NewWorkflow declares a workflow whose tasks and Start all take input In.
func (WorkflowDef[In]) Input ¶
func (d WorkflowDef[In]) Input(ctx context.Context, run *WorkflowRun) (in In, err error)
Input returns the input run was started with. After a duplicate start it is the first start's input, not the caller's.
func (WorkflowDef[In]) Name ¶
func (d WorkflowDef[In]) Name() string
Name returns the workflow's durable name.
func (WorkflowDef[In]) Start ¶
func (d WorkflowDef[In]) Start(ctx context.Context, m *Manager, input In, opts ...StartOption) (*WorkflowRun, error)
Start durably enqueues a run of the workflow and launches it in-process when a local slot is free; otherwise the poller drives it.
func (WorkflowDef[In]) StartTx ¶
func (d WorkflowDef[In]) StartTx(ctx context.Context, m *Manager, tx pgx.Tx, input In, opts ...StartOption) (*WorkflowRun, error)
StartTx inserts the run inside tx, visible once the caller commits. It does not launch the run: the Manager cannot observe the commit, so the first Await does.
type WorkflowRun ¶
type WorkflowRun struct {
// contains filtered or unexported fields
}
WorkflowRun is a handle to one started workflow run.
func (*WorkflowRun) Await ¶
func (r *WorkflowRun) Await(ctx context.Context) (Status, error)
Await blocks until the run is terminal, or returns StatusRunning when ctx expires first while the run keeps going in the background.