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
170
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::{Mutex};
use std::thread;
use std::time::Duration;
use time;
use crate::backend::Backend;
use crate::helpers::{TaskProgress, TaskReport};
use crate::models::Service;
pub fn mark_done_arc(
backend: &Backend,
reports_arc: &Arc<Mutex<Vec<TaskReport>>>,
) -> Result<bool, String>
{
let mut mutex_guard = reports_arc
.lock()
.unwrap_or_else(|_| panic!("Failed to obtain Mutex lock in drain_shared_vec"));
let reports : Vec<TaskReport> = (*mutex_guard).drain(..).collect();
if !reports.is_empty() {
let request_time = time::get_time();
let mut success = false;
if let Err(e) = backend.mark_done(&reports) {
println!("-- mark_done attempt failed: {:?}", e);
let mut retries = 0;
while retries < 3 {
thread::sleep(Duration::new(2, 0));
retries += 1;
match backend.mark_done(&reports) {
Ok(_) => {
success = true;
break;
},
Err(e) => println!("-- mark_done retry failed: {:?}", e),
};
}
} else {
success = true;
}
if !success {
return Err(String::from(
"Database ran away during mark_done persisting.",
));
}
let responded_time = time::get_time();
let request_duration = (responded_time - request_time).num_milliseconds();
println!(
"finalize: reporting tasks to DB took {}ms.",
request_duration
);
Ok(true)
} else {
Ok(false)
}
}
pub fn push_done_queue(reports_arc: &Arc<Mutex<Vec<TaskReport>>>, report: TaskReport) {
let mut reports = reports_arc
.lock()
.unwrap_or_else(|_| panic!("Failed to obtain Mutex lock in push_done_queue"));
if reports.len() > 10_000 {
panic!(
"Done queue is too large: {:?} tasks. Stop the sink!",
reports.len()
);
}
reports.push(report)
}
pub fn timeout_progress_tasks<S: ::std::hash::BuildHasher>(
progress_queue_arc: &Arc<Mutex<HashMap<i64, TaskProgress, S>>>,
) -> Vec<TaskProgress> {
let mut progress_queue = progress_queue_arc
.lock()
.unwrap_or_else(|_| panic!("Failed to obtain Mutex lock in timeout_progress_tasks"));
let now = time::get_time().sec;
let expired_keys = progress_queue
.iter()
.filter(|&(_, v)| v.expected_at() < now)
.map(|(k, _)| *k)
.collect::<Vec<_>>();
let mut expired_tasks = Vec::new();
for key in expired_keys {
match progress_queue.remove(&key) {
None => {},
Some(task_progress) => expired_tasks.push(task_progress),
}
}
expired_tasks
}
pub fn pop_progress_task<S: ::std::hash::BuildHasher>(
progress_queue_arc: &Arc<Mutex<HashMap<i64, TaskProgress, S>>>,
taskid: i64,
) -> Option<TaskProgress>
{
if taskid < 0 {
return None;
}
let mut progress_queue = progress_queue_arc
.lock()
.unwrap_or_else(|_| panic!("Failed to obtain Mutex lock in pop_progress_task"));
progress_queue.remove(&taskid)
}
pub fn push_progress_task<S: ::std::hash::BuildHasher>(
progress_queue_arc: &Arc<Mutex<HashMap<i64, TaskProgress, S>>>,
progress_task: TaskProgress,
)
{
let mut progress_queue = progress_queue_arc
.lock()
.unwrap_or_else(|_| panic!("Failed to obtain Mutex lock in push_progress_task"));
if progress_queue.len() > 10_000 {
panic!(
"Progress queue is too large: {:?} tasks. Stop the ventilator!",
progress_queue.len()
);
}
progress_queue.insert(progress_task.task.id, progress_task);
}
pub fn get_sync_service<S: ::std::hash::BuildHasher>(
service_name: &str,
services: &Arc<Mutex<HashMap<String, Option<Service>, S>>>,
backend: &Backend,
) -> Option<Service>
{
let mut services = services
.lock()
.unwrap_or_else(|_| panic!("Failed to obtain Mutex lock in get_sync_services"));
services
.entry(service_name.to_string())
.or_insert_with(
|| match Service::find_by_name(service_name, &backend.connection) {
Ok(s) => Some(s),
_ => None,
},
)
.clone()
}
pub fn get_service<S: ::std::hash::BuildHasher>(
service_name: &str,
services: &Arc<Mutex<HashMap<String, Option<Service>, S>>>,
) -> Option<Service>
{
let services = services
.lock()
.unwrap_or_else(|_| panic!("Failed to obtain Mutex lock in get_service"));
match services.get(service_name) {
None => None,
Some(service) => service.clone(),
}
}