1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-21 21:33:18 +01:00

Fixes reconnection bugs (#1256)

* Fixes reconnect by sending lastEventId see #1246

* Cleans up colors and spacing

* Fixes tests
This commit is contained in:
Amir Raminfar
2021-05-26 12:05:55 -07:00
committed by GitHub
parent 64f744a5d4
commit 920acf4256
5 changed files with 81 additions and 60 deletions

View File

@@ -22,10 +22,11 @@ export default {
return {
messages: [],
buffer: [],
es: null,
lastEventId: null,
};
},
created() {
this.es = null;
this.loadLogs();
this.flushBuffer = debounce(this.flushNow, 250, { maxWait: 1000 });
},
@@ -33,28 +34,37 @@ export default {
this.es.close();
},
methods: {
onContainerStateChange(newValue, oldValue) {
if (newValue == "running" && newValue != oldValue) {
this.connect();
}
},
loadLogs() {
this.reset();
this.connect();
},
onContainerStopped() {
this.es.close();
this.buffer.push({ event: "container-stopped", message: "Container stopped", date: new Date(), key: new Date() });
this.flushBuffer();
this.flushBuffer.flush();
},
onMessage(e) {
this.lastEventId = e.lastEventId;
this.buffer.push(this.parseMessage(e.data));
this.flushBuffer();
},
onContainerStateChange(newValue, oldValue) {
if (newValue == "running" && newValue != oldValue) {
this.buffer.push({
event: "container-started",
message: "Container started",
date: new Date(),
key: new Date(),
});
this.connect();
}
},
connect() {
this.es = new EventSource(`${config.base}/api/logs/stream?id=${this.id}`);
this.es.addEventListener("container-stopped", (e) => {
this.es.close();
this.buffer.push({ event: "container-stopped", message: "Container stopped", date: new Date() });
this.flushBuffer();
this.flushBuffer.flush();
});
this.es = new EventSource(`${config.base}/api/logs/stream?id=${this.id}&lastEventId=${this.lastEventId ?? ""}`);
this.es.addEventListener("container-stopped", (e) => this.onContainerStopped());
this.es.addEventListener("error", (e) => console.error("EventSource failed: " + JSON.stringify(e)));
this.es.onmessage = (e) => {
this.buffer.push(this.parseMessage(e.data));
this.flushBuffer();
};
this.es.onmessage = (e) => this.onMessage(e);
},
flushNow() {
this.messages.push(...this.buffer);
@@ -96,7 +106,7 @@ export default {
}
const key = data.substring(0, i);
const date = new Date(key);
const message = data.substring(i);
const message = data.substring(i + 1);
return { key, date, message };
},
},