use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::task::JoinHandle;
use super::{Client, EventQueue, SessionSetup, lock};
use crate::failure::{FailureCategory, NOT_RESPONDING, TurnFailure};
#[path = "watchdog/fence.rs"]
pub(super) mod fence;
pub(super) use fence::Activities;
#[derive(Clone, Copy, Debug)]
pub(super) struct Timing {
pub(super) stall_after: Duration,
pub(super) ping_timeout: Duration,
pub(super) check_every: Duration,
}
pub(super) const TIMING: Timing = Timing {
stall_after: Duration::from_secs(90),
ping_timeout: Duration::from_secs(15),
check_every: Duration::from_secs(15),
};
pub(super) struct Watch {
pub(super) client: Client,
pub(super) activities: Activities,
pub(super) events: Arc<EventQueue>,
pub(super) runtime_version: Option<String>,
pub(super) timing: Timing,
}
pub(super) struct Watchdog(JoinHandle<()>);
impl Drop for Watchdog {
fn drop(&mut self) {
self.0.abort();
}
}
pub(super) fn start(client: Client, setup: &SessionSetup, events: &Arc<EventQueue>) -> Watchdog {
spawn(Watch {
client,
activities: setup.activity.clone(),
events: events.clone(),
runtime_version: setup.runtime_version.clone(),
timing: TIMING,
})
}
pub(super) fn spawn(watch: Watch) -> Watchdog {
Watchdog(tokio::spawn(run(watch)))
}
async fn run(watch: Watch) {
let mut interval = tokio::time::interval(watch.timing.check_every);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
interval.tick().await;
check(&watch).await;
}
}
async fn check(watch: &Watch) {
let stall_after = watch.timing.stall_after;
let observed = fence::observe(&lock(&watch.activities), Instant::now(), stall_after);
if observed.is_empty() {
return;
}
let answered = matches!(
tokio::time::timeout(watch.timing.ping_timeout, watch.client.ping(None)).await,
Ok(Ok(_))
);
resolve(watch, &observed, answered).await;
}
async fn resolve(watch: &Watch, observed: &[(crate::ChatId, u64)], answered: bool) {
let now = Instant::now();
if answered {
fence::mark_alive(&mut lock(&watch.activities), observed, now);
return;
}
let ended = fence::expire(
&mut lock(&watch.activities),
observed,
now,
watch.timing.stall_after,
);
for (chat, turn) in ended {
let failure = TurnFailure::stopped(
FailureCategory::NotResponding,
NOT_RESPONDING,
&turn,
watch.runtime_version.as_deref(),
);
super::chat::end_reported(&watch.events, chat, &turn, failure).await;
}
}
#[cfg(test)]
#[path = "watchdog/tests.rs"]
mod tests;