Skip to main content

Job system

The job system is the base of osapi. Each operation that reads or changes state runs as an asynchronous job. Thus the API server needs no privileges, and the agents run the operations on the target hosts.

How it works​

OSAPI uses a KV-first, stream-notification architecture built on NATS JetStream:

  1. The API server writes a job definition to a NATS KV bucket.
  2. The API server publishes a notification to a NATS stream.
  3. An agent receives the notification, reads the job from KV and runs the operation.
  4. The agent writes the result to a response KV bucket.
  5. The client asks the API server for the result. The API server reads it from KV.

Job routing​

Use these routing modes to send jobs to specific agents:

TargetBehavior
_anyLoad-balanced across available agents (default)
_allBroadcast to every agent
hostnameSent to a specific host
group:labelSent to all agents matching a label

Agents register with their hostname and optional key-value labels. Labels have levels with dots between them. A label target matches its prefix: for example, group:web.dev matches agents with group: web.dev.us-east.

Job lifecycle​

Jobs progress through a defined set of states:

When you call a domain endpoint, osapi makes a job automatically. After that, you can list, examine, delete and retry the job through the job API and the CLI. Refer to CLI Reference for usage and examples, or the API Reference for the REST endpoints.

Configuration​

KeyTypeDefaultEnvironmentDescription
agent.queue_groupstringjob-agentsOSAPI_AGENT_QUEUE_GROUPQueue group for load-balanced (_any) subscriptions.
agent.hostnamestringnone: empty is the host's own hostnameOSAPI_AGENT_HOSTNAMEAgent hostname for direct routing. Defaults to the system hostname when empty.
agent.max_jobsint10OSAPI_AGENT_MAX_JOBSMaximum number of concurrent jobs to process.
agent.labelsmapnone: the labels of the host, which the operator picksOSAPI_AGENT_LABELSKey-value labels for label-based routing (max 5). Values can be hierarchical with dot separators. Each label creates NATS consumers for prefix matching. See Job System Architecture for details.
nats.kv.bucketstringjob-queueOSAPI_NATS_KV_BUCKETKV bucket for job definitions, status events and each agent's result.
nats.kv.ttlstring1hOSAPI_NATS_KV_TTLHow long a job's definition, status events and results stay in the bucket (Go duration), as nats setup sets it. Empty keeps them until max_bytes.
nats.kv.max_bytesint104857600OSAPI_NATS_KV_MAX_BYTESMaximum total size of the bucket in bytes.
nats.kv.storagestringfileOSAPI_NATS_KV_STORAGEStorage backend: 'file' or 'memory'.
nats.kv.replicasint1OSAPI_NATS_KV_REPLICASNumber of KV replicas.

Refer to Configuration for all the settings, with the NATS stream, consumer and DLQ settings.

Permissions​

The job reference page gives the permission of each method.