Skip to main content

worker/worker.go (package worker)

Source: sdks/go/worker/worker.go. Back to the Go SDK reference index.

note

This page is generated by tools/docs/gen_sdk_reference.py from the SDK sources and the OpenAPI text folders. Do not edit it by hand: change the source and regenerate.

Model Job​

Job is one unit of work.

Fields:

NameTypeRequiredDescription
IDstringyesIdentifier of the job, sent as worker in the default heartbeat.
RunIDstringyesRunID, when set, receives heartbeats on POST /v1/runs/{id}/heartbeat.
Payloadjson.RawMessageyesRaw JSON input of the job, read by the Handler.
AttemptintyesAttempt is set by the Source (1 for the first delivery).

Interface Source​

Source of a chart: exactly one of chart_path and oci.

Source.Ack​

Ack(ctx context.Context, job *Job, result any) error

Ack reports that a job succeeded, with the result of the Handler.

NameTypeDefaultRequiredDescription
ctxcontext.ContextyesContext of the call: cancellation and deadline.
job*JobyesJob that succeeded.
resultanyyesValue returned by the Handler.

Returns: error. Error of the call (a typed error of the SDK), nil on success.

Source.Fail​

Fail(ctx context.Context, job *Job, cause error) error

Fail reports that a job failed, with the error of the Handler.

NameTypeDefaultRequiredDescription
ctxcontext.ContextyesContext of the call: cancellation and deadline.
job*JobyesJob that failed.
causeerroryesError returned by the Handler, or the panic error.

Returns: error. Error of the call (a typed error of the SDK), nil on success.

Source.Next​

Next(ctx context.Context) (*Job, _ error)

Next returns the next job, or (nil, nil) when none is available right now.

NameTypeDefaultRequiredDescription
ctxcontext.Context) (*JobyesContext of the call: cancellation and deadline.
_erroryesContext of the worker loop, canceled at shutdown.

Type alias Handler​

Handler runs a job; its context is canceled on kill switch or forced shutdown.

Definition: func(ctx context.Context, job *Job) (any, error).

Model Config​

Config configures a Worker.

Fields:

NameTypeRequiredDescription
SourceSourceyesSource of a chart: exactly one of chart_path and oci.
HandlerHandleryesHandler runs a job; its context is canceled on kill switch or forced shutdown.
Concurrencyintyesdefault 1
Polltime.Durationyeswait when the source is empty, default 1s
HeartbeatIntervaltime.DurationyesHeartbeat interval while a job runs, default 10s.
Client*eai.ClientnoClient sends the default heartbeats (when Heartbeat is nil) for jobs with a RunID.
Heartbeatfunc(ctx context.Context, job *Job) erroryesHeartbeat overrides the default heartbeat.
KillSwitchfunc(ctx context.Context) (bool, error)yesKillSwitch is polled every KillInterval (default 5s); true stops the worker immediately and cancels running jobs.
KillIntervaltime.DurationyesPolling interval of KillSwitch, 5 seconds by default.
ShutdownTimeouttime.DurationyesShutdownTimeout bounds the wait for running jobs after the context is canceled (default 30s), after which their contexts are canceled.
Logger*slog.LoggernoLogger of the source, heartbeat and kill switch warnings; slog.Default() when nil.

Model Stats​

Stats are the counters of a Worker.

Fields:

NameTypeRequiredDescription
Processedint64yesJobs whose Handler succeeded.
Failedint64yesJobs whose Handler returned an error or panicked.
Heartbeatsint64yesHeartbeats sent without error.

Class Worker​

Worker runs the loop.

Worker.Kill​

Kill()

Kill stops the worker now: no new job, running jobs are canceled.

This element takes no parameter.

Worker.Run​

Run(ctx context.Context) error

Run blocks until ctx is canceled (graceful: running jobs finish within ShutdownTimeout, returns nil) or the kill switch trips (returns ErrKilled).

NameTypeDefaultRequiredDescription
ctxcontext.ContextyesContext of the call: cancellation and deadline.

Returns: error. Error of the call (a typed error of the SDK), nil on success.

Raises: error.

Worker.Stats​

Stats() Stats

Stats returns the counters.

This element takes no parameter.

Returns: Stats. A Stats: Stats are the counters of a Worker.

Class ChanSource​

ChanSource is an in-process Source fed by a channel; acks and failures are reported on callbacks.

Fields:

NameTypeRequiredDescription
Jobs<-chan *JobyesChannel the jobs are read from, without blocking.
OnAckfunc(job *Job, result any)yesCalled by Ack with the job and its result; may be nil.
OnFailfunc(job *Job, cause error)yesCalled by Fail with the job and the error; may be nil.

ChanSource.Ack​

Ack(ctx context.Context, job *Job, result any) error

Ack reports success.

NameTypeDefaultRequiredDescription
ctxcontext.ContextyesContext of the call: cancellation and deadline.
job*JobyesJob that succeeded, passed to OnAck.
resultanyyesValue returned by the Handler, passed to OnAck.

Returns: error. Error of the call (a typed error of the SDK), nil on success.

Raises: error.

ChanSource.Fail​

Fail(ctx context.Context, job *Job, cause error) error

Fail reports a failure.

NameTypeDefaultRequiredDescription
ctxcontext.ContextyesContext of the call: cancellation and deadline.
job*JobyesJob that failed, passed to OnFail.
causeerroryesError returned by the Handler (or a panic), passed to OnFail.

Returns: error. Error of the call (a typed error of the SDK), nil on success.

Raises: error.

ChanSource.Next​

Next(ctx context.Context) (*Job, error)

Next returns the next queued job or nil when the channel is empty.

NameTypeDefaultRequiredDescription
ctxcontext.ContextyesContext of the call: cancellation and deadline.

Returns: (*Job, error). A Job: Job is one unit of work. The error is a typed error of the SDK, nil on success.

Raises: error.

Functions​

New​

New(cfg Config) (*Worker, error)

New validates the configuration.

NameTypeDefaultRequiredDescription
cfgConfigyesConfiguration of the worker; Source and Handler are required, zero durations take their defaults.

Returns: (*Worker, error). A Worker: Worker runs the loop. The error is a typed error of the SDK, nil on success.

Raises: error.

AgentKillSwitch​

AgentKillSwitch(client *eai.Client, agentID string) func(ctx context.Context) (bool, error)

Get an agent. Returns the agent, the manifest of its current version and runs, the number of its runs per state. Needs the ReBAC relation viewer on the agent. 404 agent_not_found.

Route: /v1/agents/{agent_id} (GET).

NameTypeDefaultRequiredDescription
client*eai.ClientyesParent client.
agentIDstringyesIdentifier (UUID) or name of the agent.

Returns: func(ctx context.Context) (bool, error). Decoded JSON answer of the operation "Get an agent". The error is a typed error of the SDK, nil on success.

Raises: error, the typed errors of the transport (Errors).

RunKillSwitch​

RunKillSwitch(client *eai.Client, runID string) func(ctx context.Context) (bool, error)

Send a run heartbeat. Records that the runtime is alive and tells it whether to go on: action is continue, or stop with a reason (run_cancelled, run_terminal, run_suspended, agent_killed, agent_suspended). The answer also gives stale_after_seconds. A terminal run is not updated. Needs tenant_admin, developer or agent. Audited only for the first heartbeat of a run and when the runtime is told to stop.

Route: /v1/runs/{run_id}/heartbeat (POST).

NameTypeDefaultRequiredDescription
client*eai.ClientyesParent client.
runIDstringyesIdentifier (UUID) of the run.

Returns: func(ctx context.Context) (bool, error). Decoded JSON answer of the operation "Send a run heartbeat". The error is a typed error of the SDK, nil on success.

Raises: error, the typed errors of the transport (Errors).