Documentation
¶
Overview ¶
Package client is the Go client of the TaskFlow API.
Use New to create a client for tasks, steps, workspaces, approval callbacks, system status, terminal administration, and resumable uploads. Use Client.DialStream for subscriptions and calls over the shared WebSocket, or Client.OpenShell for an interactive terminal session.
Index ¶
- Variables
- func IsNotFound(err error) bool
- type APIError
- type Action
- type Client
- func (c *Client) ApprovalCallback(ctx context.Context, decision iproto.ApprovalDecision) error
- func (c *Client) CreateTask(ctx context.Context, task *iproto.STaskReq) (*iproto.STaskCreateRes, error)
- func (c *Client) CreateTaskYAML(ctx context.Context, definition []byte) (*iproto.STaskCreateRes, error)
- func (c *Client) CreateUpload(ctx context.Context, opts UploadOptions) (*Upload, error)
- func (c *Client) DeleteTask(ctx context.Context, task string) error
- func (c *Client) DeleteWorkspace(ctx context.Context, task string, paths ...string) error
- func (c *Client) DialStream(ctx context.Context, opts ...StreamOption) (*Stream, error)
- func (c *Client) DisconnectSession(ctx context.Context, id string) error
- func (c *Client) DownloadWorkspaceFile(ctx context.Context, task, path string) (*Download, error)
- func (c *Client) DumpTask(ctx context.Context, task string) (string, error)
- func (c *Client) GetStep(ctx context.Context, task, step string) (*Result[*iproto.SStepRes], error)
- func (c *Client) GetTask(ctx context.Context, task string) (*Result[*iproto.STaskRes], error)
- func (c *Client) Healthz(ctx context.Context) (*iproto.SHealthyz, error)
- func (c *Client) Heartbeat(ctx context.Context) error
- func (c *Client) ListReplays(ctx context.Context, limit, offset int) (*iproto.ReplayList, error)
- func (c *Client) ListSessions(ctx context.Context) ([]iproto.WebShellSession, error)
- func (c *Client) ListSteps(ctx context.Context, task string) (*Result[iproto.SStepsRes], error)
- func (c *Client) ListTasks(ctx context.Context, opts ListTasksOptions) (*iproto.STaskListDetailRes, error)
- func (c *Client) ListWorkspace(ctx context.Context, task, path string) (*iproto.SFileListRes, error)
- func (c *Client) ManageStep(ctx context.Context, task, step string, action Action, pauseFor time.Duration) error
- func (c *Client) ManageTask(ctx context.Context, task string, action Action, pauseFor time.Duration) error
- func (c *Client) OpenReplay(ctx context.Context, id string) (io.ReadCloser, error)
- func (c *Client) OpenShell(ctx context.Context, size ShellSize) (*Shell, error)
- func (c *Client) ResumeUpload(ctx context.Context, uploadURL string) (*Upload, error)
- func (c *Client) StepLog(ctx context.Context, task, step string) (*Result[iproto.SStepLogsRes], error)
- func (c *Client) System(ctx context.Context) (*iproto.SSystemConfig, error)
- func (c *Client) UploadFile(ctx context.Context, opts UploadOptions, r io.Reader) (*Upload, error)
- func (c *Client) Version(ctx context.Context) (*iproto.SVersion, error)
- func (c *Client) WatchSession(ctx context.Context, id string) (*Lines, error)
- type Download
- type Event
- type Feed
- type Frame
- type Lines
- type ListTasksOptions
- type Option
- type Result
- type SessionList
- type Shell
- type ShellSize
- type Stream
- func (s *Stream) Call(ctx context.Context, method string, params, out any) error
- func (s *Stream) Close() error
- func (s *Stream) CreateTask(ctx context.Context, task *iproto.STaskReq) (*iproto.STaskCreateRes, error)
- func (s *Stream) CreateTaskYAML(ctx context.Context, definition string) (*iproto.STaskCreateRes, error)
- func (s *Stream) DeleteTask(ctx context.Context, task string) error
- func (s *Stream) DisconnectSession(ctx context.Context, id string) error
- func (s *Stream) Done() <-chan struct{}
- func (s *Stream) DumpTask(ctx context.Context, task string) (string, error)
- func (s *Stream) Err() error
- func (s *Stream) ListReplays(ctx context.Context, limit, offset int) (*iproto.ReplayList, error)
- func (s *Stream) ManageStep(ctx context.Context, task, step string, action Action, pauseFor time.Duration) error
- func (s *Stream) ManageTask(ctx context.Context, task string, action Action, pauseFor time.Duration) error
- func (s *Stream) Ping(ctx context.Context) (time.Duration, error)
- func (s *Stream) ResizePool(ctx context.Context, size int) error
- func (s *Stream) Subscribe(ctx context.Context, topic string, params any) (*Subscription, error)
- func (s *Stream) WatchEvents(ctx context.Context, since uint64) (*Feed[Event], error)
- func (s *Stream) WatchStep(ctx context.Context, task, step string) (*Feed[*iproto.SStepRes], error)
- func (s *Stream) WatchStepLog(ctx context.Context, task, step string) (*Feed[iproto.SStepLogsRes], error)
- func (s *Stream) WatchSteps(ctx context.Context, task string) (*Feed[iproto.SStepsRes], error)
- func (s *Stream) WatchSystem(ctx context.Context) (*Feed[iproto.SSystemConfig], error)
- func (s *Stream) WatchTask(ctx context.Context, task string) (*Feed[*iproto.STaskRes], error)
- func (s *Stream) WatchTasks(ctx context.Context, page iproto.SPageReq) (*Feed[*iproto.STaskListDetailRes], error)
- func (s *Stream) WatchTerminalReplay(ctx context.Context, id string) (*Feed[string], error)
- func (s *Stream) WatchTerminalSession(ctx context.Context, id string) (*Feed[jsontext.Value], error)
- func (s *Stream) WatchTerminalSessions(ctx context.Context) (*Feed[SessionList], error)
- type StreamOption
- type Subscription
- type Update
- type Upload
- type UploadOptions
Constants ¶
This section is empty.
Variables ¶
var ErrStreamClosed = errors.New("client: stream closed")
ErrStreamClosed is reported by calls and subscriptions once the Stream is closed, or when it lost the connection and reconnecting is disabled.
Functions ¶
func IsNotFound ¶
IsNotFound reports whether err is a server answer meaning "no such thing".
Types ¶
type APIError ¶
type APIError struct {
// Status is the HTTP status; zero for errors that arrived over the stream.
Status int
// Code is the TaskFlow code from the envelope; zero if none was sent.
Code api.Code
Message string
}
APIError is returned when the server reports a failure: either a non-2xx HTTP answer or a response envelope / stream frame with a failing code.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to one TaskFlow server. It is safe for concurrent use.
func New ¶
New returns a client for the server at baseURL, e.g. "http://host:2376". A path prefix is kept, so a server mounted behind a reverse proxy works too.
func (*Client) ApprovalCallback ¶
ApprovalCallback reports the decision of an external approval platform, as the platform itself would POST it to the callback URL.
func (*Client) CreateTask ¶
func (c *Client) CreateTask(ctx context.Context, task *iproto.STaskReq) (*iproto.STaskCreateRes, error)
CreateTask submits a task definition.
func (*Client) CreateTaskYAML ¶
func (c *Client) CreateTaskYAML(ctx context.Context, definition []byte) (*iproto.STaskCreateRes, error)
CreateTaskYAML submits a task definition written as YAML.
func (*Client) CreateUpload ¶
CreateUpload registers a new upload on the server without sending data.
func (*Client) DeleteTask ¶
DeleteTask removes a task and its records.
func (*Client) DeleteWorkspace ¶
DeleteWorkspace removes files or directories (recursively) from the task workspace. The server validates all paths first and reports the paths that could not be removed in the error.
func (*Client) DialStream ¶
DialStream connects the multiplexed stream. The first connection attempt is synchronous, so a wrong address or credentials fail here.
func (*Client) DisconnectSession ¶
DisconnectSession forcibly closes a live WebShell session.
func (*Client) DownloadWorkspaceFile ¶
DownloadWorkspaceFile streams a file of the task workspace. It has no request timeout other than the context.
func (*Client) ListReplays ¶
ListReplays lists the recorded sessions, newest first. limit is 1..100 (0 = server default 50).
func (*Client) ListSessions ¶
ListSessions returns the live WebShell sessions.
func (*Client) ListSteps ¶
ListSteps returns the steps of a task. The result code is the task state.
func (*Client) ListTasks ¶
func (c *Client) ListTasks(ctx context.Context, opts ListTasksOptions) (*iproto.STaskListDetailRes, error)
ListTasks returns one page of tasks.
func (*Client) ListWorkspace ¶
func (c *Client) ListWorkspace(ctx context.Context, task, path string) (*iproto.SFileListRes, error)
ListWorkspace lists a directory of the task workspace; an empty path is the workspace root.
func (*Client) ManageStep ¶
func (c *Client) ManageStep(ctx context.Context, task, step string, action Action, pauseFor time.Duration) error
ManageStep kills, pauses or resumes one step; see ManageTask for pauseFor.
func (*Client) ManageTask ¶
func (c *Client) ManageTask(ctx context.Context, task string, action Action, pauseFor time.Duration) error
ManageTask kills, pauses or resumes a task. pauseFor only applies to ActionPause: a positive value resumes the task by itself after that long, zero waits for an explicit resume.
func (*Client) OpenReplay ¶
OpenReplay streams a recording as asciinema v2 NDJSON; the first line is the header. The caller closes the returned reader.
func (*Client) OpenShell ¶
OpenShell opens an interactive shell. The server's {"zmodem":true} greeting is consumed here.
func (*Client) ResumeUpload ¶
ResumeUpload attaches to an existing upload and reads its current offset.
func (*Client) StepLog ¶
func (c *Client) StepLog(ctx context.Context, task, step string) (*Result[iproto.SStepLogsRes], error)
StepLog returns a snapshot of the step output. The result code is the run state of the step; use Stream.WatchStepLog to follow a running step.
func (*Client) System ¶
System returns the worker pool size and the host resource metrics. Resizing the pool is a stream call: see Stream.ResizePool.
func (*Client) UploadFile ¶
UploadFile creates an upload and sends r in one go. For resumable behaviour use CreateUpload / ResumeUpload and Upload.Send directly.
func (*Client) WatchSession ¶
WatchSession follows the output of a live session over plain HTTP as asciinema v2 NDJSON lines. Keep-alive blank lines are skipped. Next returns io.EOF when the session ends. For a multiplexed alternative see Stream.WatchTerminalSession.
type Download ¶
type Download struct {
// Name is the file name announced by the server.
Name string
ContentType string
// Size is the content length, or -1 when unknown.
Size int64
io.ReadCloser
}
Download is a workspace file being downloaded. Close it when done.
type Event ¶
type Event struct {
// Seq is the position in the event stream; a jump means events were dropped.
Seq uint64 `json:"seq"`
// Text is the event line, e.g. "<task> <step> Execute".
Text string `json:"text"`
}
Event is one worker event.
type Feed ¶
type Feed[T any] struct { // contains filtered or unexported fields }
Feed is a typed subscription.
func (*Feed[T]) All ¶
All iterates over the updates until the feed ends or ctx is cancelled. An abnormal end is yielded once as a final error.
func (*Feed[T]) Next ¶
Next returns the next update. ok is false once the feed is over, with err set if it ended abnormally.
func (*Feed[T]) Subscription ¶
func (f *Feed[T]) Subscription() *Subscription
Subscription exposes the raw subscription.
type Frame ¶
type Frame struct {
ID string `json:"id"`
Topic string `json:"topic"`
Event string `json:"event"`
Code api.Code `json:"code"`
Message string `json:"message"`
Data jsontext.Value `json:"data"`
}
Frame is one server frame with the payload left undecoded.
type Lines ¶
type Lines struct {
// contains filtered or unexported fields
}
Lines reads a NDJSON stream line by line. It is not safe for concurrent use.
type ListTasksOptions ¶
ListTasksOptions pages the task list. Page -1 disables paging; zero values use the server defaults (page 1, 15 per page).
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithBasicAuth ¶
WithBasicAuth sends HTTP basic credentials with every request.
func WithBearerToken ¶
WithBearerToken sends "Authorization: Bearer <token>" with every request.
func WithHTTPClient ¶
WithHTTPClient replaces the default http.Client, e.g. to set TLS or proxy options. Do not set http.Client.Timeout on it: it would also cut streaming responses; use WithRequestTimeout instead.
func WithHeader ¶
WithHeader adds a header to every request, including WebSocket handshakes.
func WithRequestTimeout ¶
WithRequestTimeout bounds every non-streaming request whose context has no deadline of its own. Zero disables the bound. The default is 30 seconds.
func WithRetries ¶
WithRetries retries idempotent requests (GET, HEAD, DELETE) up to n more times after a transport error or a 502/503/504 answer, with exponential backoff.
func WithUserAgent ¶
WithUserAgent overrides the User-Agent header.
type Result ¶
Result is a response whose Code is meaningful even on success: it carries the run state of a task or step (running, pending, paused, failed, ...).
type SessionList ¶
type SessionList struct {
Sessions []iproto.WebShellSession `json:"sessions"`
}
SessionList is the payload of the terminal-sessions topic.
type Shell ¶
type Shell struct {
// ZModem reports whether the server announced ZMODEM support. It is known
// after Open returns.
ZModem bool
// contains filtered or unexported fields
}
Shell is an interactive terminal session (/api/v1/terminal). Read, Write and Resize may be used from different goroutines, but each of them is not safe for concurrent use with itself.
func (*Shell) Read ¶
Read returns the next chunk of terminal output. It returns io.EOF-like close errors (see websocket.CloseStatus) when the shell exits.
type ShellSize ¶
type ShellSize struct{ Cols, Rows int }
ShellSize is the initial terminal size; zero values mean 80x24.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream is the multiplexed WebSocket (/api/v1/ws): live subscriptions and one-shot calls over one connection that reconnects by itself and resubscribes. It is safe for concurrent use.
func (*Stream) Call ¶
Call invokes a server method and decodes its result into out (nil to discard). A failing call returns *APIError.
func (*Stream) CreateTask ¶
func (s *Stream) CreateTask(ctx context.Context, task *iproto.STaskReq) (*iproto.STaskCreateRes, error)
CreateTask submits a task definition.
func (*Stream) CreateTaskYAML ¶
func (s *Stream) CreateTaskYAML(ctx context.Context, definition string) (*iproto.STaskCreateRes, error)
CreateTaskYAML submits a task definition written as YAML.
func (*Stream) DeleteTask ¶
DeleteTask removes a task.
func (*Stream) DisconnectSession ¶
DisconnectSession closes a live terminal session.
func (*Stream) Done ¶
func (s *Stream) Done() <-chan struct{}
Done is closed when the stream has ended for good.
func (*Stream) ListReplays ¶
ListReplays lists recordings; limit is 1..100 (0 = 50).
func (*Stream) ManageStep ¶
func (s *Stream) ManageStep(ctx context.Context, task, step string, action Action, pauseFor time.Duration) error
ManageStep kills, pauses or resumes a step.
func (*Stream) ManageTask ¶
func (s *Stream) ManageTask(ctx context.Context, task string, action Action, pauseFor time.Duration) error
ManageTask kills, pauses or resumes a task; see Client.ManageTask.
func (*Stream) ResizePool ¶
ResizePool changes the worker pool size. Shrinking may wait for workers to finish their current task, so allow the context enough time.
func (*Stream) WatchEvents ¶
WatchEvents follows worker events. since replays what was emitted after that sequence number; after a reconnect the feed resumes from the last event it delivered.
func (*Stream) WatchStepLog ¶
func (s *Stream) WatchStepLog(ctx context.Context, task, step string) (*Feed[iproto.SStepLogsRes], error)
WatchStepLog replays and follows the output of a step until it finishes. After a reconnect the log is replayed from its start.
func (*Stream) WatchSteps ¶
WatchSteps follows the step list of a task until it finishes.
func (*Stream) WatchSystem ¶
WatchSystem follows worker pool size and host metrics.
func (*Stream) WatchTasks ¶
func (s *Stream) WatchTasks(ctx context.Context, page iproto.SPageReq) (*Feed[*iproto.STaskListDetailRes], error)
WatchTasks follows one page of the task list.
func (*Stream) WatchTerminalReplay ¶
WatchTerminalReplay streams a recording as chunks of NDJSON text.
func (*Stream) WatchTerminalSession ¶
func (s *Stream) WatchTerminalSession(ctx context.Context, id string) (*Feed[jsontext.Value], error)
WatchTerminalSession follows the output of a live session read-only. Each update is one asciinema cast line.
func (*Stream) WatchTerminalSessions ¶
WatchTerminalSessions follows the list of live terminal sessions; every update is the complete list.
type StreamOption ¶
type StreamOption func(*Stream)
StreamOption configures DialStream.
func WithConnectionHooks ¶
func WithConnectionHooks(onConnect func(reconnect bool), onDisconnect func(error)) StreamOption
WithConnectionHooks installs callbacks for connection state changes. Both run on the connection goroutine and must not block.
func WithPingInterval ¶
func WithPingInterval(d time.Duration) StreamOption
WithPingInterval sets how often the client probes the connection. Zero disables the probe; the default is 30 seconds.
func WithReconnect ¶
func WithReconnect(minDelay, maxDelay time.Duration) StreamOption
WithReconnect sets the backoff between reconnect attempts. A max of zero disables reconnecting; the default is 500ms growing to 30s.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription is a live topic subscription delivering raw frames. Use the typed Watch* methods unless you need the raw payload.
func (*Subscription) Close ¶
func (sub *Subscription) Close(ctx context.Context) error
Close unsubscribes.
func (*Subscription) Err ¶
func (sub *Subscription) Err() error
Err is why the subscription ended; nil for a normal end or while it runs.
func (*Subscription) Frames ¶
func (sub *Subscription) Frames() <-chan Frame
Frames returns the delivery channel, closed when the subscription ended. Drain it promptly: a consumer that stalls blocks the whole connection.
type Update ¶
Update is one decoded subscription frame. Code carries the run state of what is watched (running, pending, paused, failed, ...).
type Upload ¶
type Upload struct {
// URL is the absolute upload URL (the Location the server returned). Keep it
// to resume the upload later with Client.ResumeUpload.
URL string
// Offset is the number of bytes the server has stored.
Offset int64
// Length is the total size, or -1 if still deferred.
Length int64
// contains filtered or unexported fields
}
Upload is a resumable tus upload into a task workspace.
func (*Upload) Patch ¶
Patch appends one chunk at the current offset. setLength fixes a deferred total size with this request.
type UploadOptions ¶
type UploadOptions struct {
// Task is the task whose workspace receives the file.
Task string
// Filename is the target path inside the workspace (forward slashes, local,
// no ':' or '\\'). Empty uses the upload ID.
Filename string
// Size is the total size in bytes; negative defers it (the size is then
// fixed with the last chunk).
Size int64
// ChunkSize is the PATCH size; default 8 MiB.
ChunkSize int64
}
UploadOptions describes a new upload.