rustapi_jobs/
backend.rs

1use crate::error::Result;
2use async_trait::async_trait;
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6pub mod memory;
7
8#[cfg(feature = "redis")]
9pub mod redis;
10
11#[cfg(feature = "postgres")]
12pub mod postgres;
13
14/// A raw job request to be stored
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct JobRequest {
17    pub id: String,
18    pub name: String,
19    pub payload: serde_json::Value,
20    pub created_at: DateTime<Utc>,
21    pub attempts: u32,
22    pub max_attempts: u32,
23    pub last_error: Option<String>,
24    pub run_at: Option<DateTime<Utc>>,
25}
26
27/// Backend storage for jobs
28#[async_trait]
29pub trait JobBackend: Send + Sync {
30    /// Push a new job to the queue
31    async fn push(&self, job: JobRequest) -> Result<()>;
32
33    /// Pop the next available job
34    /// Should return None if no job is available or ready
35    async fn pop(&self) -> Result<Option<JobRequest>>;
36
37    /// Mark a job as completed successfully
38    async fn complete(&self, job_id: &str) -> Result<()>;
39
40    /// Mark a job as failed
41    /// The manager will decide whether to retry (re-push) or move to DLQ
42    async fn fail(&self, job_id: &str, error: &str) -> Result<()>;
43}