Commit dd9c2759
dd9c27594565Merged
Scott Nixon <[email protected]> ยท 9 months ago ยท 1 file ยท ~1 modified
Description
[attractor-3jj] Refactor DocumentWatcher to be per-project with SQLite persistence
- Update DocumentWatcher::new to accept SqlitePool and project_id parameters
- Persist document changes to SQLite via spawned async tokio task
- Use Handle::current().spawn() to avoid blocking the notify watcher thread
- Keep SSE broadcast unchanged for live updates
- Files changes now update both SQLite (persistence) and broadcast channel (live updates)
- Still create .attractor dir if missing
Expected breakage in document_stream function (fixed in attractor-v19):
- Removed state.doc_watcher field access
- Removed state.attractor_dir field access
Details
- Commit
- dd9c27594565
- Parent
- 2dedda7bddf1
crates/attractor-web/src/server/documents.rsmodifiedView file
@@ -24,7 +24,19 @@
2424
2525
impl DocumentWatcher {2626
/// Start watching the `.attractor/` directory for PRD/Spec changes.27-
pub fn new(watch_dir: PathBuf) -> Result<Self, notify::Error> {27+
///28+
/// File updates are broadcast via the channel for SSE streaming, and also29+
/// persisted to SQLite via the provided database pool.30+
///31+
/// # Arguments32+
/// - `watch_dir`: Path to the .attractor directory to watch33+
/// - `db`: SQLite connection pool for persisting document changes34+
/// - `project_id`: Database ID of the project being watched35+
pub fn new(36+
watch_dir: PathBuf,37+
db: sqlx::SqlitePool,38+
project_id: i64,39+
) -> Result<Self, notify::Error> {2840
use notify::{Event as NotifyEvent, RecursiveMode, Watcher};2941
3042
let (sender, _) = broadcast::channel::<DocumentUpdate>(16);@@ -56,10 +68,35 @@
5668
content.as_ref().map_or(0, |c| c.len())5769
);5870
71+
// Broadcast via channel for live SSE updates5972
let _ = tx.send(DocumentUpdate {6073
doc_type: doc_type.to_string(),61-
content,74+
content: content.clone(),6275
});76+
77+
// Persist to SQLite asynchronously78+
if let Some(content_str) = content {79+
let db_pool = db.clone();80+
let doc_type_str = doc_type.to_string();81+
let project_id_copy = project_id;82+
83+
tokio::runtime::Handle::current().spawn(async move {84+
if let Err(e) = crate::server::db::upsert_document(85+
&db_pool,86+
project_id_copy,87+
&doc_type_str,88+
&content_str,89+
)90+
.await91+
{92+
tracing::error!(93+
"Failed to persist document {} to DB: {}",94+
doc_type_str,95+
e96+
);97+
}98+
});99+
}63100
}64101
}65102
})?;@@ -68,7 +105,7 @@
68105
std::fs::create_dir_all(&watch_dir).ok();69106
70107
watcher.watch(&watch_dir, RecursiveMode::NonRecursive)?;71-
tracing::info!("Watching {:?} for document changes", watch_dir);108+
tracing::info!("Watching {:?} for document changes (project_id: {})", watch_dir, project_id);72109
73110
Ok(DocumentWatcher {74111
sender,