-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjobs_api.rs
More file actions
139 lines (123 loc) · 3.9 KB
/
jobs_api.rs
File metadata and controls
139 lines (123 loc) · 3.9 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#[cfg(not(any(feature = "extras-jobs", feature = "jobs")))]
fn main() {
eprintln!(
"Run this example with jobs support enabled:\n cargo run -p rustapi-rs --example jobs_api --features extras-jobs"
);
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
use async_trait::async_trait;
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
use rustapi_rs::extras::jobs::{InMemoryBackend, Job, JobContext, JobQueue};
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
use rustapi_rs::prelude::*;
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
use std::sync::{
atomic::{AtomicU64, Ordering},
Arc,
};
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
#[derive(Clone)]
struct AppState {
processed_jobs: Arc<AtomicU64>,
queue: JobQueue,
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
#[derive(Debug, Clone, Deserialize, Serialize, Schema)]
struct EmailJobData {
to: String,
subject: String,
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
#[derive(Clone)]
struct SendEmailJob {
processed_jobs: Arc<AtomicU64>,
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
#[async_trait]
impl Job for SendEmailJob {
const NAME: &'static str = "send_email";
type Data = EmailJobData;
async fn execute(
&self,
ctx: JobContext,
data: Self::Data,
) -> std::result::Result<(), rustapi_rs::extras::jobs::JobError> {
println!(
"[job:{} attempt:{}] sending '{}' to {}",
ctx.job_id, ctx.attempt, data.subject, data.to
);
self.processed_jobs.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
#[derive(Debug, Serialize, Schema)]
struct EnqueueResponse {
job_id: String,
queued: bool,
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
#[derive(Debug, Serialize, Schema)]
struct WorkerResponse {
processed: bool,
total_processed: u64,
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
async fn enqueue_email(
State(state): State<AppState>,
Json(payload): Json<EmailJobData>,
) -> Created<EnqueueResponse> {
let job_id = state
.queue
.enqueue::<SendEmailJob>(payload)
.await
.expect("enqueue should succeed for the in-memory backend");
Created(EnqueueResponse {
job_id,
queued: true,
})
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
async fn process_next(State(state): State<AppState>) -> Json<WorkerResponse> {
let processed = state
.queue
.process_one()
.await
.expect("processing should succeed for the in-memory backend");
Json(WorkerResponse {
processed,
total_processed: state.processed_jobs.load(Ordering::SeqCst),
})
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
async fn queue_stats(State(state): State<AppState>) -> Json<WorkerResponse> {
Json(WorkerResponse {
processed: false,
total_processed: state.processed_jobs.load(Ordering::SeqCst),
})
}
#[cfg(any(feature = "extras-jobs", feature = "jobs"))]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
println!("Starting jobs example...");
println!(" -> POST http://127.0.0.1:3000/jobs/email");
println!(" -> POST http://127.0.0.1:3000/jobs/process-next");
println!(" -> GET http://127.0.0.1:3000/jobs/stats");
let processed_jobs = Arc::new(AtomicU64::new(0));
let queue = JobQueue::new(InMemoryBackend::new());
queue
.register_job(SendEmailJob {
processed_jobs: processed_jobs.clone(),
})
.await;
RustApi::new()
.state(AppState {
processed_jobs,
queue,
})
.route("/jobs/email", post(enqueue_email))
.route("/jobs/process-next", post(process_next))
.route("/jobs/stats", get(queue_stats))
.run("127.0.0.1:3000")
.await
}