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
pub mod timepoint;
use futures::future::join_all;
use tokio::time::sleep;
use self::timepoint::TimePoint;
use crate::{error::Error, task::Task};
#[derive(Debug)]
pub struct Job {
pub tasks: Vec<Task>,
pub refresh_time: Option<TimePoint>,
}
impl Job {
pub async fn run(&mut self) -> Result<(), Vec<Error>> {
loop {
let jobs = self.tasks.iter_mut().map(Task::run);
let results = join_all(jobs).await;
let errors = results
.into_iter()
.filter_map(|r| match r {
Ok(()) => None,
Err(e) => Some(e),
})
.collect::<Vec<_>>();
if !errors.is_empty() {
return Err(errors);
}
match &self.refresh_time {
Some(refresh_time) => {
let remaining_time = refresh_time.remaining_from_now();
tracing::debug!(
"Putting job to sleep for {}m",
remaining_time.as_secs() / 60
);
sleep(remaining_time).await;
}
None => return Ok(()),
}
}
}
}