desktop: Refresh CI status on job updates

Every status chip of a repository shares one list of its jobs, which
refreshes when a job changes, at most every few seconds, since listing
them scans every job of the repository.
This commit is contained in:
Rūdolfs Ošiņš
2026-11-20 19:32:16 +01:00
parent 8ac86d4f2c
commit cbdd3d2d86
11 changed files with 258 additions and 38 deletions
@@ -1,4 +1,3 @@
use radicle::git;
use radicle::identity::RepoId;
use radicle_types::cobs::job;
use radicle_types::error::Error;
@@ -11,7 +10,6 @@ use crate::commands::blocking;
pub async fn list_jobs(
ctx: tauri::State<'_, AppState>,
rid: RepoId,
sha: git::Oid,
) -> Result<Vec<job::Job>, Error> {
blocking(ctx, move |ctx| ctx.list_jobs(rid, sha)).await
blocking(ctx, move |ctx| ctx.list_jobs(rid)).await
}
+87 -3
View File
@@ -1,6 +1,6 @@
use radicle::cob::store::access::ReadOnly;
use radicle::identity;
use radicle::storage::ReadStorage;
use radicle::{git, identity};
use radicle_job::{Job, JobId, Jobs as JobsStore};
use crate::cobs::job;
@@ -8,13 +8,18 @@ use crate::error::Error;
use crate::traits::Profile;
pub trait Jobs: Profile {
fn list_jobs(&self, rid: identity::RepoId, sha: git::Oid) -> Result<Vec<job::Job>, Error> {
/// Every job of the repository that has runs. Finding one commit's jobs
/// scans them all anyway, so views share one scan and pick their commits.
fn list_jobs(&self, rid: identity::RepoId) -> Result<Vec<job::Job>, Error> {
let profile = self.profile();
let repo = profile.storage.repository(rid)?;
let aliases = &profile.aliases();
let jobs = JobsStore::open(&repo, ReadOnly)?;
let found_jobs: Result<Vec<(JobId, Job)>, _> = jobs.find_by_commit(sha)?.collect();
let found_jobs: Result<Vec<(JobId, Job)>, _> = jobs
.all()?
.map(|job| job.map(|(id, job)| (JobId::from(id), job)))
.collect();
Ok(found_jobs?
.into_iter()
@@ -23,3 +28,82 @@ pub trait Jobs: Profile {
.collect())
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
use std::convert::Infallible;
use radicle::cob::store::access::WriteAs;
use radicle::cob::{self, Contents, Create, Entry, Evaluate, Version};
use radicle::crypto::{Seed, SigningKey};
use radicle::git::Oid;
use radicle::storage::ReadStorage;
use radicle::test::fixtures;
use radicle_job::{Jobs as JobsStore, TYPENAME};
use crate::traits::job::Jobs;
use crate::{AppState, test};
/// Accepts any contents, to write a job that the job store can't load.
struct Raw;
impl<R> Evaluate<R> for Raw {
type Error = Infallible;
fn init(_: &Entry, _: &R) -> Result<Self, Self::Error> {
Ok(Raw)
}
fn apply<'a, I: Iterator<Item = (&'a Oid, &'a Entry)>>(
&mut self,
_: &Entry,
_: I,
_: &R,
) -> Result<(), Self::Error> {
Ok(())
}
}
#[test]
fn list_jobs_skips_jobs_that_cannot_be_loaded() {
let tmp = tempfile::tempdir().unwrap();
let profile = test::profile(&tmp.path().join("home"), [0xff; 32]);
let signer = SigningKey::from_seed(Seed::new([0xff; 32]));
let (rid, _, _, head) =
fixtures::project(tmp.path().join("working"), &profile.storage, &signer).unwrap();
let repo = profile.storage.repository(rid).unwrap();
let mut jobs = JobsStore::open(&repo, WriteAs::new(&signer)).unwrap();
let mut job = jobs.create(head.into()).unwrap();
job.run(
serde_json::from_str("\"67e55044-10b1-426f-9247-bb680e5fe0c8\"").unwrap(),
"https://ci.example/1".parse().unwrap(),
)
.unwrap();
let id = job.id;
cob::create::<Raw, _>(
&repo,
&signer,
None,
vec![],
&profile.public_key,
Create {
contents: Contents::new(b"not a job action".to_vec()),
type_name: TYPENAME.clone(),
message: "Broken job".to_string(),
embeds: vec![],
version: Version::default(),
},
)
.unwrap();
let state = AppState { profile };
let listed = state.list_jobs(rid).unwrap();
assert_eq!(
listed.iter().map(|job| job.job_id).collect::<Vec<_>>(),
vec![id]
);
}
}
+2 -6
View File
@@ -1745,14 +1745,10 @@ async fn delete_patch_handler(
#[derive(Serialize, Deserialize)]
struct JobsBody {
pub rid: identity::RepoId,
pub sha: git::Oid,
}
async fn jobs_handler(
Ctx(ctx): Ctx,
Json(JobsBody { rid, sha }): Json<JobsBody>,
) -> impl IntoResponse {
let jobs = ctx.list_jobs(rid, sha)?;
async fn jobs_handler(Ctx(ctx): Ctx, Json(JobsBody { rid }): Json<JobsBody>) -> impl IntoResponse {
let jobs = ctx.list_jobs(rid)?;
Ok::<_, Error>(Json(jobs))
}
+23 -6
View File
@@ -1,4 +1,8 @@
<script lang="ts">
import type { Job } from "@bindings/repo/Job";
import { keys } from "@radicle-gui/core/keys";
import { createQuery } from "@radicle-gui/ui/lib/query.svelte";
import { SvelteSet } from "svelte/reactivity";
import type { RunView, Status } from "@app/lib/ciJobs";
@@ -8,7 +12,9 @@
statusLabel,
totalCounts,
} from "@app/lib/ciJobs";
import { cachedListJobs } from "@app/lib/invoke";
import { nodeEvents } from "@app/lib/events";
import { listJobs } from "@app/lib/invoke";
import { queryClient } from "@app/lib/query";
import { authorForNodeId } from "@app/lib/utils";
import Button from "@app/components/Button.svelte";
@@ -28,9 +34,19 @@
const collapsed = new SvelteSet<string>();
let popoverExpanded: boolean = $state(false);
// Held in a `$derived`; see `ReviewCodeThread`. The TTL here means a re-run
// would refetch and blank the chip.
const jobsPromise = $derived(cachedListJobs(rid, commit).catch(() => []));
// Listing a commit's jobs scans every job of the repository, so the cache
// keeps one list of them all, shared by every chip of the repository and
// kept across navigations. Job updates from the node refresh it; without
// them, it is refetched on mount once it is 30 seconds old.
const jobsQuery = createQuery<Job[]>(queryClient, () => ({
queryKey: keys.jobs(rid),
queryFn: () => listJobs(rid),
staleTime: $nodeEvents ? Infinity : 30_000,
}));
const loadedJobs = $derived(
jobsQuery.current.data?.filter(job => job.commit === commit) ??
(jobsQuery.current.isError ? [] : undefined),
);
function toggleNode(key: string) {
if (collapsed.has(key)) {
@@ -145,7 +161,8 @@
}
</style>
{#await jobsPromise then jobs}
{#if loadedJobs}
{@const jobs = loadedJobs}
{#if jobs.length > 0}
{@const groups = groupJobs(jobs)}
{@const overallCounts = totalCounts(groups)}
@@ -262,4 +279,4 @@
{/snippet}
</Popover>
{/if}
{/await}
{/if}
+4 -12
View File
@@ -196,19 +196,11 @@ export const cachedRepoCommitCount = cached(
{ max: 5_000 },
);
async function listJobs(rid: string, sha: string): Promise<Job[]> {
return withTestBackend(tauri.invoke, "list_jobs", { rid, sha });
}
// Listing jobs scans every job COB in the repo, so it is slow (hundreds of ms
// to seconds). It is re-requested on every JobCob mount; caching by (rid, sha)
// collapses concurrent identical calls into one in-flight request and reuses
// the result across navigations. A short TTL keeps CI status reasonably fresh.
export const cachedListJobs = cached(
listJobs,
(...[rid, sha]) => `list_jobs:${rid}:${sha}`,
{ max: 200, ttl: 30_000 },
);
// to seconds).
export async function listJobs(rid: string): Promise<Job[]> {
return withTestBackend(tauri.invoke, "list_jobs", { rid });
}
async function alias(nid: string): Promise<string | null> {
return withTestBackend(tauri.invoke, "alias", { nid });
+29 -8
View File
@@ -14,8 +14,9 @@ import { fetchingRepos } from "@app/lib/seedRepo";
import { artifactNodeRunning, nodeEvents, nodeRunning } from "./events";
// Collects keys and invalidates them together after `delay`.
function batch(delay: number): (affected: Key[]) => void {
// Collects keys and invalidates them together after `delay`. Job lists under
// a key are left to their own batch unless `jobs` says the keys are theirs.
export function batch(delay: number, jobs: boolean): (affected: Key[]) => void {
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- never rendered
const pending = new Map<string, Key>();
let flush: ReturnType<typeof setTimeout> | undefined;
@@ -26,18 +27,35 @@ function batch(delay: number): (affected: Key[]) => void {
const queryKeys = [...pending.values()];
pending.clear();
for (const queryKey of queryKeys) {
void queryClient.invalidateQueries({ queryKey });
void queryClient.invalidateQueries({
queryKey,
predicate: query => jobs || !isJobs(query.queryKey as Key),
});
}
}, delay);
};
}
// A fetch often updates many refs in a burst, so invalidations are batched.
const invalidate = batch(100);
const invalidate = batch(100, false);
// Listing CI jobs scans every job in the repository, and a running
// job updates often, so job lists refresh at most every few seconds.
const invalidateJobs = batch(5_000, true);
function onRefUpdate(update: RefUpdate) {
function isJobs(key: Key): boolean {
return key[0] === "repo" && key[2] === "jobs";
}
export function onRefUpdate(update: RefUpdate) {
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");
invalidateJobs([
...affected.filter(isJobs),
...(repoWide ? [keys.jobs(update.rid)] : []),
]);
invalidate([
...affectedKeys(update),
...affected.filter(key => !isJobs(key)),
// The node writes notifications as it fetches.
keys.notificationCount(),
// The repository list changes when a seeded repository arrives, or when an
@@ -90,10 +108,13 @@ export const createEventEmittersOnce = once(async (): Promise<UnlistenFn> => {
]);
// Without the node's events, refetch what is shown whenever the user comes
// back to the window.
// back to the window. CI jobs only arrive through the node, so they can't
// have changed.
const onFocus = () => {
if (!get(nodeEvents)) {
void queryClient.invalidateQueries();
void queryClient.invalidateQueries({
predicate: query => !isJobs(query.queryKey as Key),
});
}
};
window.addEventListener("focus", onFocus);
+96
View File
@@ -0,0 +1,96 @@
import type { Query } from "@tanstack/query-core";
import { keys } from "@radicle-gui/core/keys";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { batch, onRefUpdate } from "@app/lib/startup.svelte";
const { invalidateQueries } = vi.hoisted(() => ({
invalidateQueries: vi.fn(),
}));
vi.mock("@app/lib/query", () => ({ queryClient: { invalidateQueries } }));
vi.mock("@app/lib/invoke", () => ({ invoke: vi.fn() }));
vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn() }));
vi.mock("@app/lib/seedRepo", async () => {
const { writable } = await import("svelte/store");
return { fetchingRepos: writable([]) };
});
const rid = "rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5";
// Whether an invalidation with the given filter refetches a query of `key`.
function covers(call: unknown[], key: readonly unknown[]): boolean {
const { predicate } = call[0] as {
predicate: (query: Pick<Query, "queryKey">) => boolean;
};
return predicate({ queryKey: [...key] });
}
// Invalidations of the repository's data, leaving out others such as the
// notification count.
function repoCalls() {
return invalidateQueries.mock.calls.filter(
call => call[0].queryKey[0] === "repo",
);
}
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
invalidateQueries.mockReset();
});
describe("batch", () => {
test("invalidates each key once after the delay", () => {
const invalidate = batch(100, false);
invalidate([keys.issues(rid)]);
invalidate([keys.issues(rid), keys.patches(rid)]);
expect(invalidateQueries).not.toHaveBeenCalled();
vi.advanceTimersByTime(100);
expect(invalidateQueries.mock.calls.map(call => call[0].queryKey)).toEqual([
keys.issues(rid),
keys.patches(rid),
]);
});
test("leaves job lists under a repository key to their own batch", () => {
const invalidate = batch(100, false);
invalidate([keys.repo(rid)]);
vi.advanceTimersByTime(100);
const [call] = invalidateQueries.mock.calls;
expect(covers(call, keys.issues(rid))).toBe(true);
expect(covers(call, keys.jobs(rid))).toBe(false);
});
test("the job batch refetches job lists", () => {
const invalidateJobs = batch(5_000, true);
invalidateJobs([keys.jobs(rid)]);
vi.advanceTimersByTime(5_000);
const [call] = invalidateQueries.mock.calls;
expect(covers(call, keys.jobs(rid))).toBe(true);
});
test("an update naming no refs refreshes job lists in their own batch", () => {
onRefUpdate({ rid });
vi.advanceTimersByTime(100);
const [first] = repoCalls();
expect(first[0].queryKey).toEqual(keys.repo(rid));
expect(covers(first, keys.jobs(rid))).toBe(false);
vi.advanceTimersByTime(5_000);
expect(repoCalls().map(call => call[0].queryKey)).toEqual([
keys.repo(rid),
keys.jobs(rid),
]);
});
});