1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-24 06:28:42 +01:00

fix: clears logs when an exception is thrown in logs events source (#2406)

* feat: adds more logging for eventsource

* feat: removes error from event streams

* removes unused code

* adds errors back

* moves logs to debug

* removes lastEventId

* fixes tests
This commit is contained in:
Amir Raminfar
2023-10-03 09:58:46 -07:00
committed by GitHub
parent 32bc9a407b
commit f1dad96b86
2 changed files with 31 additions and 25 deletions

View File

@@ -88,7 +88,7 @@ describe("<LogEventSource />", () => {
});
}
const sourceUrl = "/api/logs/stream/localhost/abc?lastEventId=&stdout=1&stderr=1";
const sourceUrl = "/api/logs/stream/localhost/abc?stdout=1&stderr=1";
test("renders correctly", async () => {
const wrapper = createLogEventSource();

View File

@@ -30,6 +30,7 @@ export function useLogStream(container: Ref<Container>, streamConfig: LogStreamC
let messages: LogEntry<string | JSONObject>[] = $ref([]);
let buffer: LogEntry<string | JSONObject>[] = $ref([]);
const scrollingPaused = $ref(inject("scrollingPaused") as Ref<boolean>);
let containerId = container.value.id;
function flushNow() {
if (messages.length > config.maxLogs) {
@@ -57,21 +58,30 @@ export function useLogStream(container: Ref<Container>, streamConfig: LogStreamC
}
const flushBuffer = debounce(flushNow, 250, { maxWait: 1000 });
let es: EventSource | null = null;
let lastEventId = "";
function close() {
if (es) {
es.close();
console.debug(`EventSource closed for ${containerId}`);
es = null;
}
}
function clearMessages() {
flushBuffer.cancel();
messages = [];
buffer = [];
console.debug(`Clearing messages for ${containerId}`);
}
function connect({ clear } = { clear: true }) {
es?.close();
close();
if (clear) {
flushBuffer.cancel();
messages = [];
buffer = [];
lastEventId = "";
clearMessages();
}
const params = {
lastEventId,
} as { lastEventId: string; stdout?: string; stderr?: string };
const params = {} as { stdout?: string; stderr?: string };
if (streamConfig.stdout) {
params.stdout = "1";
@@ -79,23 +89,25 @@ export function useLogStream(container: Ref<Container>, streamConfig: LogStreamC
if (streamConfig.stderr) {
params.stderr = "1";
}
containerId = container.value.id;
console.debug(`Connecting to ${containerId} with params`, params);
es = new EventSource(
`${config.base}/api/logs/stream/${container.value.host}/${container.value.id}?${new URLSearchParams(
params,
).toString()}`,
`${config.base}/api/logs/stream/${container.value.host}/${containerId}?${new URLSearchParams(params).toString()}`,
);
es.addEventListener("container-stopped", () => {
es?.close();
es = null;
close();
buffer.push(new DockerEventLogEntry("Container stopped", new Date(), "container-stopped"));
flushBuffer();
flushBuffer.flush();
});
es.addEventListener("error", (e) => console.error("EventSource failed: " + JSON.stringify(e)));
es.onerror = (e) => {
console.error(`Unexpected error for eventsource container-id:${containerId}. Clearing logs and reconnecting.`);
clearMessages();
};
es.onmessage = (e) => {
lastEventId = e.lastEventId;
if (e.data) {
buffer.push(parseMessage(e.data));
flushBuffer();
@@ -126,9 +138,7 @@ export function useLogStream(container: Ref<Container>, streamConfig: LogStreamC
const logs = await (
await fetch(
`${config.base}/api/logs/${container.value.host}/${container.value.id}?${new URLSearchParams(
params,
).toString()}`,
`${config.base}/api/logs/${container.value.host}/${containerId}?${new URLSearchParams(params).toString()}`,
)
).text();
if (logs) {
@@ -152,11 +162,7 @@ export function useLogStream(container: Ref<Container>, streamConfig: LogStreamC
},
);
onUnmounted(() => {
if (es) {
es.close();
}
});
onUnmounted(() => close());
watch(
() => container.value.id,