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
use std::error::Error;
use async_trait::async_trait;
use serde_json::Value;
use crate::director::Director;
#[derive(Debug)]
pub enum JobResult {
Accept,
Defer(String),
Reject(String),
Fail(Box<dyn Error + Send + 'static>),
Restart,
Done,
}
impl JobResult {
pub fn accept() -> Self {
Self::Accept
}
pub fn defer<M>(msg: M) -> Self
where
M: Into<String>,
{
Self::Defer(msg.into())
}
pub fn reject<M>(msg: M) -> Self
where
M: Into<String>,
{
Self::Reject(msg.into())
}
pub fn fail<E>(err: E) -> Self
where
E: Into<Box<dyn Error + Send + Sync + 'static>>,
{
Self::Fail(err.into())
}
pub fn restart() -> Self {
Self::Restart
}
pub fn done() -> Self {
Self::Done
}
pub fn combine(self, other: Self) -> Self {
match (self, other) {
(Self::Accept, next) | (next, Self::Accept) => next,
(Self::Done, _) | (_, Self::Done) => Self::Done,
(Self::Restart, _) | (_, Self::Restart) => Self::Restart,
(Self::Defer(left), Self::Defer(right)) => Self::Defer(format!("{}\n{}", left, right)),
(defer @ Self::Defer(_), _) | (_, defer @ Self::Defer(_)) => defer,
(fail @ Self::Fail(_), _) | (_, fail @ Self::Fail(_)) => fail,
(Self::Reject(left), Self::Reject(right)) => {
Self::Reject(format!("{}\n{}", left, right))
},
}
}
}
pub type JobError = Box<dyn Error + Send + Sync>;
pub trait HandlerCore {
fn add_to_director<'a>(&'a self, director: &mut Director<'a>) -> Result<(), JobError>;
}
#[async_trait]
pub trait Handler: HandlerCore {
async fn handle(
&self,
kind: &str,
object: &Value,
retry_count: usize,
) -> Result<JobResult, JobError>;
}