clevixflow

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clevixflow holds the Queue adapter over Clevix Flow, and the Flow function that runs its jobs.

Each job is one run of one Flow function, started with Client.Start. A key is the function's singleton key in "skip" mode, so at most one live run holds it, and it is free again as soon as that run ends. A delay is a sleep at the start of the run, so the key stays held while the job waits. Cancel cancels the run that holds a key. Find and Stats read Flow's runs index, which lags the runs by a few seconds.

Queue is also a ports.QueueAdmin: Pause and Resume pause the function, and the dead jobs are its failed runs.

Needs Flow SDK v0.8.0 or later and an engine with the run control routes.

Index

Constants

View Source
const FunctionID = "agentenkit-run"

FunctionID is the function the queue starts and the worker serves, unless both are given another.

View Source
const JobEvent = "agentenkit/job"

JobEvent is the event the function lists as its trigger. Nothing sends it: runs come from Client.Start. Flow needs every function to have a trigger.

Variables

This section is empty.

Functions

func Functions

func Functions(client *flow.Client, worker Worker, opts FunctionOpts) ([]flow.ServableFunction, error)

Functions makes the Flow function that runs the queue's jobs, and its failure function. Serve them with the app's other Flow functions.

A job runs in one step. HandleJob only fails for what a queue should retry (an unknown agent, a store that is down), so Flow's retries cover delivery; agentic-kit retries a failed run itself. When Flow gives up, the failure function hands the job to HandleDeadJob, which fails the run.

func StreamID

func StreamID(streamID string) string

StreamID is the Flow stream a run stream is kept in.

func ThreadChannel

func ThreadChannel(threadID string) string

ThreadChannel is the channel a thread's events go on.

Types

type Bus

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

Bus is an EventBus over Clevix Flow realtime.

Each thread has its own channel, and a process opens one socket per thread it follows, shared by every subscriber in the process. Each subscription has its own queue and goroutine, so one slow handler holds up nobody else. Realtime keeps nothing and drops what comes while a socket reconnects: the bus is at-most-once anyway, and a follower fills any gap from the log. An event over about 60 KB waits in the kv for a minute, and the channel carries its key. While anyone subscribes, each subscriber gets a HEARTBEAT notice (seq 0) every Heartbeat, as with the other buses.

func NewBus

func NewBus(client *flow.Client, opts BusOptions) (*Bus, error)

NewBus makes a bus. opts.Kv is required.

func (*Bus) Publish

func (b *Bus) Publish(ctx context.Context, threadID string, event ports.AgentEvent) error

Publish sends the event on the thread's channel.

func (*Bus) Subscribe

func (b *Bus) Subscribe(ctx context.Context, threadID string, handler func(ports.AgentEvent)) (func() error, error)

Subscribe adds a handler for the thread's events. It returns once the thread's socket is open, or after OpenTimeout: an event published before then may be missed, which a follower's gap read covers.

type BusOptions

type BusOptions struct {
	// Kv holds events too big for one realtime message (over about 60 KB).
	// Required.
	Kv ports.Kv
	// Heartbeat is how often each subscriber gets a HEARTBEAT notice.
	// Default one minute.
	Heartbeat time.Duration
	// OpenTimeout is how long Subscribe waits for its socket to open before
	// it returns anyway. Default 10 seconds.
	OpenTimeout time.Duration
}

BusOptions tune the bus.

type FunctionOpts

type FunctionOpts struct {
	// ID defaults to FunctionID. Must match the queue's FunctionID. At most 56
	// characters with the namespace, since the failure function adds
	// "-failure".
	ID string
	// Namespace goes in front of the ID. Must match the queue's Namespace.
	Namespace string
	// Concurrency is how many jobs of one PartitionKey run at once. Zero: no
	// limit. Job priorities only take effect under a limit.
	Concurrency int
	// GlobalConcurrency is how many jobs run at once in all, whatever their
	// PartitionKey. Zero: no limit. It can be set with Concurrency.
	GlobalConcurrency int
	// Retries is Flow's retries for a job that fails. Flow's default when nil.
	Retries *int
	// StepTimeout is how long Flow waits for one job. Defaults to 14 minutes,
	// Flow's most. When a job runs longer, Flow stops waiting and tries the
	// step again, but nothing stops the first call: it keeps running. The
	// retry finds the run lock held and does nothing, so keep segments
	// shorter than this (SegmentTimeout).
	StepTimeout time.Duration
	// SegmentTimeout is the longest SegmentTimeout of the agents this
	// function runs. When set, Functions refuses one that is not shorter than
	// StepTimeout, since a longer segment breaks Flow's retries.
	SegmentTimeout time.Duration
	// RetryBackoff is the wait before a failed job is tried again, doubled on
	// every further failure up to RetryBackoffMax, as on the Postgres queue.
	// Zero: Flow's own backoff. RetryBackoffMax zero means one minute.
	RetryBackoff    time.Duration
	RetryBackoffMax time.Duration
}

FunctionOpts tune the Flow function.

type JobData

type JobData struct {
	Job ports.RunJob `json:"job"`
	// Key is the singleton key: the enqueue's key, or the job's dispatch id.
	Key string `json:"key"`
	// Priority is in seconds, for priority.run.
	Priority int `json:"priority"`
	// NotBefore is epoch ms. The run sleeps until then before it runs the job.
	NotBefore int64 `json:"notBefore,omitempty"`
}

JobData is what one run of the function carries.

type Options

type Options struct {
	// FunctionID is the function to start. Defaults to FunctionID. Must match
	// the ID given to Functions.
	FunctionID string
	// Namespace lets two deployments share one Flow app: it goes in front of
	// the function ID, as "<namespace>-<id>". Singleton keys belong to one
	// function, so the keys are apart too. Must match Functions' Namespace.
	Namespace string
	// MaxDepth refuses a fresh dispatch (ports.ErrQueueFull) once this many
	// runs wait to start. Retries, redrives, resumes and expiries always go
	// in. The count comes from Flow's runs index, which lags a few seconds,
	// so a burst can go a little past it. Zero means no limit.
	MaxDepth int
}

Options tune the queue.

type Queue

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

Queue is a Queue over Clevix Flow.

func New

func New(client *flow.Client, opts Options) *Queue

New makes a queue.

func (*Queue) Cancel

func (q *Queue) Cancel(ctx context.Context, key string) error

Cancel cancels the run that holds key, if any.

func (*Queue) Enqueue

func (q *Queue) Enqueue(ctx context.Context, job ports.RunJob, opts *ports.EnqueueOptions) error

Enqueue starts one run for the job.

func (*Queue) Find

func (q *Queue) Find(ctx context.Context, runID string) (*ports.QueuedJob, error)

Find returns the earliest active run for runID, or nil.

func (*Queue) ListDead added in v0.12.0

func (q *Queue) ListDead(ctx context.Context, limit int) ([]ports.DeadJob, error)

ListDead lists the failed runs, newest first, with the job each carried. It asks each run for its error, so it makes one call per job.

func (*Queue) Pause added in v0.12.0

func (q *Queue) Pause(ctx context.Context) error

Pause pauses the function. Jobs enqueued meanwhile wait, paused, and jobs already started pause at their next step. Resume lets them all go.

func (*Queue) Purge added in v0.12.0

func (q *Queue) Purge(ctx context.Context, f ports.PurgeFilter) (int64, error)

Purge cancels the waiting runs that match. Running runs are left alone. With IncludeDead it deletes the failed runs that match too. It reads Flow's runs index, so a job enqueued in the last few seconds can be missed.

func (*Queue) PurgeDead added in v0.12.0

func (q *Queue) PurgeDead(ctx context.Context, olderThan time.Duration) (int64, error)

PurgeDead deletes the failed runs that ended longer ago than olderThan. Runs kept for debugging in the dashboard stay.

func (*Queue) Redrive added in v0.12.0

func (q *Queue) Redrive(ctx context.Context, id string) (bool, error)

Redrive retries a failed run from where it stopped, under the same run ID. False when there is no such failed run (a run Purge cancelled is not dead), the queue is paused, or another run holds the job's key now.

func (*Queue) Resume added in v0.12.0

func (q *Queue) Resume(ctx context.Context) error

Resume resumes the function and its paused runs.

func (*Queue) Stats

func (q *Queue) Stats(ctx context.Context) (ports.QueueStats, error)

Stats counts the function's runs by status.

type Streams

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

Streams is RunStreams over Clevix Flow streams.

Flow keeps the items, gives offsets, and lets a reader start from any offset and follow live, which is what a run stream needs. An item over about 60 KB is kept in the kv and the stream carries its key; readers get the event back. Flow keeps a stream at most 7 days and a grace window at most 7 days, so longer times are cut to that. A stream holds at most 20,000 items and 32 MB; an append past that fails.

func NewStreams

func NewStreams(client *flow.Client, opts StreamsOptions) (*Streams, error)

NewStreams makes the run streams. opts.Kv is required.

func (*Streams) Append

func (s *Streams) Append(ctx context.Context, streamID string, events []ports.StreamEvent) ([]string, error)

Append appends in order, in batches Flow takes: 100 items and under 1 MB a call.

func (*Streams) Close

func (s *Streams) Close(ctx context.Context, streamID string, end ports.StreamEnd, grace time.Duration) error

Close writes the end item and starts the grace window.

func (*Streams) Delete

func (s *Streams) Delete(ctx context.Context, streamID string) error

Delete removes the stream now. A missing one is a no-op.

func (*Streams) Open

func (s *Streams) Open(ctx context.Context, streamID string, meta ports.StreamMeta, ttl time.Duration) error

Open opens the stream. Opening one that exists is a no-op.

func (*Streams) Read

func (s *Streams) Read(ctx context.Context, streamID, after string) iter.Seq2[ports.StreamItem, error]

Read yields the items after `after`, then live ones, until the end item.

func (*Streams) Snapshot

func (s *Streams) Snapshot(ctx context.Context, streamID, after string) (*ports.StreamSnapshot, error)

Snapshot returns the items after `after` now. Nil, nil when the stream does not exist.

type StreamsOptions

type StreamsOptions struct {
	// Kv holds items too big for one stream item (over about 60 KB).
	// Required.
	Kv ports.Kv
	// RefTTL is how long such an item stays in the kv. Default 8 days: past
	// Flow's longest time to live, so it outlives its stream.
	RefTTL time.Duration
}

StreamsOptions tune the run streams.

type Worker

type Worker interface {
	Handler() func(ctx context.Context, job ports.RunJob) error
	HandleDeadJob(ctx context.Context, job ports.RunJob, attempts int, cause error)
}

Worker is the side of the platform the function calls. *agentenkit.WorkerAPI has both.

Jump to

Keyboard shortcuts

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