1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! Background job processing for Rust.
//!
//! # Quick start
//!
//! ```rust,no_run
//! use asyncq::{Job, Perform, JobContext, JobResult, Queue, Worker};
//! use asyncq::backends::InMemoryBackend;
//! use serde::{Serialize, Deserialize};
//!
//! // 1. Define your job payload and metadata
//! #[derive(Job, Serialize, Deserialize)]
//! #[job(queue = "emails", retries = 3)]
//! struct WelcomeEmail {
//! email: String,
//! }
//!
//! // 2. Implement the work
//! impl Perform for WelcomeEmail {
//! async fn perform(self, _ctx: JobContext) -> JobResult {
//! println!("sending welcome email to {}", self.email);
//! Ok(())
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! // 3. Create a queue (swap InMemoryBackend for RedisBackend in production)
//! let queue = Queue::new(InMemoryBackend::new());
//!
//! // 4. Enqueue from anywhere
//! queue.enqueue(WelcomeEmail { email: "user@example.com".into() }).await.unwrap();
//!
//! // 5. Process
//! Worker::new(queue)
//! .register::<WelcomeEmail>()
//! .concurrency(10)
//! .run()
//! .await;
//! }
//! ```
//!
//! # Testing without infrastructure
//!
//! Use [`backends::InMemoryBackend`] with [`Worker::run_once`] for fast,
//! infrastructure-free tests:
//!
//! ```rust,no_run
//! use asyncq::{Queue, Worker, backends::InMemoryBackend};
//!
//! #[tokio::test]
//! async fn test_my_job() {
//! let backend = InMemoryBackend::new();
//! let queue = Queue::new(backend.clone());
//!
//! queue.enqueue(WelcomeEmail { email: "test@test.com".into() }).await.unwrap();
//!
//! Worker::new(queue).register::<WelcomeEmail>().run_once().await.unwrap();
//!
//! assert_eq!(backend.completed_count("emails").await, 1);
//! }
//! ```
//!
//! # Shared state in jobs
//!
//! Pass your app state (database pool, config, etc.) via `Queue::with_state`.
//! Jobs access it through `ctx.state::<T>()` — the same mental model as axum:
//!
//! ```rust,ignore
//! use std::sync::Arc;
//!
//! let queue = Queue::new(backend).with_state(Arc::new(db_pool));
//!
//! impl Perform for WelcomeEmail {
//! async fn perform(self, ctx: JobContext) -> JobResult {
//! let db = ctx.state::<Arc<PgPool>>().expect("db not in state");
//! // use db ...
//! Ok(())
//! }
//! }
//! ```
// Flat re-exports — everything a user needs at the top level
pub use Backend;
pub use InMemoryBackend;
pub use JobContext;
pub use ;
pub use ;
pub use Queue;
pub use Worker;
pub use Scheduler;
// Re-export the derive macro so users only need `use asyncq::Job`
pub use Job;