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
|
//! The scheduler tick (§4: "one tick thread + sweep over `next_check_at`").
//!
//! Blocking is the right shape here: the tick sleeps between sweeps and each
//! sweep is a sequence of blocking git and HTTP calls.
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread::JoinHandle;
use crate::config::Config;
use crate::db::{self, Db};
use crate::watch::detect::check_pkgbase;
/// Spawn the watcher tick thread.
pub fn spawn_scheduler(cfg: Arc<Config>, db: Db, stop: Arc<AtomicBool>) -> JoinHandle<()> {
std::thread::Builder::new()
.name("watcher".into())
.spawn(move || {
while !stop.load(Ordering::SeqCst) {
let tick = crate::db::setting_i64(&db, "poll_tick", 30);
sweep(&cfg, &db);
// Sleep in small slices so shutdown is prompt.
let mut slept = 0;
while slept < tick && !stop.load(Ordering::SeqCst) {
std::thread::sleep(std::time::Duration::from_secs(1));
slept += 1;
}
}
})
.expect("spawn watcher")
}
/// One sweep over everything that is due.
pub fn sweep(cfg: &Config, db: &Db) -> usize {
let now = crate::time::now();
let due = match db::due_subscriptions(db, &now) {
Ok(d) => d,
Err(_) => return 0,
};
let mut n = 0;
for sub in due {
// Mark it checked first so a slow or hanging source cannot make the
// scheduler re-enter it on the next tick.
let interval = sub
.poll_interval_secs
.unwrap_or(crate::db::setting_i64(db, "poll_interval", 86400));
let _ = db::note_check(&db.writer, &sub.pkgbase, &crate::time::secs_ahead(interval), None);
match check_pkgbase(cfg, db, &sub.pkgbase) {
Ok(report) => {
if matches!(report.outcome, crate::watch::detect::Outcome::Blocked(_)) {
let _ = db::note_check(
&db.writer,
&sub.pkgbase,
&crate::time::secs_ahead(600),
Some(&format!("{:?}", report.outcome)),
);
}
}
Err(e) => {
let _ = db::note_check(
&db.writer,
&sub.pkgbase,
&crate::time::secs_ahead(600),
Some(&e.to_string()),
);
}
}
n += 1;
}
n
}
|