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.
Needs Flow SDK v0.7.0 or later and an engine with the run routes.
Index ¶
- Constants
- func Functions(client *flow.Client, worker Worker, opts FunctionOpts) ([]flow.ServableFunction, error)
- func StreamID(streamID string) string
- func ThreadChannel(threadID string) string
- type Bus
- type BusOptions
- type FunctionOpts
- type JobData
- type Options
- type Queue
- func (q *Queue) Cancel(ctx context.Context, key string) error
- func (q *Queue) Enqueue(ctx context.Context, job ports.RunJob, opts *ports.EnqueueOptions) error
- func (q *Queue) Find(ctx context.Context, runID string) (*ports.QueuedJob, error)
- func (q *Queue) Stats(ctx context.Context) (ports.QueueStats, error)
- type Streams
- func (s *Streams) Append(ctx context.Context, streamID string, events []ports.StreamEvent) ([]string, error)
- func (s *Streams) Close(ctx context.Context, streamID string, end ports.StreamEnd, grace time.Duration) error
- func (s *Streams) Delete(ctx context.Context, streamID string) error
- func (s *Streams) Open(ctx context.Context, streamID string, meta ports.StreamMeta, ttl time.Duration) error
- func (s *Streams) Read(ctx context.Context, streamID, after string) iter.Seq2[ports.StreamItem, error]
- func (s *Streams) Snapshot(ctx context.Context, streamID, after string) (*ports.StreamSnapshot, error)
- type StreamsOptions
- type Worker
Constants ¶
const FunctionID = "agentenkit-run"
FunctionID is the function the queue starts and the worker serves, unless both are given another.
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 ThreadChannel ¶
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) 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, since the failure function adds "-failure".
ID 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
// 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
}
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
}
Options tune the queue.
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue is a Queue over Clevix Flow.
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) 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.
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.