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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::request::JobState;
use chrono::{DateTime, Utc};
use http::Extensions;
use serde::{Deserialize, Serialize};
use std::{any::Any, marker::Send};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobContext {
pub(crate) id: String,
pub(crate) status: JobState,
pub(crate) run_at: DateTime<Utc>,
pub(crate) attempts: i32,
pub(crate) max_attempts: i32,
pub(crate) last_error: Option<String>,
pub(crate) lock_at: Option<DateTime<Utc>>,
pub(crate) lock_by: Option<String>,
pub(crate) done_at: Option<DateTime<Utc>>,
#[serde(skip)]
pub(crate) data: Data,
}
#[derive(Debug, Default)]
pub(crate) struct Data(Extensions);
impl Clone for Data {
fn clone(&self) -> Self {
Data(Extensions::new())
}
}
impl JobContext {
pub fn new(id: String) -> Self {
JobContext {
id,
status: JobState::Pending,
run_at: Utc::now(),
lock_at: None,
done_at: None,
attempts: 0,
max_attempts: 25,
last_error: None,
lock_by: None,
data: Default::default(),
}
}
pub fn data_opt<D: Any + Send + Sync>(&self) -> Option<&D> {
self.data.0.get()
}
pub fn insert<D: Any + Send + Sync>(&mut self, data: D) -> Option<D> {
self.data.0.insert(data)
}
pub fn set_max_attempts(&mut self, max_attempts: i32) {
self.max_attempts = max_attempts;
}
pub fn max_attempts(&self) -> i32 {
self.max_attempts
}
pub fn id(&self) -> String {
self.id.clone()
}
pub fn attempts(&self) -> i32 {
self.attempts
}
pub fn set_attempts(&mut self, attempts: i32) {
self.attempts = attempts;
}
pub fn done_at(&self) -> &Option<DateTime<Utc>> {
&self.done_at
}
pub fn set_done_at(&mut self, done_at: Option<DateTime<Utc>>) {
self.done_at = done_at;
}
pub fn run_at(&self) -> &DateTime<Utc> {
&self.run_at
}
pub fn set_run_at(&mut self, run_at: DateTime<Utc>) {
self.run_at = run_at;
}
pub fn lock_at(&self) -> &Option<DateTime<Utc>> {
&self.lock_at
}
pub fn set_lock_at(&mut self, lock_at: Option<DateTime<Utc>>) {
self.lock_at = lock_at;
}
pub fn status(&self) -> &JobState {
&self.status
}
pub fn set_status(&mut self, status: JobState) {
self.status = status;
}
pub fn lock_by(&self) -> &Option<String> {
&self.lock_by
}
pub fn set_lock_by(&mut self, lock_by: Option<String>) {
self.lock_by = lock_by;
}
pub fn last_error(&self) -> &Option<String> {
&self.last_error
}
pub fn set_last_error(&mut self, error: String) {
self.last_error = Some(error);
}
}