apalis_sql/
context.rs

1use apalis_core::request::Request;
2use apalis_core::service_fn::FromRequest;
3use apalis_core::worker::WorkerId;
4use apalis_core::{error::Error, request::State};
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8/// The context for a job is represented here
9/// Used to provide a context for a job with an sql backend
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct SqlContext {
12    status: State,
13    run_at: DateTime<Utc>,
14    max_attempts: i32,
15    last_error: Option<String>,
16    lock_at: Option<i64>,
17    lock_by: Option<WorkerId>,
18    done_at: Option<i64>,
19}
20
21impl Default for SqlContext {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl SqlContext {
28    /// Build a new context with defaults
29    pub fn new() -> Self {
30        SqlContext {
31            status: State::Pending,
32            run_at: Utc::now(),
33            lock_at: None,
34            done_at: None,
35            max_attempts: 25,
36            last_error: None,
37            lock_by: None,
38        }
39    }
40
41    /// Set the number of attempts
42    pub fn set_max_attempts(&mut self, max_attempts: i32) {
43        self.max_attempts = max_attempts;
44    }
45
46    /// Gets the maximum attempts for a job. Default 25
47    pub fn max_attempts(&self) -> i32 {
48        self.max_attempts
49    }
50
51    /// Get the time a job was done
52    pub fn done_at(&self) -> &Option<i64> {
53        &self.done_at
54    }
55
56    /// Set the time a job was done
57    pub fn set_done_at(&mut self, done_at: Option<i64>) {
58        self.done_at = done_at;
59    }
60
61    /// Get the time a job is supposed to start
62    pub fn run_at(&self) -> &DateTime<Utc> {
63        &self.run_at
64    }
65
66    /// Set the time a job should run
67    pub fn set_run_at(&mut self, run_at: DateTime<Utc>) {
68        self.run_at = run_at;
69    }
70
71    /// Get the time a job was locked
72    pub fn lock_at(&self) -> &Option<i64> {
73        &self.lock_at
74    }
75
76    /// Set the lock_at value
77    pub fn set_lock_at(&mut self, lock_at: Option<i64>) {
78        self.lock_at = lock_at;
79    }
80
81    /// Get the job status
82    pub fn status(&self) -> &State {
83        &self.status
84    }
85
86    /// Set the job status
87    pub fn set_status(&mut self, status: State) {
88        self.status = status;
89    }
90
91    /// Get the time a job was locked
92    pub fn lock_by(&self) -> &Option<WorkerId> {
93        &self.lock_by
94    }
95
96    /// Set `lock_by`
97    pub fn set_lock_by(&mut self, lock_by: Option<WorkerId>) {
98        self.lock_by = lock_by;
99    }
100
101    /// Get the time a job was locked
102    pub fn last_error(&self) -> &Option<String> {
103        &self.last_error
104    }
105
106    /// Set the last error
107    pub fn set_last_error(&mut self, error: Option<String>) {
108        self.last_error = error;
109    }
110}
111
112impl<Req> FromRequest<Request<Req, SqlContext>> for SqlContext {
113    fn from_request(req: &Request<Req, SqlContext>) -> Result<Self, Error> {
114        Ok(req.parts.context.clone())
115    }
116}