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    priority: i32,
20}
21
22impl Default for SqlContext {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl SqlContext {
29    /// Build a new context with defaults
30    pub fn new() -> Self {
31        SqlContext {
32            status: State::Pending,
33            run_at: Utc::now(),
34            lock_at: None,
35            done_at: None,
36            max_attempts: 5,
37            last_error: None,
38            lock_by: None,
39            priority: 0,
40        }
41    }
42
43    /// Set the number of attempts
44    pub fn set_max_attempts(&mut self, max_attempts: i32) {
45        self.max_attempts = max_attempts;
46    }
47
48    /// Gets the maximum attempts for a job. Default 25
49    pub fn max_attempts(&self) -> i32 {
50        self.max_attempts
51    }
52
53    /// Get the time a job was done
54    pub fn done_at(&self) -> &Option<i64> {
55        &self.done_at
56    }
57
58    /// Set the time a job was done
59    pub fn set_done_at(&mut self, done_at: Option<i64>) {
60        self.done_at = done_at;
61    }
62
63    /// Get the time a job is supposed to start
64    pub fn run_at(&self) -> &DateTime<Utc> {
65        &self.run_at
66    }
67
68    /// Set the time a job should run
69    pub fn set_run_at(&mut self, run_at: DateTime<Utc>) {
70        self.run_at = run_at;
71    }
72
73    /// Get the time a job was locked
74    pub fn lock_at(&self) -> &Option<i64> {
75        &self.lock_at
76    }
77
78    /// Set the lock_at value
79    pub fn set_lock_at(&mut self, lock_at: Option<i64>) {
80        self.lock_at = lock_at;
81    }
82
83    /// Get the job status
84    pub fn status(&self) -> &State {
85        &self.status
86    }
87
88    /// Set the job status
89    pub fn set_status(&mut self, status: State) {
90        self.status = status;
91    }
92
93    /// Get the time a job was locked
94    pub fn lock_by(&self) -> &Option<WorkerId> {
95        &self.lock_by
96    }
97
98    /// Set `lock_by`
99    pub fn set_lock_by(&mut self, lock_by: Option<WorkerId>) {
100        self.lock_by = lock_by;
101    }
102
103    /// Get the time a job was locked
104    pub fn last_error(&self) -> &Option<String> {
105        &self.last_error
106    }
107
108    /// Set the last error
109    pub fn set_last_error(&mut self, error: Option<String>) {
110        self.last_error = error;
111    }
112
113    /// Set the job priority. Larger values will run sooner. Default is 0.
114    pub fn set_priority(&mut self, priority: i32) {
115        self.priority = priority
116    }
117
118    /// Get the job priority
119    pub fn priority(&self) -> &i32 {
120        &self.priority
121    }
122}
123
124impl<Req> FromRequest<Request<Req, SqlContext>> for SqlContext {
125    fn from_request(req: &Request<Req, SqlContext>) -> Result<Self, Error> {
126        Ok(req.parts.context.clone())
127    }
128}