|
| 1 | +# Background Jobs |
| 2 | + |
| 3 | +RustAPI provides a robust background job processing system through the `rustapi-jobs` crate. This allows you to offload time-consuming tasks (like sending emails, processing images, or generating reports) from the main request/response cycle, keeping your API fast and responsive. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +First, add `rustapi-jobs` to your `Cargo.toml`. Since `rustapi-jobs` is not re-exported by the main crate by default, you must include it explicitly. |
| 8 | + |
| 9 | +```toml |
| 10 | +[dependencies] |
| 11 | +rustapi-rs = "0.1" |
| 12 | +rustapi-jobs = "0.1" |
| 13 | +serde = { version = "1.0", features = ["derive"] } |
| 14 | +async-trait = "0.1" |
| 15 | +tokio = { version = "1.0", features = ["full"] } |
| 16 | +``` |
| 17 | + |
| 18 | +## Defining a Job |
| 19 | + |
| 20 | +A job consists of a data structure (the payload) and an implementation of the `Job` trait. |
| 21 | + |
| 22 | +```rust,no_run |
| 23 | +use rustapi_jobs::{Job, JobContext, Result}; |
| 24 | +use serde::{Deserialize, Serialize}; |
| 25 | +use async_trait::async_trait; |
| 26 | +
|
| 27 | +
|
| 28 | +// 1. Define the job payload |
| 29 | +#[derive(Debug, Serialize, Deserialize, Clone)] |
| 30 | +pub struct WelcomeEmailData { |
| 31 | + pub user_id: String, |
| 32 | + pub email: String, |
| 33 | +} |
| 34 | +
|
| 35 | +// 2. Define the job handler struct |
| 36 | +#[derive(Clone)] |
| 37 | +pub struct WelcomeEmailJob; |
| 38 | +
|
| 39 | +// 3. Implement the Job trait |
| 40 | +#[async_trait] |
| 41 | +impl Job for WelcomeEmailJob { |
| 42 | + // Unique name for the job type |
| 43 | + const NAME: &'static str = "send_welcome_email"; |
| 44 | +
|
| 45 | + // The payload type |
| 46 | + type Data = WelcomeEmailData; |
| 47 | +
|
| 48 | + async fn execute(&self, ctx: JobContext, data: Self::Data) -> Result<()> { |
| 49 | + println!("Processing job {} (attempt {})", ctx.job_id, ctx.attempt); |
| 50 | + println!("Sending welcome email to {} ({})", data.email, data.user_id); |
| 51 | +
|
| 52 | + // Simulate work |
| 53 | + tokio::time::sleep(std::time::Duration::from_millis(100)).await; |
| 54 | +
|
| 55 | + Ok(()) |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Registering and Running the Queue |
| 61 | + |
| 62 | +In your main application setup, you need to: |
| 63 | +1. Initialize the backend (Memory, Redis, or Postgres). |
| 64 | +2. Create the `JobQueue`. |
| 65 | +3. Register your job handlers. |
| 66 | +4. Start the worker loop in a background task. |
| 67 | +5. Add the `JobQueue` to your application state so handlers can use it. |
| 68 | + |
| 69 | +```rust,no_run |
| 70 | +use rustapi_rs::prelude::*; |
| 71 | +use rustapi_jobs::{JobQueue, InMemoryBackend}; |
| 72 | +// use crate::jobs::{WelcomeEmailJob, WelcomeEmailData}; // Import your job |
| 73 | +
|
| 74 | +#[tokio::main] |
| 75 | +async fn main() -> std::io::Result<()> { |
| 76 | + // 1. Initialize backend |
| 77 | + // For production, use Redis or Postgres backend |
| 78 | + let backend = InMemoryBackend::new(); |
| 79 | +
|
| 80 | + // 2. Create queue |
| 81 | + let queue = JobQueue::new(backend); |
| 82 | +
|
| 83 | + // 3. Register jobs |
| 84 | + // You must register an instance of the job handler |
| 85 | + queue.register_job(WelcomeEmailJob).await; |
| 86 | +
|
| 87 | + // 4. Start worker in background |
| 88 | + let queue_for_worker = queue.clone(); |
| 89 | + tokio::spawn(async move { |
| 90 | + if let Err(e) = queue_for_worker.start_worker().await { |
| 91 | + eprintln!("Worker failed: {}", e); |
| 92 | + } |
| 93 | + }); |
| 94 | +
|
| 95 | + // 5. Build application |
| 96 | + RustApi::auto() |
| 97 | + .with_state(queue) // Inject queue into state |
| 98 | + .serve("127.0.0.1:3000") |
| 99 | + .await |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Enqueueing Jobs |
| 104 | + |
| 105 | +You can now inject the `JobQueue` into your request handlers using the `State` extractor and enqueue jobs. |
| 106 | + |
| 107 | +```rust,no_run |
| 108 | +use rustapi_rs::prelude::*; |
| 109 | +use rustapi_jobs::JobQueue; |
| 110 | +
|
| 111 | +#[rustapi::post("/register")] |
| 112 | +async fn register_user( |
| 113 | + State(queue): State<JobQueue>, |
| 114 | + Json(payload): Json<RegisterRequest>, |
| 115 | +) -> Result<impl IntoResponse, ApiError> { |
| 116 | + // ... logic to create user in DB ... |
| 117 | + let user_id = "user_123".to_string(); // Simulated ID |
| 118 | +
|
| 119 | + // Enqueue the background job |
| 120 | + // The queue will handle serialization and persistence |
| 121 | + queue.enqueue::<WelcomeEmailJob>(WelcomeEmailData { |
| 122 | + user_id, |
| 123 | + email: payload.email, |
| 124 | + }).await.map_err(|e| ApiError::InternalServerError(e.to_string()))?; |
| 125 | +
|
| 126 | + Ok(Json(json!({ |
| 127 | + "status": "registered", |
| 128 | + "message": "Welcome email will be sent shortly" |
| 129 | + }))) |
| 130 | +} |
| 131 | +
|
| 132 | +#[derive(Deserialize)] |
| 133 | +struct RegisterRequest { |
| 134 | + username: String, |
| 135 | + email: String, |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Resilience and Retries |
| 140 | + |
| 141 | +`rustapi-jobs` handles failures automatically. If your `execute` method returns an `Err`, the job will be: |
| 142 | +1. Marked as failed. |
| 143 | +2. Optionally scheduled for retry with **exponential backoff** if retries are enabled. |
| 144 | +3. Retried up to `max_attempts` when you configure it via `EnqueueOptions`. |
| 145 | + |
| 146 | +By default, `EnqueueOptions::new()` sets `max_attempts` to `0`, so a failed job will **not** be retried unless you explicitly opt in by calling `.max_attempts(...)` with a value greater than the current `attempts` count. |
| 147 | +To customize retry behavior, use `enqueue_opts`: |
| 148 | + |
| 149 | +```rust,no_run |
| 150 | +use rustapi_jobs::EnqueueOptions; |
| 151 | +
|
| 152 | +queue.enqueue_opts::<WelcomeEmailJob>( |
| 153 | + data, |
| 154 | + EnqueueOptions::new() |
| 155 | + .max_attempts(5) // Retry up to 5 times |
| 156 | + .delay(std::time::Duration::from_secs(60)) // Initial delay |
| 157 | +).await?; |
| 158 | +``` |
| 159 | + |
| 160 | +## Backends |
| 161 | + |
| 162 | +While `InMemoryBackend` is great for testing and simple apps, production systems should use persistent backends: |
| 163 | + |
| 164 | +- **Redis**: High performance, good for volatile queues. Enable `redis` feature in `rustapi-jobs`. |
| 165 | +- **Postgres**: Best for reliability and transactional safety. Enable `postgres` feature. |
| 166 | + |
| 167 | +```toml |
| 168 | +# In Cargo.toml |
| 169 | +rustapi-jobs = { version = "0.1", features = ["redis"] } |
| 170 | +``` |
0 commit comments