worker

package
v0.45.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 20 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 worker is `ingestion worker run`. It leases tasks from the service by capability, runs each through the handler for its capability, extends the lease while the handler runs, and reports the outcome under the lease's token. This is the external task pattern that Camunda uses: the worker holds no store, and the service is the one writer.

A stage declares its capability by its Handler, and cmd/worker.go lists every handler that the worker runs. A worker claims tasks only for the capabilities of its handlers.

Index

Constants

This section is empty.

Variables

View Source
var ErrStaleLease = errors.New("the service refused the lease's token")

ErrStaleLease is the service refusing a heartbeat or report, because the lease lapsed and a later claim took the task.

Functions

func Keep added in v0.40.0

func Keep(ctx context.Context, blobs BlobWriter, mediaType string, body []byte) (string, error)

Keep writes body through blobs as mediaType, under its SHA-256, and answers that SHA-256 in hex. Every stage that keeps the bytes it read keeps them so.

func MarkUndumpable

func MarkUndumpable() error

MarkUndumpable turns off the process's dumpable flag. The kernel then gives its /proc files to root, so a program it starts, running as the same user, cannot read its environment or memory, where its client secret and token live. ssh-agent guards its keys the same way.

func Run

func Run(ctx context.Context, client *Client, cfg Config, handlers ...Handler) error

Run claims and runs tasks for handlers until ctx ends. A worker with no handler claims nothing, and its claims still record its heartbeat. Each claim asks for no more of a capability, or of a slot group, than its slot limit leaves free. Run refuses a slot limit under 1, and one that names a capability inside a slot group, whose limit is the group's.

Types

type BlobWriter added in v0.40.0

type BlobWriter interface {
	PutBlob(ctx context.Context, sum, mediaType string, r io.Reader) (server.BlobAnswer, error)
}

BlobWriter keeps bytes under their SHA-256 in the service, as Client does through the blob route.

type Client

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

Client calls the service's task routes.

func NewClient

func NewClient(base string, hc *http.Client) *Client

NewClient is a client for the service at base, sending each request through hc, which carries the worker's token.

func (*Client) Blob

func (c *Client) Blob(ctx context.Context, sum string) (io.ReadCloser, string, error)

Blob opens the bytes named sum, and answers their media type. The caller closes the body.

func (*Client) Claim

Claim asks for tasks.

func (*Client) Heartbeat

func (c *Client) Heartbeat(ctx context.Context, id string, req server.HeartbeatRequest) error

Heartbeat extends the lease on task id.

func (*Client) PutBlob

func (c *Client) PutBlob(ctx context.Context, sum, mediaType string, r io.Reader) (server.BlobAnswer, error)

PutBlob writes r's bytes under sum as mediaType. The service refuses bytes whose SHA-256 is not sum, and answers the row it holds for the same bytes sent again.

func (*Client) QuotedParse added in v0.36.0

func (c *Client) QuotedParse(ctx context.Context, task Task) (server.TaskParseAnswer, error)

QuotedParse reads the parse that task's claim named as the one its stage's prompt quotes, so the call reads the parse that its output names. It fails where the claim named none, as where the item holds no done parse that the stage quotes, and the task tries again after its backoff.

func (*Client) Report

func (c *Client) Report(ctx context.Context, id string, req server.ReportRequest) error

Report ends the lease on task id.

func (*Client) TaskParse

func (c *Client) TaskParse(ctx context.Context, id string, token int64, pick store.ParsePick) (server.TaskParseAnswer, error)

TaskParse reads the newest done parse that pick names of the item that task id is for, and the newest of any extractor where pick is empty. The worker names its lease by token, and the service answers only while that token holds the lease, refusing with ErrStaleLease otherwise. A task whose item holds no such parse answers a StatusError coded parse_not_found.

type Config

type Config struct {
	// ID names the worker in every claim, and in the lease of each task it
	// holds.
	ID      string
	Version string
	// Slots is how many tasks run at once.
	Slots int
	// SlotLimits caps how many tasks of a capability, or of a slot group's
	// capabilities together, run at once, within Slots, such as one slot for
	// a long parse so that the rest stay free for every other stage. A key
	// names a slot group or an ungrouped capability, and overrides the
	// handlers' own SlotLimit. One that neither names may take every slot.
	SlotLimits map[string]int
	Lease      time.Duration
	// Poll is how often an idle worker claims again.
	Poll time.Duration
	Log  *slog.Logger
}

Config is how a worker runs.

type Handler

type Handler interface {
	Capability() string
	// Run runs task. ctx ends where the lease is lost, and the result is then
	// refused.
	Run(ctx context.Context, task Task) Result
}

Handler runs the tasks of one capability.

type Result

type Result = server.TaskResult

Result is how a handler's run ended. It is the result that the report carries, so a field that the service reads is one that a handler sets.

type SlotGrouper added in v0.29.0

type SlotGrouper interface {
	SlotGroup() (name string, limit int)
}

SlotGrouper is a Handler whose tasks share one slot limit with every other handler that names the same group, such as two parses whose runs each hold gigabytes. It names the group and the group's default limit, which every member names alike, and "" names no group.

type SlotLimiter added in v0.22.0

type SlotLimiter interface {
	SlotLimit() int
}

SlotLimiter is a Handler that caps how many of its tasks run at once by default. Zero leaves them uncapped, and Config.SlotLimits overrides it. A handler in a slot group sets none, because its group's limit caps it.

type StatusError

type StatusError struct {
	Method string
	Path   string
	Status int
	// Code is the refusal's code, where its body carries one.
	Code string
	Body string
}

StatusError is the service refusing a call, other than a task call's 409.

func (*StatusError) Error

func (e *StatusError) Error() string

type Task

type Task = server.LeasedTask

Task is one leased task.

Jump to

Keyboard shortcuts

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