Documentation
¶
Overview ¶
Package flow is the Go SDK for clevix-worker, a durable workflow engine on Cloudflare. Its API follows Inngest's inngestgo.
Make a client with NewClient, add functions with CreateFunction, and serve them with client.Serve(), which returns an http.Handler for your app's one endpoint (for example /api/flow). Steps are in the step package, parallel steps in the group package.
Index ¶
- Constants
- func BoolPtr(b bool) *bool
- func IntPtr(i int) *int
- func NoRetryError(err error) error
- func RetryAtError(err error, at time.Time) error
- func StrPtr(s string) *string
- type AppManifest
- type BatchEvents
- type CancelOn
- type Client
- func (c *Client) AppID() string
- func (c *Client) Close()
- func (c *Client) Manifest(serveURL string) AppManifest
- func (c *Client) ManifestHash() string
- func (c *Client) Realtime() Realtime
- func (c *Client) Send(ctx context.Context, event Event) (string, error)
- func (c *Client) SendMany(ctx context.Context, events []Event) ([]string, error)
- func (c *Client) Serve() http.Handler
- func (c *Client) Sync(ctx context.Context, serveURL string) (int, []byte, error)
- type ClientOpts
- type Concurrency
- type Debounce
- type Event
- type FunctionConfig
- type FunctionOpts
- type GenericEvent
- type Handler
- type Input
- type InputCtx
- type MultipleTriggers
- type Priority
- type RateLimit
- type Realtime
- type RealtimeToken
- type SDKInfo
- type ServableFunction
- type Singleton
- type StepError
- type Throttle
- type Timeouts
- type Trigger
- type Triggerable
Constants ¶
const DefaultBaseURL = "https://worker.clevixstudio.com"
DefaultBaseURL is the engine URL when neither ClientOpts.BaseURL nor FLOW_BASE_URL is set. Self-hosters set FLOW_BASE_URL.
const Version = "0.6.5"
Version is this SDK's version, sent in the manifest.
Variables ¶
This section is empty.
Functions ¶
func NoRetryError ¶
NoRetryError wraps err so it is not retried. Returned from a step.Run callback, the step fails at once. Returned from the handler, the run fails at once.
func RetryAtError ¶
RetryAtError wraps err so the next attempt runs at the given time.
Types ¶
type AppManifest ¶
type AppManifest struct {
AppID string `json:"appId"`
// URL is left out of the sync at boot: the engine keeps the URL it has.
URL string `json:"url,omitempty"`
SDK *SDKInfo `json:"sdk,omitempty"`
Functions []FunctionConfig `json:"functions"`
// ManifestHash is the SDK's hash of SDK and Functions (see ManifestHash).
ManifestHash string `json:"manifestHash,omitempty"`
}
AppManifest is what the app sends on sync and returns on GET.
type BatchEvents ¶
BatchEvents runs the function with up to MaxSize events, or after Timeout.
type CancelOn ¶
type CancelOn struct {
Event string
// Match is a field path in both events, for example "data.orderId".
Match *string
// If is an expression over event (the trigger) and async (the new event).
If *string
// Timeout stops listening after this long. Zero means as long as the run lives.
Timeout time.Duration
}
CancelOn cancels a run when a matching event arrives.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client holds an app's functions and talks to the engine.
func NewClient ¶
func NewClient(opts ClientOpts) (*Client, error)
NewClient makes a client. It fails if AppID or a signing key is not valid.
func (*Client) Close ¶ added in v0.6.5
func (c *Client) Close()
Close stops the sync checks that Serve started, and waits until a check on its way has ended. Call it when the server shuts down. Serve still answers after Close; it just checks no more.
func (*Client) Manifest ¶
func (c *Client) Manifest(serveURL string) AppManifest
Manifest builds the app's manifest with serveURL as the app URL.
func (*Client) ManifestHash ¶ added in v0.6.5
ManifestHash is the SDK's hash of the app's manifest: "v1:" and the sha256 of its SDK and functions as JSON with sorted keys. The URL is left out, so every copy of the app has the same hash. The engine stores it on sync, and the sync at boot syncs only when the engine has a different one.
func (*Client) Serve ¶
Serve returns the http.Handler for the app's one endpoint:
- POST runs a function (signed by the engine).
- PUT syncs: sends the manifest to the engine and returns its answer. Call it on deploy, for example: curl -X PUT https://app/api/flow
- GET returns the manifest (signed GET with an empty body).
Without a signing key, and not in dev mode, it answers every request with 500 NoSigningKey.
The first call also starts the sync at boot in the background (see ClientOpts.NoSyncOnBoot), so call Serve after the functions are made. The checks go on until Close.
type ClientOpts ¶
type ClientOpts struct {
// AppID names the app. Required.
AppID string
// SigningKey checks calls from the engine and signs sync. Env: FLOW_SIGNING_KEY.
SigningKey *string
// SigningKeyFallback is the old key while rotating. Env: FLOW_SIGNING_KEY_FALLBACK.
SigningKeyFallback *string
// EventKey is used to send events. Env: FLOW_EVENT_KEY.
EventKey *string
// BaseURL is the engine URL. Env: FLOW_BASE_URL. Default: DefaultBaseURL.
BaseURL *string
// Dev turns off signature checks. Env: FLOW_DEV=1.
Dev *bool
// HTTPClient is used for sync and sending events. Default: http.DefaultClient.
HTTPClient *http.Client
// NoStreaming turns off streamed answers: answers are sent whole at the end.
// By default a POST answer is streamed when the engine asks for it, so a
// proxy in front of the app does not end a long step (see docs/protocol.md).
NoStreaming bool
// HeartbeatInterval is the time between heartbeats in a streamed answer.
// Default: 20s. At least 1ms. It also moves the server's write deadline
// (http.Server.WriteTimeout) on, so that does not end a long step.
HeartbeatInterval time.Duration
// NoSyncOnBoot turns off the sync at boot. By default Serve checks in the
// background whether the engine has this app's manifest, and syncs when it
// changed, so a deploy needs no Resync. Then it checks again every
// checkEveryMs the engine gives (5 min) until Client.Close. It needs the
// signing key; without one it does nothing.
NoSyncOnBoot bool
}
ClientOpts configures a client. Fields left nil are read from environment variables; values set here win.
type Concurrency ¶
type Concurrency struct {
Limit int `json:"limit"`
Key *string `json:"key,omitempty"`
Scope string `json:"scope,omitempty"`
}
Concurrency limits how many steps of the function run at once. Key is an expression over event; Scope is "fn" (default), "env" or "account".
type Debounce ¶
Debounce waits for Period of quiet before running, with the last event. Timeout is the longest it may wait in all (zero: no limit).
type FunctionConfig ¶
type FunctionConfig struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Triggers []Trigger `json:"triggers"`
Retries *int `json:"retries,omitempty"`
Timeouts *timeoutsJSON `json:"timeouts,omitempty"`
CancelOn []cancelOnJSON `json:"cancelOn,omitempty"`
Concurrency []Concurrency `json:"concurrency,omitempty"`
Throttle *throttleJSON `json:"throttle,omitempty"`
RateLimit *rateLimitJSON `json:"rateLimit,omitempty"`
Debounce *debounceJSON `json:"debounce,omitempty"`
BatchEvents *batchEventsJSON `json:"batchEvents,omitempty"`
Priority *Priority `json:"priority,omitempty"`
Idempotency *string `json:"idempotency,omitempty"`
Singleton *Singleton `json:"singleton,omitempty"`
}
FunctionConfig is one function in the manifest, with Inngest's key names.
type FunctionOpts ¶
type FunctionOpts struct {
ID string
Name string
Retries *int
Timeouts *Timeouts
CancelOn []CancelOn
Concurrency []Concurrency
Throttle *Throttle
RateLimit *RateLimit
Debounce *Debounce
BatchEvents *BatchEvents
Priority *Priority
// Idempotency is an expression over event; one run per value.
Idempotency *string
Singleton *Singleton
}
FunctionOpts configures a function. Only ID is required.
Durations are sent to the engine as "<n>s" when they are whole seconds, else as "<n>ms".
type GenericEvent ¶
type GenericEvent[T any] struct { ID string `json:"id"` Name string `json:"name"` Data T `json:"data"` Timestamp int64 `json:"ts"` User any `json:"user,omitempty"` }
GenericEvent is an event a function receives, with typed data.
type Input ¶
type Input[T any] struct { Event GenericEvent[T] Events []GenericEvent[T] InputCtx InputCtx }
Input is what a handler gets: the trigger event, the batch (one item unless batching) and the run details.
type InputCtx ¶
type InputCtx struct {
FunctionID string
RunID string
// Attempt counts from 0.
Attempt int
MaxAttempts int
}
InputCtx says which run and attempt this is.
type MultipleTriggers ¶
type MultipleTriggers []Trigger
MultipleTriggers starts a function from any of several triggers.
type Priority ¶
type Priority struct {
Run *string `json:"run,omitempty"`
}
Priority orders waiting runs. Run is an expression giving a boost in seconds (-600 to 600).
type Realtime ¶ added in v0.4.0
type Realtime struct {
// contains filtered or unexported fields
}
Realtime sends small live messages from functions to browsers. A message goes to the browsers connected right now; nothing is stored or replayed. Get it with Client.Realtime.
A publish is a plain call, not a step: if the step around it is retried, the message is sent again. Treat messages as hints (progress, logs), and keep anything that must happen once in a step's result.
type RealtimeToken ¶ added in v0.4.0
type RealtimeToken struct {
Token string `json:"token"`
URL string `json:"url"`
Channel string `json:"channel"`
Topics []string `json:"topics"`
// ExpiresAt is the connect deadline (ms since epoch), about a minute away.
ExpiresAt int64 `json:"expiresAt"`
}
RealtimeToken lets one browser connect to one channel and read the listed topics. Hand it to the browser as JSON; the TypeScript client reads it.
type ServableFunction ¶
type ServableFunction interface {
// ID is the function ID without the app prefix.
ID() string
// FullyQualifiedID is "<appId>-<id>", used by step.Invoke and in
// inngest/function.failed events.
FullyQualifiedID() string
// Config is the function's manifest entry.
Config() FunctionConfig
// contains filtered or unexported methods
}
ServableFunction is a function made by CreateFunction.
func CreateFunction ¶
func CreateFunction[T any](c *Client, opts FunctionOpts, trigger Triggerable, handler Handler[T]) (ServableFunction, error)
CreateFunction defines a function and adds it to the client.
For failure handling (onFailure in TypeScript), create a second function with trigger EventTrigger("inngest/function.failed", StrPtr("event.data.function_id == '<appId>-<id>'")). Its event data holds function_id, run_id, error and event (the original trigger event).
type Timeouts ¶
Timeouts sets time limits. Step is the longest one step's HTTP call may take. Finish is the longest the whole run may take, counted from its first step call (time spent queued does not count); a run still going then is cancelled. Zero means the engine's default (Step) or no limit (Finish).
type Trigger ¶
type Trigger struct {
Event string `json:"event,omitempty"`
If *string `json:"if,omitempty"`
Cron string `json:"cron,omitempty"`
}
Trigger starts a function: an event (with an optional "if" expression) or a cron schedule. Make one with EventTrigger or CronTrigger.
func CronTrigger ¶
CronTrigger runs the function on a cron schedule, for example "0 2 * * *" or "TZ=Africa/Lagos 0 2 * * *".
func EventTrigger ¶
EventTrigger runs the function for each event with this name. expr is an optional "if" expression over event, for example "event.data.plan == 'pro'".
type Triggerable ¶
type Triggerable interface {
// contains filtered or unexported methods
}
Triggerable is a Trigger or MultipleTriggers.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package group runs steps in parallel.
|
Package group runs steps in parallel. |
|
internal
|
|
|
core
Package core holds the parts shared by the flow, step and group packages: the wire types, the replay state of one call, and the error types.
|
Package core holds the parts shared by the flow, step and group packages: the wire types, the replay state of one call, and the error types. |
|
Package step has the durable steps a flow function calls: Run, Sleep, SleepUntil, WaitForEvent, Invoke and Send.
|
Package step has the durable steps a flow function calls: Run, Sleep, SleepUntil, WaitForEvent, Invoke and Send. |