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:
- The API server writes a job definition to a NATS KV bucket.
- The API server publishes a notification to a NATS stream.
- An agent receives the notification, reads the job from KV and runs the operation.
- The agent writes the result to a response KV bucket.
- 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:
| Target | Behavior |
|---|---|
_any | Load-balanced across available agents (default) |
_all | Broadcast to every agent |
hostname | Sent to a specific host |
group:label | Sent 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
| Key | Type | Default | Environment | Description |
|---|---|---|---|---|
agent.queue_group | string | job-agents | OSAPI_AGENT_QUEUE_GROUP | Queue group for load-balanced (_any) subscriptions. |
agent.hostname | string | none: empty is the host's own hostname | OSAPI_AGENT_HOSTNAME | Agent hostname for direct routing. Defaults to the system hostname when empty. |
agent.max_jobs | int | 10 | OSAPI_AGENT_MAX_JOBS | Maximum number of concurrent jobs to process. |
agent.labels | map | none: the labels of the host, which the operator picks | OSAPI_AGENT_LABELS | Key-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.bucket | string | job-queue | OSAPI_NATS_KV_BUCKET | KV bucket for job definitions, status events and each agent's result. |
nats.kv.ttl | string | 1h | OSAPI_NATS_KV_TTL | How 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_bytes | int | 104857600 | OSAPI_NATS_KV_MAX_BYTES | Maximum total size of the bucket in bytes. |
nats.kv.storage | string | file | OSAPI_NATS_KV_STORAGE | Storage backend: 'file' or 'memory'. |
nats.kv.replicas | int | 1 | OSAPI_NATS_KV_REPLICAS | Number 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.
Related
- CLI Reference: job commands
- API Reference: REST API documentation
- Running Jobs: targeting, statuses, polling, and what to monitor
- Architecture: system design overview