rustapi_jobs/backend/
memory.rs1use super::{JobBackend, JobRequest};
2use crate::error::{JobError, Result};
3use async_trait::async_trait;
4use std::collections::VecDeque;
5use std::sync::{Arc, Mutex};
6
7#[derive(Debug, Clone, Default)]
9pub struct InMemoryBackend {
10 queue: Arc<Mutex<VecDeque<JobRequest>>>,
11 }
13
14impl InMemoryBackend {
15 pub fn new() -> Self {
16 Self::default()
17 }
18}
19
20#[async_trait]
21impl JobBackend for InMemoryBackend {
22 async fn push(&self, job: JobRequest) -> Result<()> {
23 let mut q = self
24 .queue
25 .lock()
26 .map_err(|_| JobError::BackendError("Lock poisoned".to_string()))?;
27 q.push_back(job);
28 Ok(())
29 }
30
31 async fn pop(&self) -> Result<Option<JobRequest>> {
32 let mut q = self
33 .queue
34 .lock()
35 .map_err(|_| JobError::BackendError("Lock poisoned".to_string()))?;
36
37 if let Some(job) = q.front() {
40 if let Some(run_at) = job.run_at {
41 if run_at > chrono::Utc::now() {
42 return Ok(None);
43 }
44 }
45 } else {
46 return Ok(None);
47 }
48
49 Ok(q.pop_front())
50 }
51
52 async fn complete(&self, _job_id: &str) -> Result<()> {
53 Ok(())
55 }
56
57 async fn fail(&self, _job_id: &str, _error: &str) -> Result<()> {
58 Ok(())
60 }
61}