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
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use strum::{AsRefStr, EnumString};
use crate::context::JobContext;
#[derive(
EnumString, Serialize, Deserialize, Debug, Clone, AsRefStr, Hash, PartialEq, std::cmp::Eq,
)]
pub enum JobState {
#[serde(alias = "Latest")]
Pending,
Running,
Done,
Retry,
Failed,
Killed,
}
impl Default for JobState {
fn default() -> Self {
JobState::Pending
}
}
#[derive(Serialize, Debug, Deserialize, Clone)]
pub struct JobRequest<T> {
pub(crate) job: T,
pub(crate) context: JobContext,
}
impl<T> JobRequest<T> {
pub fn new(job: T) -> Self {
let id = uuid::Uuid::new_v4().to_string();
let context = JobContext::new(id);
Self { job, context }
}
pub fn new_with_context(job: T, ctx: JobContext) -> Self {
Self { job, context: ctx }
}
pub fn inner(&self) -> &T {
&self.job
}
pub fn context_mut(&mut self) -> &mut JobContext {
&mut self.context
}
pub fn context(&self) -> &JobContext {
&self.context
}
pub fn record_attempt(&mut self) {
self.context.set_attempts(self.context.attempts() + 1);
}
}
impl<T> std::ops::Deref for JobRequest<T> {
type Target = JobContext;
fn deref(&self) -> &Self::Target {
&self.context
}
}
impl<T> std::ops::DerefMut for JobRequest<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.context
}
}