desktop: Watch the node's COB cache for changes

Issues and patches written without announcing them, such as with
`rad issue comment --no-announce` or by another instance of the app,
never reached the open views. The cache also changes with every fetch and
write the app already heard of, and doesn't say which repo changed, so
right after those it refreshes once things are quiet instead of with each
one. The watchers now also notice a database that appears or comes back
after an error, and one locked database no longer holds up the others.
This commit is contained in:
Rūdolfs Ošiņš
2026-11-20 19:32:17 +01:00
parent 05d1a99fef
commit f87cbcfc94
7 changed files with 206 additions and 18 deletions
@@ -51,6 +51,10 @@ pub(crate) fn startup(app: AppHandle) -> Result<Config, Error> {
"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);
+110 -14
View File
@@ -15,17 +15,13 @@ pub(crate) fn spawn(app: AppHandle, databases: Vec<(PathBuf, &'static str)>) {
std::thread::spawn(move || {
let mut watched: Vec<Watched> = 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<radicle::node::policy::store::Write>, 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());
}
}
@@ -34,11 +34,21 @@ impl Sqlite {
const DB_READ_TIMEOUT: time::Duration = time::Duration::from_secs(3);
pub fn reader<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
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<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
Self::read_only(path, time::Duration::from_millis(100))
}
fn read_only<P: AsRef<Path>>(path: P, timeout: time::Duration) -> Result<Self, Error> {
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) })
}
+11
View File
@@ -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;
}
+5 -1
View File
@@ -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<T = null>(
} 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 });
}
+37 -1
View File
@@ -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<typeof setTimeout> | 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<UnlistenFn> => {
listen("notifications_changed", () => {
invalidateBatched([keys.notifications()]);
}),
listen("cobs_changed", onCobsChanged),
listen("policies_changed", () => {
invalidateBatched([keys.repos()]);
void invalidate(queryClient, {
+28 -1
View File
@@ -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<RefUpdate>().toEqualTypeOf<BackendRefUpdate>();
});
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);
});
});