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
use std::collections::HashMap;
use chrono::{DateTime, Utc};
use std::io::prelude::*;
use serde_json;
use std::io;
mod cmd;
mod resp;
pub use self::cmd::*;
pub use self::resp::*;
#[derive(Serialize, Deserialize, Debug)]
pub struct Job {
pub(crate) jid: String,
pub queue: String,
#[serde(rename = "jobtype")]
pub(crate) kind: String,
pub(crate) args: Vec<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub enqueued_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reserve_for: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub retry: Option<usize>,
pub priority: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub backtrace: Option<usize>,
#[serde(skip_serializing)]
pub(crate) failure: Option<Failure>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
#[serde(default = "HashMap::default")]
pub custom: HashMap<String, serde_json::Value>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Failure {
retry_count: usize,
failed_at: String,
#[serde(skip_serializing_if = "Option::is_none")] next_at: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] message: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "errtype")]
kind: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] backtrace: Option<Vec<String>>,
}
impl Job {
pub fn new<S, A>(kind: S, args: Vec<A>) -> Self
where
S: ToString,
A: Into<serde_json::Value>,
{
use rand::{thread_rng, Rng};
let random_jid = thread_rng().gen_ascii_chars().take(16).collect();
use chrono::prelude::*;
Job {
jid: random_jid,
queue: "default".into(),
kind: kind.to_string(),
args: args.into_iter().map(|s| s.into()).collect(),
created_at: Some(Utc::now()),
enqueued_at: None,
at: None,
reserve_for: Some(600),
retry: Some(25),
priority: Some(5),
backtrace: Some(0),
failure: None,
custom: Default::default(),
}
}
pub fn kind(&self) -> &str {
&self.kind
}
pub fn args(&self) -> &[serde_json::Value] {
&self.args
}
}
pub fn write_command<W: Write, C: FaktoryCommand>(w: &mut W, command: C) -> io::Result<()> {
command.issue::<W>(w)?;
w.flush()
}
pub fn write_command_and_await_ok<X: BufRead + Write, C: FaktoryCommand>(
x: &mut X,
command: C,
) -> io::Result<()> {
write_command(x, command)?;
read_ok(x)
}