workers

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 19 Imported by: 1 module, 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 workers provides a long-poll runtime for BPMN external service tasks. Register a handler per task type, then call Run; the runtime owns polling, lock heartbeats, dispatch, and outcome mapping (Complete on success, ThrowError on a BpmnError, ThrowError with a generic code on any other handler error).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleTyped

func HandleTyped[T any](w *Worker, taskType string, handler func(ctx context.Context, job *Job, in T) (variables.Vars, error), opts ...HandleOption)

HandleTyped registers a handler that decodes the job's input variables into a value of type T before invoking handler. Use it for opt-in typed dispatch when the producing service task has a known variable schema.

Types

type BpmnError

type BpmnError struct {
	Code      string
	Variables variables.Vars
}

BpmnError is a typed error a handler can return to fail a job with a BPMN error code. The runtime translates it into a ThrowError call against the originating service task - matching boundary error events on the task can then route the exception in the model.

Variables, when provided, are merged into the instance scope as part of the error throw.

func NewBpmnError

func NewBpmnError(code string, vars variables.Vars) *BpmnError

NewBpmnError constructs a BpmnError. Pass nil for vars when there are no supplementary variables.

func (*BpmnError) Error

func (e *BpmnError) Error() string

Error implements error.

type Config

type Config struct {
	// ClientID is the worker's stable identity. Used to attribute job locks
	// and counted as one of the active workers per task type. Surrounding
	// whitespace is trimmed. If empty, derived from the host name.
	ClientID string
	// Logger receives lifecycle messages (poll errors, fatal handler errors).
	// Nil disables logging.
	Logger *log.Logger
	// MaxErrorMessageBytes caps the byte length of the auto-built
	// WORKER_ERROR message attached when a handler returns a non-BpmnError.
	// Zero falls back to 2048. User-thrown BpmnError variables are not clamped.
	MaxErrorMessageBytes int
	// ShutdownTimeout bounds how long Run waits for running handlers after its
	// ctx is cancelled. Handlers still running then are cancelled and not
	// reported, so their locks run out on the server. Zero falls back to 20s.
	ShutdownTimeout time.Duration
}

Config configures a Worker.

type HandleOption

type HandleOption func(*handleOpts)

HandleOption tunes per-task-type registration.

func WithLockDuration

func WithLockDuration(d time.Duration) HandleOption

WithLockDuration sets the exclusive lock duration on each acquired job. The server clamps it to 10s..5m by default. The runtime heartbeats at half of the lock the server granted while the handler runs.

func WithMaxJobs

func WithMaxJobs(n int) HandleOption

WithMaxJobs caps how many jobs the runtime acquires per poll for the task type. Higher values amortize the round-trip; lower values share the queue with other workers.

func WithPollTimeout

func WithPollTimeout(d time.Duration) HandleOption

WithPollTimeout sets how long each long-poll call waits before returning 204. Defaults to 30s.

type Handler

type Handler func(ctx context.Context, job *Job) (variables.Vars, error)

Handler processes a single job. Return value semantics:

  • (vars, nil) → Complete with vars merged into instance
  • (_, *BpmnError) → ThrowError with the supplied code as a business error (retryable=false): routed to a matching boundary error event immediately, bypassing the retry budget
  • (_, any other error) → ThrowError with a generic code as a retryable technical failure: the retry budget is consumed before the error surfaces

type Job

type Job struct {
	*generated.ExternalJob
	// Vars holds the input variables resolved by the service task. Use
	// variables.Get[T] or variables.As[T] to decode into typed values.
	Vars variables.Vars
}

Job is the work unit handed to a Handler. It wraps the generated ExternalJob and exposes the variables decoded into a Vars value.

type Worker

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

Worker is a long-poll runtime owning a set of handlers, one per task type. Use Worker.Handle to register a handler; Worker.Run starts the polling goroutines and blocks until ctx is cancelled.

func New

func New(api *generated.ClientWithResponses, projectID openapi_types.UUID, cfg Config) *Worker

New constructs a Worker bound to projectID. api should be an authenticated client (typically obtained from auth.NewClient).

func (*Worker) Handle

func (w *Worker) Handle(taskType string, handler Handler, opts ...HandleOption)

Handle registers handler as the processor for taskType. Re-registering an existing taskType replaces the previous handler.

func (*Worker) Run

func (w *Worker) Run(ctx context.Context) error

Run polls each registered task type in its own goroutine until ctx is cancelled. It then stops polling and waits up to Config.ShutdownTimeout for running jobs to finish and report. Returns nil after shutdown, or an error if no handlers are registered.

Jump to

Keyboard shortcuts

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