Skip to main content

Crate boson_backend_nats

Crate boson_backend_nats 

Source
Expand description

NATS JetStream QueueBackend for fleet-scale deployments (remote worker / multi-host).

When to use: broker-backed fleets with NATS JetStream (KV and/or workqueue). Not a boson public crate feature — depend on this crate directly. Remote workers need unique worker_id and lease_ttl_secs > 0.

Getting started: Remote worker. Full Compose / KV vs WorkQueue / env: crate README.

Fleet URL precedence: BOSON_NATS_POOL_ROUTING over BOSON_NATS_URLS (see connect_fleet_from_env).

§Remote worker — Enqueue binary

Shared NATS with the worker. No claim loop in this process:

use std::sync::Arc;

use boson_backend_nats::NatsQueueBackend;
use boson_core::JsonExecutionContextFactory;
use boson::{configure, Boson};

let url = std::env::var("BOSON_NATS_URL")
    .unwrap_or_else(|_| "nats://127.0.0.1:4222".into());
let backend = NatsQueueBackend::connect(&url).await?;
let boson = Boson::builder()
    .queue_backend(Arc::new(backend))
    .execution_context_factory(JsonExecutionContextFactory)
    .auto_registry()
    .without_worker()
    .build()?;
configure(boson);
// MyTask::send_with(...).await?;

Also connect_auto / connect_fleet_from_env with the same without_worker + configure pattern.

§Remote worker — Worker binary

Same NATS URL / fleet, unique worker_id, and lease_ttl_secs > 0:

use std::sync::Arc;

use boson_backend_nats::NatsQueueBackend;
use boson_core::JsonExecutionContextFactory;
use boson::Boson;

let url = std::env::var("BOSON_NATS_URL")
    .unwrap_or_else(|_| "nats://127.0.0.1:4222".into());
let backend = NatsQueueBackend::connect(&url).await?;
let _boson = Boson::builder()
    .queue_backend(Arc::new(backend))
    .execution_context_factory(JsonExecutionContextFactory)
    .worker_id(std::env::var("BOSON_WORKER_ID").unwrap_or_else(|_| "worker-1".into()))
    .lease_ttl_secs(30)
    .auto_registry()
    .build()?;

Other remote-worker backends: SQLite, Postgres, Redis.

Custom adapters: How to implement on QueueBackend.

Modules§

keys
Key paths for JetStream KV (mirrors Redis layout).

Structs§

NatsEnqueueConfig
Resolved enqueue pipeline settings.
NatsQueueBackend
NATS JetStream KV queue backend.
NatsWorkQueueBackend
NATS JetStream WorkQueue backend (stream ready queue + KV job bodies).

Enums§

EnqueueMode
How enqueue writes job data to JetStream.

Functions§

connect_auto
Connect KV or WorkQueue backend based on BOSON_NATS_QUEUE_MODE.
connect_fleet_from_env
Connect one WorkQueue backend per fleet URL.
install_default_nats_backend
Install default NATS backend on global router (tests).