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
171
use crate::{_prelude::*, task_filters, types::*};
pub(crate) struct TaskScheduler<JS: JobStateValues, TS: TaskStateValues, P: ParsedDocument> {
job: ResolvedJob<JS, TS, P>,
task_filters: TaskFilters<JS, TS>,
task_seq_num: usize,
pages_pending: usize,
tasks_tx: Sender<Arc<Task>>,
pub(crate) tasks_rx: Receiver<Arc<Task>>,
pub(crate) job_update_tx: Sender<JobUpdate<JS, TS>>,
job_update_rx: Receiver<JobUpdate<JS, TS>>,
update_tx: Sender<JobUpdate<JS, TS>>,
}
impl<JS: JobStateValues, TS: TaskStateValues, P: ParsedDocument> TaskScheduler<JS, TS, P> {
pub(crate) fn new(job: ResolvedJob<JS, TS, P>, update_tx: Sender<JobUpdate<JS, TS>>) -> TaskScheduler<JS, TS, P> {
let (job_update_tx, job_update_rx) = unbounded_ch::<JobUpdate<JS, TS>>();
let (tasks_tx, tasks_rx) = unbounded_ch::<Arc<Task>>();
TaskScheduler {
task_filters: job.rules.task_filters(),
job,
task_seq_num: 0,
pages_pending: 0,
tasks_tx,
tasks_rx,
job_update_tx,
job_update_rx,
update_tx,
}
}
fn schedule(&mut self, task: Arc<Task>) {
self.pages_pending += 1;
let r = self.tasks_tx.send(task);
if r.is_err() {
panic!("cannot send task to tasks_tx! should never ever happen!")
}
}
fn schedule_filter(&mut self, task: &mut Task) -> task_filters::Result {
let action = if task.link.redirect > 0 { "[scheduling redirect]" } else { "[scheduling]" };
let _span = span!(task = %task).entered();
for filter in &mut self.task_filters {
match filter.accept(&mut self.job.ctx, self.task_seq_num, task) {
Ok(task_filters::Action::Accept) => continue,
Ok(task_filters::Action::Skip) => {
if task.is_root() {
info!(action = "skip", filter_name = %filter.name(), action);
} else {
debug!(action = "skip", filter_name = %filter.name(), action);
}
return Ok(task_filters::Action::Skip)
}
Err(ExtError::Term { reason }) => {
if task.is_root() {
info!(action = "term", filter_name = %filter.name(), reason = reason, action);
} else {
debug!(action = "term", filter_name = %filter.name(), reason = reason, action);
}
return Err(ExtError::Term { reason })
}
Err(ExtError::Other(err)) => {
debug!(filter_name = %filter.name(), "error during task filtering: {:#}", err);
continue
}
}
}
debug!(action = "scheduled", action);
Ok(task_filters::Action::Accept)
}
async fn process_task_response(&mut self, task_response: JobUpdate<JS, TS>, ignore_links: bool) {
self.pages_pending -= 1;
self.task_seq_num += 1;
let mut links = self.job.ctx.consume_links();
if let (JobStatus::Processing(Ok(ref r)), false) = (&task_response.status, ignore_links) {
links.extend(r.links.clone());
}
let tasks: Vec<_> = links
.iter()
.filter_map(|link| Task::new(Arc::clone(link), &task_response.task).ok())
.map(|mut task| (self.schedule_filter(&mut task), task))
.take_while(|(r, _)| {
if let Err(ExtError::Term { reason: _ }) = r {
return false
}
true
})
.filter_map(|(r, task)| {
if let Ok(task_filters::Action::Skip) = r {
return None
}
Some(task)
})
.collect();
for task in tasks {
self.schedule(Arc::new(task));
}
let _ = self.update_tx.send_async(task_response).await;
}
pub(crate) fn go<'a>(mut self) -> Result<TaskFut<'a, JobUpdate<JS, TS>>> {
let mut root_task = Task::new_root(&self.job.url)?;
Ok(TracingTask::new(span!(url = %self.job.url), async move {
trace!(
soft_timeout_ms = self.job.settings.job_soft_timeout.as_millis() as u32,
hard_timeout_ms = self.job.settings.job_hard_timeout.as_millis() as u32,
"Starting..."
);
let r = self.schedule_filter(&mut root_task);
let root_task = Arc::new(root_task);
if let Ok(task_filters::Action::Accept) = r {
self.schedule(Arc::clone(&root_task));
}
let mut is_soft_timeout = false;
let mut is_hard_timeout = false;
let mut timeout = self.job.ctx.timeout_remaining(*self.job.settings.job_soft_timeout);
while self.pages_pending > 0 {
tokio::select! {
res = self.job_update_rx.recv_async() => {
self.process_task_response(res.unwrap(), is_soft_timeout).await;
}
_ = &mut timeout => {
if is_soft_timeout {
is_hard_timeout = true;
break
}
is_soft_timeout = true;
let jitter_ms = {
let mut rng = thread_rng();
Duration::from_millis(rng.gen_range(0..self.job.settings.job_hard_timeout_jitter.as_millis()) as u64)
};
timeout = self.job.ctx.timeout_remaining(*self.job.settings.job_hard_timeout + jitter_ms);
}
}
}
trace!("Finishing..., pages remaining = {}", self.pages_pending);
let status = JobStatus::Finished(if is_hard_timeout {
Err(JobError::JobFinishedByHardTimeout)
} else if is_soft_timeout {
Err(JobError::JobFinishedBySoftTimeout)
} else {
Ok(JobFinished {})
});
Ok(JobUpdate { task: root_task, status, ctx: self.job.ctx })
})
.instrument())
}
}