worker

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 16 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 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.

Types

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) 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) (server.TaskParseAnswer, error)

TaskParse reads the newest done parse of the item that task id is for. 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 done 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
	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 struct {
	// Outcome is server.OutcomeDone, OutcomeFailed or OutcomeRetry.
	Outcome    string
	Error      *string
	RetryAfter time.Duration
	// Outputs are the stage's records for the task's item, and Delivery is how
	// a deliver task's write ended. The service writes both in the
	// transaction that ends the lease.
	Outputs  []server.ReportOutput
	Delivery *server.ReportDelivery
}

Result is how a handler's run ended.

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