Skip to content

Commit 5af611a

Browse files
authored
feat: removes orphaned containers. fixes #3077 (#3098)
1 parent c5771fa commit 5af611a

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

assets/stores/container.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,18 @@ export const useContainerStore = defineStore("container", () => {
5959
container.updateStat(rest);
6060
}
6161
});
62-
es.addEventListener("container-die", (e) => {
63-
const event = JSON.parse((e as MessageEvent).data) as { actorId: string };
64-
const container = allContainersById.value[event.actorId];
65-
if (container) {
66-
container.state = "exited";
62+
es.addEventListener("container-event", (e) => {
63+
const event = JSON.parse((e as MessageEvent).data) as { actorId: string; name: string };
64+
switch (event.name) {
65+
case "die":
66+
const container = allContainersById.value[event.actorId];
67+
if (container) {
68+
container.state = "exited";
69+
}
70+
break;
71+
case "destroy":
72+
containers.value = containers.value.filter((c) => c.id !== event.actorId);
73+
break;
6774
}
6875
});
6976

internal/web/__snapshots__/web.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ event: containers-changed
135135
data: [{"id":"1234","names":null,"name":"test","image":"test","imageId":"","command":"","created":"0001-01-01T00:00:00Z","state":"","status":"","stats":[]}]
136136

137137

138-
event: container-start
138+
event: container-event
139139
data: {"actorId":"1234","name":"start","host":"localhost"}
140140

141141
/* snapshot: Test_handler_streamLogs_error_finding_container */

internal/web/events.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) {
6565
return
6666
}
6767
switch event.Name {
68-
case "start", "die":
68+
case "start", "die", "destroy":
6969
if event.Name == "start" {
7070
log.Debugf("found new container with id: %v", event.ActorID)
7171
if containers, err := h.multiHostService.ListContainersForHost(event.Host); err == nil {
@@ -77,7 +77,7 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) {
7777
}
7878

7979
bytes, _ := json.Marshal(event)
80-
if _, err := fmt.Fprintf(w, "event: container-%s\ndata: %s\n\n", event.Name, string(bytes)); err != nil {
80+
if _, err := fmt.Fprintf(w, "event: container-event\ndata: %s\n\n", string(bytes)); err != nil {
8181
log.Errorf("error writing event to event stream: %v", err)
8282
return
8383
}

0 commit comments

Comments
 (0)