rust
54 lines · 9 steps
An async job queue handler in Axum
An Axum handler accepts an export request, spawns the work in the background, and immediately returns 202 with a status URL.
Explained by
highlit
1use axum::{
2 extract::State,
3 http::StatusCode,
4 response::IntoResponse,
5 Json,
6};
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9use uuid::Uuid;
10
11#[derive(Deserialize)]
12pub struct ExportRequest {
13 pub dataset_id: Uuid,
14 pub format: String,
15}
16
17#[derive(Serialize)]
18pub struct ExportAccepted {
19 pub job_id: Uuid,
20 pub status: &'static str,
21}
22
23pub async fn enqueue_export(
24 State(state): State<Arc<AppState>>,
25 Json(req): Json<ExportRequest>,
26) -> impl IntoResponse {
27 let job_id = Uuid::new_v4();
28 state.jobs.mark_pending(job_id).await;
29
30 let state = state.clone();
31 tokio::spawn(async move {
32 if let Err(err) = run_export(&state, job_id, &req).await {
33 tracing::error!(%job_id, %err, "export job failed");
34 state.jobs.mark_failed(job_id, err.to_string()).await;
35 }
36 });
37
38 (
39 StatusCode::ACCEPTED,
40 [("Location", format!("/exports/{job_id}"))],
41 Json(ExportAccepted { job_id, status: "pending" }),
42 )
43}
44
45async fn run_export(
46 state: &AppState,
47 job_id: Uuid,
48 req: &ExportRequest,
49) -> anyhow::Result<()> {
50 let rows = state.repo.load_dataset(req.dataset_id).await?;
51 let url = state.storage.write_report(&rows, &req.format).await?;
52 state.jobs.mark_complete(job_id, url).await;
53 Ok(())
54}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Returning 202 Accepted with a Location header lets clients poll a long-running job instead of blocking on the response.
- 2Spawning work with tokio::spawn detaches it from the request lifecycle, so the handler returns before the job finishes.
- 3Cloning an Arc state before moving it into a spawned task keeps shared resources available to the background work.
Related explainers
rust
use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::thread;
Aggregating metrics across threads in Rust
concurrency
shared-state
mutex
Intermediate
7 steps
rust
use std::sync::{mpsc, Arc, Mutex}; use std::thread; use std::time::Duration;
Building a thread pool in Rust
concurrency
channels
thread-pool
Advanced
9 steps
rust
use std::time::Instant; use tracing::info; pub struct Timer {
A scope-guard timer with Drop in Rust
raii
drop-guard
timing
Intermediate
7 steps
rust
use std::collections::HashMap; pub struct Memoizer<K, V, F> { cache: HashMap<K, V>,
A generic memoizer in Rust
memoization
generics
caching
Intermediate
6 steps
ruby
class ReportBatcher BATCH_SIZE = 500 def initialize(account)
Batching monthly email summaries in Rails
batching
service-object
background-jobs
Intermediate
7 steps
rust
use std::collections::HashMap; #[derive(Debug, Clone)] struct Order {
Aggregating Rust data with fold and entry
fold
hashmap
ownership
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/an-async-job-queue-handler-in-axum-explained-rust-bb16/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.