desktop: Follow seeding from other processes

Seeding or unseeding with the `rad` CLI or another instance of the app
writes the policies database without a node event, so the backend now
watches it like the notifications database.
This commit is contained in:
Rūdolfs Ošiņš
2026-11-20 19:32:17 +01:00
parent e0ccaedf35
commit 0b9394af9b
5 changed files with 98 additions and 40 deletions
@@ -3,7 +3,7 @@ use std::sync::Once;
use tauri::{AppHandle, Emitter, Manager};
use radicle::cob::cache::COBS_DB_FILE;
use radicle::node::{Handle, NOTIFICATIONS_DB_FILE, Node};
use radicle::node::{Handle, NOTIFICATIONS_DB_FILE, Node, POLICIES_DB_FILE};
use radicle_types::binaries::GitInfo;
use radicle_types::config::{Config, Version};
@@ -43,7 +43,16 @@ pub(crate) fn startup(app: AppHandle) -> Result<Config, Error> {
let cobs_db =
radicle_types::outbound::sqlite::Sqlite::reader(profile.cobs().join(COBS_DB_FILE))?;
crate::notifications::spawn(app.app_handle().clone(), inbox_db.clone());
crate::db_changes::spawn(
app.app_handle().clone(),
vec![
(
profile.node().join(NOTIFICATIONS_DB_FILE),
"notifications_changed",
),
(profile.node().join(POLICIES_DB_FILE), "policies_changed"),
],
);
let inbox_service = domain::inbox::service::Service::new(inbox_db);
let patch_service = domain::patch::service::Service::new(cobs_db.clone());
let issue_service = domain::issue::service::Service::new(cobs_db);
@@ -0,0 +1,74 @@
use std::path::PathBuf;
use std::sync::Once;
use std::time::Duration;
use radicle_types::outbound::sqlite::Sqlite;
use tauri::{AppHandle, Emitter};
/// Tells the frontend when another process changes a database the app reads,
/// by emitting the event named with it. The node's events don't cover these:
/// the `rad` CLI or another instance of the app may clear notifications or
/// change seeding policies, and the node writes notifications as it fetches.
pub(crate) fn spawn(app: AppHandle, databases: Vec<(PathBuf, &'static str)>) {
static WATCHING: Once = Once::new();
WATCHING.call_once(|| {
std::thread::spawn(move || {
let mut watched: Vec<Watched> = databases
.into_iter()
.map(|(path, event)| Watched {
path,
event,
db: None,
last: None,
failing: false,
})
.collect();
loop {
for watched in &mut watched {
watched.check(&app);
}
std::thread::sleep(Duration::from_secs(2));
}
});
});
}
struct Watched {
path: PathBuf,
event: &'static str,
/// Opened on first use, and again after a failure, such as when the
/// database doesn't exist yet.
db: Option<Sqlite>,
last: Option<i64>,
failing: bool,
}
impl Watched {
fn check(&mut self, app: &AppHandle) {
let version = match &self.db {
Some(db) => db.data_version(),
None => Sqlite::reader(&self.path).and_then(|db| {
let version = db.data_version();
self.db = Some(db);
version
}),
};
match version {
Ok(version) => {
if self.last.is_some_and(|last| last != version) {
let _ = app.emit(self.event, ());
}
self.last = Some(version);
self.failing = false;
}
// 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.failing = true;
}
}
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
mod commands;
mod notifications;
mod db_changes;
mod ref_updates;
use std::sync::atomic::{AtomicBool, Ordering};
@@ -1,37 +0,0 @@
use std::sync::Once;
use std::time::Duration;
use radicle_types::outbound::sqlite::Sqlite;
use tauri::{AppHandle, Emitter};
/// Tells the frontend when another process changes the notifications, such as
/// the `rad` CLI or another instance of the app clearing some. The node's
/// events only cover notifications it adds while fetching.
pub(crate) fn spawn(app: AppHandle, db: Sqlite) {
static WATCHING: Once = Once::new();
WATCHING.call_once(|| {
std::thread::spawn(move || {
let mut last = None;
let mut failing = false;
loop {
match db.data_version() {
Ok(version) => {
if last.is_some_and(|last| last != version) {
let _ = app.emit("notifications_changed", ());
}
last = Some(version);
failing = false;
}
// Logged once until it works again.
Err(e) => {
if !failing {
log::warn!("Not able to watch the notifications: {e}");
}
failing = true;
}
}
std::thread::sleep(Duration::from_secs(2));
}
});
});
}
+12
View File
@@ -43,6 +43,10 @@ const invalidate = batch(100, false);
// job updates often, so job lists refresh at most every few seconds.
const invalidateJobs = batch(5_000, true);
function isRepoInfo(key: Key): boolean {
return key[0] === "repo" && key[2] === "info";
}
function isJobs(key: Key): boolean {
return key[0] === "repo" && key[2] === "jobs";
}
@@ -104,6 +108,14 @@ export const createEventEmittersOnce = once(async (): Promise<UnlistenFn> => {
listen("notifications_changed", () => {
invalidate([keys.notifications()]);
}),
// Seeding or unseeding with the `rad` CLI or another instance of the app
// changes the repository list and each repository's seeded state.
listen("policies_changed", () => {
invalidate([keys.repos()]);
void queryClient.invalidateQueries({
predicate: query => isRepoInfo(query.queryKey as Key),
});
}),
// The backend may have missed events while it wasn't subscribed.
listen("ref_resync", () => {
clearEntityCaches();