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 ¶
- Variables
- func MarkUndumpable() error
- func Run(ctx context.Context, client *Client, cfg Config, handlers ...Handler) error
- type Client
- func (c *Client) Blob(ctx context.Context, sum string) (io.ReadCloser, string, error)
- func (c *Client) Claim(ctx context.Context, req server.ClaimRequest) (server.ClaimAnswer, error)
- func (c *Client) Heartbeat(ctx context.Context, id string, req server.HeartbeatRequest) error
- func (c *Client) PutBlob(ctx context.Context, sum, mediaType string, r io.Reader) (server.BlobAnswer, error)
- func (c *Client) Report(ctx context.Context, id string, req server.ReportRequest) error
- func (c *Client) TaskParse(ctx context.Context, id string, token int64, pick store.ParsePick) (server.TaskParseAnswer, error)
- type Config
- type Handler
- type Result
- type SlotGrouper
- type SlotLimiter
- type StatusError
- type Task
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client calls the service's task routes.
func NewClient ¶
NewClient is a client for the service at base, sending each request through hc, which carries the worker's token.
func (*Client) Blob ¶
Blob opens the bytes named sum, and answers their media type. The caller closes the body.
func (*Client) Claim ¶
func (c *Client) Claim(ctx context.Context, req server.ClaimRequest) (server.ClaimAnswer, error)
Claim asks for tasks.
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) 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 struct {
// Outcome is server.OutcomeDone, OutcomeFailed or OutcomeRetry.
Outcome string
Error *string
RetryAfter time.Duration
// Wait marks a retry that waits on the far end, or on a stopping worker,
// rather than one that a fault in the run caused. Such a retry spends no
// attempt, and the service counts how long the task has waited since the
// first, as Task.WaitedSeconds.
Wait bool
// Outputs are the stage's records for the task's item, and Delivery is how
// a deliver task's write ended. Arrivals are what a poll that ends done
// found at its source, and Cursor its connector's next position. The
// service writes each in the transaction that ends the lease.
Outputs []server.ReportOutput
Delivery *server.ReportDelivery
Arrivals []server.ReportArrival
Cursor *string
}
Result is how a handler's run ended.
type SlotGrouper ¶ added in v0.29.0
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