-
-
Notifications
You must be signed in to change notification settings - Fork 1
Update documentation versions and expand learning path #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fa4a2a6
e3be77b
daade11
fb9edb6
42a0cd6
48fd787
a9b3c0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,25 +5,87 @@ | |
|
|
||
| ## Background Processing | ||
|
|
||
| Long-running tasks shouldn't block HTTP requests. `rustapi-jobs` provides a robust queue system. | ||
| Long-running tasks shouldn't block HTTP requests. `rustapi-jobs` provides a robust queue system that can run in-memory or be backed by Redis/Postgres. | ||
|
|
||
| ## Usage Example | ||
|
|
||
| Here is how to set up a simple background job queue using the in-memory backend. | ||
|
|
||
| ### 1. Define the Job | ||
|
|
||
| Jobs are simple structs that implement `Serialize` and `Deserialize`. | ||
|
|
||
| ```rust | ||
| use serde::{Deserialize, Serialize}; | ||
| use rustapi_jobs::{Job, JobContext, Result}; | ||
| use std::sync::Arc; | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug, Clone)] | ||
| struct EmailJob { | ||
| to: String, | ||
| subject: String, | ||
| body: String, | ||
| } | ||
|
|
||
| // Implement the Job trait to define how to process it | ||
| #[async_trait::async_trait] | ||
| impl Job for EmailJob { | ||
| const NAME: &'static str = "email_job"; | ||
|
|
||
| async fn run(&self, _ctx: JobContext) -> Result<()> { | ||
| println!("Sending email to {} with subject: {}", self.to, self.subject); | ||
| // Simulate work | ||
| tokio::time::sleep(std::time::Duration::from_millis(100)).await; | ||
| Ok(()) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. Configure the Queue | ||
|
|
||
| In your `main` function, initialize the queue and start the worker. | ||
|
|
||
| ```rust | ||
| // Define a job | ||
| #[derive(Serialize, Deserialize)] | ||
| struct EmailJob { to: String } | ||
| use rustapi_jobs::{JobQueue, InMemoryBackend, EnqueueOptions}; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| // 1. Create the backend | ||
| let backend = InMemoryBackend::new(); | ||
|
|
||
| // 2. Create the queue | ||
| let queue = JobQueue::new(backend); | ||
|
|
||
| // Enqueue it | ||
| queue.push(EmailJob { to: "alice@example.com" }).await; | ||
| // 3. Register the job type | ||
| queue.register_job::<EmailJob>(); | ||
|
|
||
| // 4. Start the worker in the background | ||
| let worker_queue = queue.clone(); | ||
| tokio::spawn(async move { | ||
| worker_queue.start_workers().await; | ||
| }); | ||
|
|
||
| // 5. Enqueue a job | ||
| queue.enqueue(EmailJob { | ||
| to: "user@example.com".into(), | ||
| subject: "Welcome!".into(), | ||
| body: "Thanks for joining.".into(), | ||
| }).await?; | ||
|
Comment on lines
+68
to
+73
|
||
|
|
||
| Ok(()) | ||
| } | ||
| ``` | ||
|
|
||
| ## Backends | ||
|
|
||
| - **Memory**: Great for development and testing. | ||
| - **Redis**: High throughput persistence. | ||
| - **Postgres**: Transactional reliability (acid). | ||
| - **Memory**: Great for development and testing. Zero infrastructure required. | ||
| - **Redis**: High throughput persistence. Recommended for production. | ||
| - **Postgres**: Transactional reliability (ACID). Best if you cannot lose jobs. | ||
|
|
||
| ## Reliability Features | ||
|
|
||
| ## Reliability | ||
| The worker system includes built-in reliability features: | ||
|
|
||
| The worker system features: | ||
| - **Exponential Backoff**: Automatic retries for failing jobs. | ||
| - **Dead Letter Queue**: Poison jobs are isolated for manual inspection. | ||
| - **Exponential Backoff**: Automatically retries failing jobs with increasing delays. | ||
| - **Dead Letter Queue (DLQ)**: "Poison" jobs that fail repeatedly are isolated for manual inspection. | ||
| - **Concurrency Control**: Limit the number of concurrent workers to prevent overloading your system. | ||
|
Comment on lines
+89
to
+91
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,21 @@ Verify your installation: | |
| cargo-rustapi --version | ||
| ``` | ||
|
|
||
| ## Adding to an Existing Project | ||
|
|
||
| If you prefer not to use the CLI, you can add RustAPI to your `Cargo.toml` manually: | ||
|
|
||
| ```bash | ||
| cargo add rustapi-rs@0.1.233 | ||
| ``` | ||
|
Comment on lines
+28
to
+34
|
||
|
|
||
| Or add this to your `Cargo.toml`: | ||
|
|
||
| ```toml | ||
| [dependencies] | ||
| rustapi-rs = "0.1.233" | ||
| ``` | ||
|
|
||
| ## Editor Setup | ||
|
|
||
| For the best experience, we recommend **VS Code** with the **rust-analyzer** extension. This provides: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The text suggests a job is just a serializable struct, but in
rustapi-jobstheJobtrait is implemented by a handler type with an associatedDatapayload type (the payload is what needsSerialize/Deserialize). Update this description to match the actual API so readers don’t model jobs incorrectly.