use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_core::store::StoreAdmin;
use second_brain_sync::peers;
use crate::health::{DistillReport, Health, ProgressSeen, SyncReport};
pub const INTERVAL: Duration = Duration::from_secs(5 * 60);
pub struct ProgressCheck {
pub store: Arc<SqliteStore>,
pub data_dir: PathBuf,
pub health: Arc<Health>,
}
impl ProgressCheck {
pub async fn start(self) -> tokio::task::JoinHandle<()> {
let check = Arc::new(self);
check.clone().run_blocking().await;
tokio::spawn(async move {
let mut interval = tokio::time::interval(INTERVAL);
interval.tick().await;
loop {
interval.tick().await;
check.clone().run_blocking().await;
}
})
}
async fn run_blocking(self: Arc<Self>) {
if let Err(e) = tokio::task::spawn_blocking(move || self.run_once(Utc::now())).await {
tracing::error!("sync and distillation check panicked: {e}");
}
}
pub fn run_once(&self, now: DateTime<Utc>) {
let mut seen = ProgressSeen::default();
let mut errors = Vec::new();
match self.sync_seen() {
Ok(sync) => seen.sync = Some(sync),
Err(e) => errors.push(format!("{e:#}")),
}
match self.store.pending_distillation() {
Ok(p) => {
seen.distill = Some(DistillReport {
checked_at: None,
pending: p.count,
oldest_at: p.oldest_created_at,
})
}
Err(e) => errors.push(format!("reading raws pending distillation: {e:#}")),
}
if !errors.is_empty() {
let error = errors.join("; ");
tracing::warn!("sync and distillation check failed: {error}");
seen.error = Some(error);
}
self.health.progress_checked(seen, now);
}
fn sync_seen(&self) -> Result<SyncReport> {
let peers = peers::configured_peers(&self.data_dir)
.with_context(|| format!("reading {}", peers::PEERS_FILE))?;
let cursors = self
.store
.get_all_sync_states()
.context("reading sync state")?;
let last_success_at = peers::last_success(&self.data_dir)?;
Ok(SyncReport {
checked_at: None,
peers: peers.map(|p| p.len()),
synced_before: !cursors.is_empty(),
last_success_at,
last_cursor_at: cursors.iter().map(|c| c.last_sync_at).max(),
})
}
}