Skip to main content

second_brain_api/
progress.rs

1use std::path::PathBuf;
2use std::sync::Arc;
3use std::time::Duration;
4
5use anyhow::{Context, Result};
6use chrono::{DateTime, Utc};
7use second_brain_core::sqlite_store::SqliteStore;
8use second_brain_core::store::StoreAdmin;
9use second_brain_sync::peers;
10
11use crate::health::{DistillReport, Health, ProgressSeen, SyncReport};
12
13pub const INTERVAL: Duration = Duration::from_secs(5 * 60);
14
15pub struct ProgressCheck {
16    pub store: Arc<SqliteStore>,
17    /// Where `sync-peers` and the sync success stamp live.
18    pub data_dir: PathBuf,
19    pub health: Arc<Health>,
20}
21
22impl ProgressCheck {
23    /// Runs one check before returning, so health holds a full reading
24    /// when the caller announces it, then checks every `INTERVAL`.
25    pub async fn start(self) -> tokio::task::JoinHandle<()> {
26        let check = Arc::new(self);
27        check.clone().run_blocking().await;
28        tokio::spawn(async move {
29            let mut interval = tokio::time::interval(INTERVAL);
30            interval.tick().await;
31            loop {
32                interval.tick().await;
33                check.clone().run_blocking().await;
34            }
35        })
36    }
37
38    async fn run_blocking(self: Arc<Self>) {
39        if let Err(e) = tokio::task::spawn_blocking(move || self.run_once(Utc::now())).await {
40            tracing::error!("sync and distillation check panicked: {e}");
41        }
42    }
43
44    pub fn run_once(&self, now: DateTime<Utc>) {
45        let mut seen = ProgressSeen::default();
46        let mut errors = Vec::new();
47        match self.sync_seen() {
48            Ok(sync) => seen.sync = Some(sync),
49            Err(e) => errors.push(format!("{e:#}")),
50        }
51        match self.store.pending_distillation() {
52            Ok(p) => {
53                seen.distill = Some(DistillReport {
54                    checked_at: None,
55                    pending: p.count,
56                    oldest_at: p.oldest_created_at,
57                })
58            }
59            Err(e) => errors.push(format!("reading raws pending distillation: {e:#}")),
60        }
61        if !errors.is_empty() {
62            let error = errors.join("; ");
63            tracing::warn!("sync and distillation check failed: {error}");
64            seen.error = Some(error);
65        }
66        self.health.progress_checked(seen, now);
67    }
68
69    fn sync_seen(&self) -> Result<SyncReport> {
70        let peers = peers::configured_peers(&self.data_dir)
71            .with_context(|| format!("reading {}", peers::PEERS_FILE))?;
72        let cursors = self
73            .store
74            .get_all_sync_states()
75            .context("reading sync state")?;
76        let last_success_at = peers::last_success(&self.data_dir)?;
77        Ok(SyncReport {
78            checked_at: None,
79            peers: peers.map(|p| p.len()),
80            synced_before: !cursors.is_empty(),
81            last_success_at,
82            last_cursor_at: cursors.iter().map(|c| c.last_sync_at).max(),
83        })
84    }
85}