diff --git a/desktop/crates/radicle-tauri/src/commands/startup.rs b/desktop/crates/radicle-tauri/src/commands/startup.rs index 811d860fd..2aca2465e 100644 --- a/desktop/crates/radicle-tauri/src/commands/startup.rs +++ b/desktop/crates/radicle-tauri/src/commands/startup.rs @@ -51,6 +51,10 @@ pub(crate) fn startup(app: AppHandle) -> Result { "notifications_changed", ), (profile.node().join(POLICIES_DB_FILE), "policies_changed"), + // Writes made without announcing them, such as with + // `rad issue comment --no-announce` or by another instance of the + // app, don't reach the node's events. + (profile.cobs().join(COBS_DB_FILE), "cobs_changed"), ], ); let inbox_service = domain::inbox::service::Service::new(inbox_db); diff --git a/desktop/crates/radicle-tauri/src/db_changes.rs b/desktop/crates/radicle-tauri/src/db_changes.rs index fa16fe7f4..2912d0f87 100644 --- a/desktop/crates/radicle-tauri/src/db_changes.rs +++ b/desktop/crates/radicle-tauri/src/db_changes.rs @@ -15,17 +15,13 @@ pub(crate) fn spawn(app: AppHandle, databases: Vec<(PathBuf, &'static str)>) { std::thread::spawn(move || { let mut watched: Vec = databases .into_iter() - .map(|(path, event)| Watched { - path, - event, - db: None, - last: None, - failing: false, - }) + .map(|(path, event)| Watched::new(path, event)) .collect(); loop { for watched in &mut watched { - watched.check(&app); + if watched.changed() { + let _ = app.emit(watched.event, ()); + } } std::thread::sleep(Duration::from_secs(2)); } @@ -44,10 +40,25 @@ struct Watched { } impl Watched { - fn check(&mut self, app: &AppHandle) { + fn new(path: PathBuf, event: &'static str) -> Self { + Self { + path, + event, + db: None, + last: None, + failing: false, + } + } + + /// Whether the database changed since the previous check. A database + /// that opens after failing counts as changed: `data_version` only + /// compares within one connection, so what happened in between is + /// unknown. + fn changed(&mut self) -> bool { + let opened = self.db.is_none(); let version = match &self.db { Some(db) => db.data_version(), - None => Sqlite::reader(&self.path).and_then(|db| { + None => Sqlite::watcher(&self.path).and_then(|db| { let version = db.data_version(); self.db = Some(db); version @@ -55,20 +66,105 @@ impl Watched { }; match version { Ok(version) => { - if self.last.is_some_and(|last| last != version) { - let _ = app.emit(self.event, ()); - } + let changed = if opened { + self.failing + } else { + self.last != Some(version) + }; self.last = Some(version); self.failing = false; + changed } - // Logged once until it works again. Err(e) => { if !self.failing { log::warn!("Not able to watch for `{}`: {e}", self.event); } self.db = None; + self.last = None; self.failing = true; + false } } } } + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use std::path::PathBuf; + use std::str::FromStr; + + use radicle::identity::RepoId; + use radicle::node::policy::Scope; + use radicle::node::policy::store::Store; + + use super::Watched; + + struct TempDir(PathBuf); + + impl TempDir { + fn new(name: &str) -> Self { + let dir = std::env::temp_dir().join(format!( + "radicle-tauri-db-changes-{name}-{}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + Self(dir) + } + } + + impl Drop for TempDir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + + fn seed(store: &mut Store, rid: &str) { + store + .seed(&RepoId::from_str(rid).unwrap(), Scope::All) + .unwrap(); + } + + #[test] + fn emits_on_changes_but_not_on_first_sight() { + let dir = TempDir::new("changes"); + let path = dir.0.join("policies.db"); + let mut store = Store::open(&path).unwrap(); + let mut watched = Watched::new(path, "policies_changed"); + + assert!(!watched.changed()); + assert!(!watched.changed()); + seed(&mut store, "rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5"); + assert!(watched.changed()); + assert!(!watched.changed()); + } + + #[test] + fn emits_once_when_a_missing_database_appears() { + let dir = TempDir::new("missing"); + let path = dir.0.join("policies.db"); + let mut watched = Watched::new(path.clone(), "policies_changed"); + + assert!(!watched.changed()); + assert!(!watched.changed()); + Store::open(&path).unwrap(); + assert!(watched.changed()); + assert!(!watched.changed()); + } + + #[test] + fn emits_once_when_it_works_again_after_failing() { + let dir = TempDir::new("failing"); + let path = dir.0.join("policies.db"); + std::fs::write(&path, b"not a database").unwrap(); + let mut watched = Watched::new(path.clone(), "policies_changed"); + + assert!(!watched.changed()); + assert!(watched.failing); + std::fs::remove_file(&path).unwrap(); + Store::open(&path).unwrap(); + assert!(watched.changed()); + assert!(!watched.changed()); + } +} diff --git a/desktop/crates/radicle-types/src/outbound/sqlite.rs b/desktop/crates/radicle-types/src/outbound/sqlite.rs index 44bf98165..b23149e48 100644 --- a/desktop/crates/radicle-types/src/outbound/sqlite.rs +++ b/desktop/crates/radicle-types/src/outbound/sqlite.rs @@ -34,11 +34,21 @@ impl Sqlite { const DB_READ_TIMEOUT: time::Duration = time::Duration::from_secs(3); pub fn reader>(path: P) -> Result { + Self::read_only(path, Self::DB_READ_TIMEOUT) + } + + /// A reader that gives up quickly on a locked database, for polling + /// several databases without one holding up the others. + pub fn watcher>(path: P) -> Result { + Self::read_only(path, time::Duration::from_millis(100)) + } + + fn read_only>(path: P, timeout: time::Duration) -> Result { let mut db = sql::Connection::open_thread_safe_with_flags( path, sqlite::OpenFlags::new().with_read_only(), )?; - db.set_busy_timeout(Self::DB_READ_TIMEOUT.as_millis() as usize)?; + db.set_busy_timeout(timeout.as_millis() as usize)?; Ok(Self { db: Arc::new(db) }) } diff --git a/desktop/src/lib/cacheActivity.ts b/desktop/src/lib/cacheActivity.ts new file mode 100644 index 000000000..3322148b2 --- /dev/null +++ b/desktop/src/lib/cacheActivity.ts @@ -0,0 +1,11 @@ +// When the cache last heard of a change from the node or the user's own write. +// A change to the node's COB cache soon after is most likely the same one. +let lastHeard = 0; + +export function heardOfChange(): void { + lastHeard = Date.now(); +} + +export function heardOfChangeWithin(ms: number): boolean { + return Date.now() - lastHeard < ms; +} diff --git a/desktop/src/lib/invoke.ts b/desktop/src/lib/invoke.ts index b6b0303f7..b6e7cff8f 100644 --- a/desktop/src/lib/invoke.ts +++ b/desktop/src/lib/invoke.ts @@ -19,6 +19,7 @@ import { cached } from "@radicle-gui/ui/lib/cached"; import { invalidate } from "@radicle-gui/ui/lib/query.svelte"; import * as tauri from "@tauri-apps/api/core"; +import { heardOfChange } from "@app/lib/cacheActivity"; import { queryClient } from "@app/lib/query"; import { writtenKeys } from "@app/lib/writes"; @@ -34,7 +35,10 @@ export async function invoke( } finally { // Also after a failure: a command can fail after writing part of a change. const written = writtenKeys(cmd, args, result); - if (written.length > 0) clearEntityCaches(); + if (written.length > 0) { + clearEntityCaches(); + heardOfChange(); + } for (const queryKey of written) { void invalidate(queryClient, { queryKey }); } diff --git a/desktop/src/lib/startup.svelte.ts b/desktop/src/lib/startup.svelte.ts index 890cad629..f76b32cf0 100644 --- a/desktop/src/lib/startup.svelte.ts +++ b/desktop/src/lib/startup.svelte.ts @@ -2,13 +2,19 @@ import type { Key } from "@radicle-gui/core/keys"; import type { RefUpdate } from "@radicle-gui/core/refUpdates"; import type { UnlistenFn } from "@tauri-apps/api/event"; -import { isJobsKey, isRepoInfoKey, keys } from "@radicle-gui/core/keys"; +import { + isCobCacheKey, + isJobsKey, + isRepoInfoKey, + keys, +} from "@radicle-gui/core/keys"; import { affectedKeys } from "@radicle-gui/core/refUpdates"; import { invalidate } from "@radicle-gui/ui/lib/query.svelte"; import { listen } from "@tauri-apps/api/event"; import once from "lodash/once"; import { get } from "svelte/store"; +import { heardOfChange, heardOfChangeWithin } from "@app/lib/cacheActivity"; import type { RepoList } from "@app/lib/cobQueries"; import { clearEntityCaches, invoke } from "@app/lib/invoke"; import { queryClient } from "@app/lib/query"; @@ -59,6 +65,7 @@ function listed(rid: string): boolean { } export function onRefUpdate(update: RefUpdate) { + heardOfChange(); const affected = affectedKeys(update); // A key for the whole repository covers its jobs too. const repoWide = affected.some(key => key.length === 2 && key[0] === "repo"); @@ -78,6 +85,34 @@ export function onRefUpdate(update: RefUpdate) { ]); } +function refreshCobCache(): void { + clearEntityCaches(); + void invalidate(queryClient, { + predicate: query => isCobCacheKey(query.queryKey as Key), + }); +} + +let deferredCobRefresh: ReturnType | undefined; + +// Issues and patches written without announcing them, such as with +// `rad issue comment --no-announce`, only show in the node's COB cache. It +// also changes with every fetch and write the cache already heard of, and +// says nothing about which repository changed. So right after such a change +// it refreshes once things are quiet, rather than with every fetch, and never +// leaves a change out. +export function onCobsChanged(): void { + if (!heardOfChangeWithin(COB_QUIET_MS)) { + refreshCobCache(); + return; + } + deferredCobRefresh ??= setTimeout(() => { + deferredCobRefresh = undefined; + refreshCobCache(); + }, COB_QUIET_MS); +} + +const COB_QUIET_MS = 5_000; + export let unlistenNodeEvents: UnlistenFn | undefined = undefined; export function setUnlistenNodeEvents(unlisten: UnlistenFn) { @@ -116,6 +151,7 @@ export const createEventEmittersOnce = once(async (): Promise => { listen("notifications_changed", () => { invalidateBatched([keys.notifications()]); }), + listen("cobs_changed", onCobsChanged), listen("policies_changed", () => { invalidateBatched([keys.repos()]); void invalidate(queryClient, { diff --git a/desktop/tests/unit/eventBatch.test.ts b/desktop/tests/unit/eventBatch.test.ts index e4a191dea..91723a067 100644 --- a/desktop/tests/unit/eventBatch.test.ts +++ b/desktop/tests/unit/eventBatch.test.ts @@ -14,7 +14,7 @@ import { import { queryClient } from "@app/lib/query"; import { repoListScope } from "@app/lib/repoListScope"; -import { batch, onRefUpdate } from "@app/lib/startup.svelte"; +import { batch, onCobsChanged, onRefUpdate } from "@app/lib/startup.svelte"; const { clearEntityCaches } = vi.hoisted(() => ({ clearEntityCaches: vi.fn(), @@ -165,3 +165,30 @@ describe("the repository list", () => { test("the core's ref update matches the one the backend sends", () => { expectTypeOf().toEqualTypeOf(); }); + +describe("the COB cache", () => { + // The COB cache refresh is the only invalidation by predicate alone. + function cobInvalidations() { + return invalidateQueries.mock.calls.filter( + call => call[0] !== undefined && call[0].queryKey === undefined, + ); + } + + test("refreshes right away for a change nothing else told of", () => { + vi.advanceTimersByTime(10_000); + onCobsChanged(); + expect(cobInvalidations()).toHaveLength(1); + }); + + test("refreshes once things are quiet after a change it heard of", () => { + vi.advanceTimersByTime(10_000); + onRefUpdate({ rid, refs: [`refs/namespaces/${nid}/refs/heads/main`] }); + invalidateQueries.mockClear(); + + onCobsChanged(); + onCobsChanged(); + expect(cobInvalidations()).toHaveLength(0); + vi.advanceTimersByTime(5_000); + expect(cobInvalidations()).toHaveLength(1); + }); +});