For the complete documentation index, see llms.txt.
Skip to main content
Version: 8.10

Job workers

Technical Preview

The Rust SDK is a technical preview. Its API surface may still evolve and changes may not follow semantic versioning. Pin an exact version if you need stability.

use camunda_orchestration_sdk::{JobAction, JobWorkerConfig};

let worker = client.create_job_worker(
JobWorkerConfig::new("payment-service")
.max_jobs_to_activate(20)
.worker_name("payment-worker"),
);

worker
.run(|job| async move {
println!("handling job {}", job.key());
JobAction::complete_with(serde_json::json!({ "paid": true }))
})
.await?;

A handler returns a JobAction:

  • JobAction::complete() / JobAction::complete_with(vars) — complete the job.
  • JobAction::fail("message") — fail the job (retries decremented by the engine).
  • JobAction::error("ERROR_CODE") — throw a catchable BPMN error.
  • JobAction::leave() — take no action; the job remains activated until timeout.

The Job exposes key(), job_type(), process_instance_key(), variables(), and variables_as::<T>() for typed deserialization.

Enable job leasing with JobWorkerConfig::new("...").with_lease(true). Each activated job then carries a lease token that the worker sends back on complete, fail, and throw-error, so the engine fences the command against a superseded activation (for example after the job timed out and another worker picked it up). Leasing is off by default and needs a server that supports it: rather than silently sending unfenced commands, a worker that asked for a lease and is handed a job without a token stops with CamundaError::LeaseNotHonored.

For managed lifecycle, register workers on the client and stop them all gracefully:

// Spawn managed workers; the client retains them in its registry.
client.spawn_worker(client.worker_config("payment-service"), |job| async move {
JobAction::complete_with(serde_json::json!({ "paid": true }))
});

// ... later, on shutdown: drain in-flight jobs and stop every worker gracefully.
client.stop_all_workers().await?;

For complete, runnable programs see examples/worker.rs and examples/deploy_start_and_work.rs.