taskworker

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: Apache-2.0 Imports: 20 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 taskworker is the single poll→execute→update loop shared by every worker flavour in the CLI: stdio workers, JavaScript (goja) workers, registry workers, and skill tool workers. It is layered — the loop and its two interfaces live here, the Conductor SDK is confined to the runner bridge, and each flavour's execution logic lives in a Handler.

Before this package existed the loop was hand-copied four times in cmd/worker.go with no backoff, no context cancellation, and no tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToTaskResult

func ToTaskResult(t Task, r Result, opts RunnerOptions) *model.TaskResult

ToTaskResult maps a Result onto the SDK's TaskResult. It is a pure function so the mapping — the part most likely to drift during a refactor — can be table-tested without a live client.

Types

type Config

type Config struct {
	// PollBackoff is the wait after an empty or failed poll. Zero uses the default.
	PollBackoff time.Duration
}

Config tunes the loop.

type GojaHandler

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

GojaHandler runs a JavaScript worker in the CLI's embedded interpreter.

The program is compiled once and each task gets a fresh goja.Runtime: Runtimes are not safe for concurrent use, and a Handler is shared across the goroutines of a batch poll.

func NewGojaHandler

func NewGojaHandler(script, name string) (*GojaHandler, error)

NewGojaHandler compiles script for repeated execution. name appears in stack traces.

func (*GojaHandler) Handle

func (h *GojaHandler) Handle(ctx context.Context, t Task) Result

type Handler

type Handler interface {
	Handle(ctx context.Context, t Task) Result
}

Handler executes one task.

A Handler MUST be safe for concurrent use: one Handler is shared across all the goroutines of a batch poll.

type HandlerFunc

type HandlerFunc func(ctx context.Context, t Task) Result

HandlerFunc adapts a function to Handler.

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, t Task) Result

type PolledTask

type PolledTask struct {
	Task Task
	// Err is set when this task could not be converted from the SDK model. The loop
	// fails such a task individually and continues with the rest of the batch.
	Err error
}

PolledTask is one entry from a poll. Conversion happens per task so that a single malformed task fails on its own instead of discarding its whole batch.

type Result

type Result struct {
	Status Status                 `json:"status"`
	Output map[string]interface{} `json:"output,omitempty"`
	Logs   []string               `json:"logs,omitempty"`
	Reason string                 `json:"reason,omitempty"`
}

Result is the outcome of executing one task.

There is no error return alongside it: every outcome is a Result, so there is exactly one way to express failure. Handlers build their own failure Results because the flavours shape failures differently — JavaScript workers report the message under an "error" output key, stdio workers use ReasonForIncompletion plus logs — and those differences are observable by workflows. The json tags exist because --verbose prints a Result back to the user, and that output was previously the stdio worker's own lowercase, omitempty-tagged shape.

func Failure

func Failure(reason string) Result

Failure builds a Result for the common shape: FAILED with a reason and no output.

type Runner

type Runner interface {
	Poll(ctx context.Context, taskType string) ([]PolledTask, error)
	Update(ctx context.Context, t Task, r Result) error
}

Runner is the server boundary: polling for work and reporting results. The production implementation wraps the Conductor SDK; tests inject a fake.

func NewConductorRunner

func NewConductorRunner(taskClient *client.TaskResourceApiService, opts RunnerOptions) Runner

NewConductorRunner returns a Runner backed by the Conductor task client, which the cmd layer supplies via internal.GetTaskClient().

type RunnerOptions

type RunnerOptions struct {
	WorkerID      string
	Domain        string
	Count         int32
	PollTimeoutMs int32
	// UseTaskWorkerID reports the polled task's own worker id on the result instead of
	// WorkerID. JavaScript workers behave this way and workflows can observe it.
	UseTaskWorkerID bool
}

RunnerOptions carries the poll and reporting settings that the worker commands expose as flags. Each poll field is applied only when set, because always sending a field — an empty Workerid, for instance — would change the outgoing request.

type Status

type Status string

Status is the task status reported back to Conductor. It is deliberately an open string type rather than a closed enum: JavaScript workers forward whatever status their script returns straight through, and FAILED_WITH_TERMINAL_ERROR is documented and in use. Handlers that need to constrain the set normalise it themselves.

const (
	StatusCompleted               Status = "COMPLETED"
	StatusFailed                  Status = "FAILED"
	StatusInProgress              Status = "IN_PROGRESS"
	StatusFailedWithTerminalError Status = "FAILED_WITH_TERMINAL_ERROR"
)

type StdioHandler

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

StdioHandler runs an external program per task: the full task JSON goes in on stdin, a result JSON comes back on stdout. It is safe for concurrent use — each Handle call builds its own command and buffers.

func NewStdioHandler

func NewStdioHandler(opts StdioOptions) *StdioHandler

NewStdioHandler returns a Handler that executes opts.Command for each task.

func (*StdioHandler) Handle

func (h *StdioHandler) Handle(ctx context.Context, t Task) Result

type StdioOptions

type StdioOptions struct {
	// Command and Args are the worker program to run, once per task.
	Command string
	Args    []string
	// Env is appended to the child's environment. It is passed in rather than read from
	// viper so this package does not depend on process-global config, and so tests can
	// assert what the child receives without mutating global state.
	Env []string
	// Domain, when set, is exported to the child as POLL_DOMAIN.
	Domain string
	// ExecTimeout bounds a single task's execution. Zero means no timeout.
	ExecTimeout time.Duration
	// Verbose prints the task JSON and the result JSON to stdout.
	Verbose bool
}

StdioOptions configures a StdioHandler.

type Task

type Task struct {
	ID         string
	WorkflowID string
	Type       string
	// WorkerID is the worker id carried by the polled task. JavaScript workers report
	// this value back on the result rather than the configured --worker-id.
	WorkerID string
	// Raw is json.Marshal(model.Task) — the whole task, not the raw HTTP body and not
	// just inputData. Stdio workers write it to the child's stdin and goja workers
	// expose it as $.task, so both depend on it being the SDK struct's marshalling.
	Raw json.RawMessage
}

Task is one polled task, decoupled from the SDK's model.Task.

type Worker

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

Worker runs the poll→execute→update loop for a single task type over a Runner.

func NewWorker

func NewWorker(runner Runner, cfg Config) *Worker

NewWorker returns a Worker backed by the given Runner.

func (*Worker) Run

func (w *Worker) Run(ctx context.Context, taskType string, h Handler)

Run polls taskType and dispatches each task to h until ctx is cancelled.

Cancellation is not immediate: Run returns once the in-flight batch finishes. A handler that blocks — a goja script has no interrupt wired, for instance — delays shutdown for as long as it runs.

Transient poll failures back off and retry rather than stop the loop, and a failing task affects only itself.

Jump to

Keyboard shortcuts

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