fix: handle errors in sse stream

This commit is contained in:
2026-06-09 01:01:08 +03:00
parent d9b52806c0
commit 49cd3d43c8
2 changed files with 57 additions and 5 deletions
+28 -2
View File
@@ -17,8 +17,10 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import type { QueryClient } from "@tanstack/react-query";
import { useQueryClient } from "@tanstack/react-query"; import { useQueryClient } from "@tanstack/react-query";
import { buildEventStreamUrl } from "hooks/useEventStream"; import { buildEventStreamUrl } from "hooks/useEventStream";
import { useNotification } from "hooks/useNotification";
import type { LogRecord } from "models/LogRecord"; import type { LogRecord } from "models/LogRecord";
import type { RepositoryId } from "models/RepositoryId"; import type { RepositoryId } from "models/RepositoryId";
import { useEffect } from "react"; import { useEffect } from "react";
@@ -34,14 +36,38 @@ function appendLogRecord(existing: LogRecord[] | undefined, record: LogRecord):
return [...existing ?? [], record]; return [...existing ?? [], record];
} }
function invalidateLogs(queryClient: QueryClient, repository: RepositoryId, packageBase: string): void {
void queryClient.invalidateQueries({ queryKey: ["logs", repository.key, packageBase] });
}
export function useBuildLogStream(packageBase: string, repository: RepositoryId): void { export function useBuildLogStream(packageBase: string, repository: RepositoryId): void {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { showError } = useNotification();
useEffect(() => { useEffect(() => {
const source = new EventSource(buildEventStreamUrl(repository, ["build-log"], packageBase)); const source = new EventSource(buildEventStreamUrl(repository, ["build-log"], packageBase));
let needsRefresh = false;
source.addEventListener("error", () => {
needsRefresh = true;
});
source.addEventListener("open", () => {
if (needsRefresh) {
invalidateLogs(queryClient, repository, packageBase);
needsRefresh = false;
}
});
source.addEventListener("build-log", (event: MessageEvent<string>) => { source.addEventListener("build-log", (event: MessageEvent<string>) => {
const data = JSON.parse(event.data) as BuildLogEvent; let data: BuildLogEvent;
try {
data = JSON.parse(event.data) as BuildLogEvent;
} catch {
showError("Live updates failed", "Could not parse build log event; refreshing logs.");
invalidateLogs(queryClient, repository, packageBase);
return;
}
const record: LogRecord = { const record: LogRecord = {
created: data.created, created: data.created,
@@ -66,5 +92,5 @@ export function useBuildLogStream(packageBase: string, repository: RepositoryId)
return () => { return () => {
source.close(); source.close();
}; };
}, [queryClient, packageBase, repository]); }, [queryClient, packageBase, repository, showError]);
} }
+29 -3
View File
@@ -18,6 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import type { QueryClient } from "@tanstack/react-query"; import type { QueryClient } from "@tanstack/react-query";
import { useNotification } from "hooks/useNotification";
import type { RepositoryId } from "models/RepositoryId"; import type { RepositoryId } from "models/RepositoryId";
import { useEffect } from "react"; import { useEffect } from "react";
@@ -62,6 +63,12 @@ function invalidateForEvent(
} }
} }
function invalidateRepository(queryClient: QueryClient, repositoryKey: string): void {
void queryClient.invalidateQueries({ queryKey: ["packages", repositoryKey] });
void queryClient.invalidateQueries({ queryKey: ["status", repositoryKey] });
void queryClient.invalidateQueries({ queryKey: ["events", repositoryKey] });
}
export function buildEventStreamUrl( export function buildEventStreamUrl(
repository: RepositoryId, repository: RepositoryId,
events?: readonly string[], events?: readonly string[],
@@ -80,22 +87,41 @@ export function buildEventStreamUrl(
} }
export function useEventStream(queryClient: QueryClient, repository: RepositoryId | null): void { export function useEventStream(queryClient: QueryClient, repository: RepositoryId | null): void {
const { showError } = useNotification();
useEffect(() => { useEffect(() => {
if (!repository) { if (!repository) {
return; return;
} }
const source = new EventSource(buildEventStreamUrl(repository, GLOBAL_EVENT_TYPES)); const source = new EventSource(buildEventStreamUrl(repository, GLOBAL_EVENT_TYPES));
let needsRefresh = false;
source.addEventListener("error", () => {
needsRefresh = true;
});
source.addEventListener("open", () => {
if (needsRefresh) {
invalidateRepository(queryClient, repository.key);
needsRefresh = false;
}
});
for (const eventType of GLOBAL_EVENT_TYPES) { for (const eventType of GLOBAL_EVENT_TYPES) {
source.addEventListener(eventType, (event: MessageEvent<string>) => { source.addEventListener(eventType, (event: MessageEvent<string>) => {
const data = JSON.parse(event.data) as { object_id?: string }; try {
invalidateForEvent(queryClient, repository.key, eventType, data.object_id ?? undefined); const data = JSON.parse(event.data) as { object_id?: string };
invalidateForEvent(queryClient, repository.key, eventType, data.object_id ?? undefined);
} catch {
showError("Live updates failed", "Could not parse server event; refreshing data.");
invalidateRepository(queryClient, repository.key);
}
}); });
} }
return () => { return () => {
source.close(); source.close();
}; };
}, [queryClient, repository]); }, [queryClient, repository, showError]);
} }