Skip to content

Commit 84ccd92

Browse files
committed
reflect devcontainer status in container and fix story
1 parent 7d9f43c commit 84ccd92

File tree

9 files changed

+110
-27
lines changed

9 files changed

+110
-27
lines changed

agent/agentcontainers/api.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
403403
// Check if the container is running and update the known devcontainers.
404404
for i := range updated.Containers {
405405
container := &updated.Containers[i] // Grab a reference to the container to allow mutating it.
406+
container.DevcontainerStatus = "" // Reset the status for the container (updated later).
406407
container.DevcontainerDirty = false // Reset dirty state for the container (updated later).
407408

408409
workspaceFolder := container.Labels[DevcontainerLocalFolderLabel]
@@ -463,6 +464,11 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
463464
// Iterate through all known devcontainers and update their status
464465
// based on the current state of the containers.
465466
for _, dc := range api.knownDevcontainers {
467+
if dc.Container != nil {
468+
dc.Container.DevcontainerStatus = dc.Status
469+
dc.Container.DevcontainerDirty = dc.Dirty
470+
}
471+
466472
switch {
467473
case dc.Status == codersdk.WorkspaceAgentDevcontainerStatusStarting:
468474
continue // This state is handled by the recreation routine.
@@ -608,6 +614,9 @@ func (api *API) handleDevcontainerRecreate(w http.ResponseWriter, r *http.Reques
608614
// Update the status so that we don't try to recreate the
609615
// devcontainer multiple times in parallel.
610616
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStarting
617+
if dc.Container != nil {
618+
dc.Container.DevcontainerStatus = dc.Status
619+
}
611620
api.knownDevcontainers[dc.WorkspaceFolder] = dc
612621
api.recreateWg.Add(1)
613622
go api.recreateDevcontainer(dc, configPath)
@@ -680,6 +689,9 @@ func (api *API) recreateDevcontainer(dc codersdk.WorkspaceAgentDevcontainer, con
680689
api.mu.Lock()
681690
dc = api.knownDevcontainers[dc.WorkspaceFolder]
682691
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusError
692+
if dc.Container != nil {
693+
dc.Container.DevcontainerStatus = dc.Status
694+
}
683695
api.knownDevcontainers[dc.WorkspaceFolder] = dc
684696
api.recreateErrorTimes[dc.WorkspaceFolder] = api.clock.Now("recreate", "errorTimes")
685697
api.mu.Unlock()
@@ -695,10 +707,12 @@ func (api *API) recreateDevcontainer(dc codersdk.WorkspaceAgentDevcontainer, con
695707
// allows the update routine to update the devcontainer status, but
696708
// to minimize the time between API consistency, we guess the status
697709
// based on the container state.
698-
if dc.Container != nil && dc.Container.Running {
699-
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusRunning
700-
} else {
701-
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStopped
710+
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStopped
711+
if dc.Container != nil {
712+
if dc.Container.Running {
713+
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusRunning
714+
}
715+
dc.Container.DevcontainerStatus = dc.Status
702716
}
703717
dc.Dirty = false
704718
api.recreateSuccessTimes[dc.WorkspaceFolder] = api.clock.Now("recreate", "successTimes")

coderd/apidoc/docs.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codersdk/workspaceagents.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ type WorkspaceAgentContainer struct {
450450
// Volumes is a map of "things" mounted into the container. Again, this
451451
// is somewhat implementation-dependent.
452452
Volumes map[string]string `json:"volumes"`
453+
// DevcontainerStatus is the status of the devcontainer, if this
454+
// container is a devcontainer. This is used to determine if the
455+
// devcontainer is running, stopped, starting, or in an error state.
456+
DevcontainerStatus WorkspaceAgentDevcontainerStatus `json:"devcontainer_status,omitempty"`
453457
// DevcontainerDirty is true if the devcontainer configuration has changed
454458
// since the container was created. This is used to determine if the
455459
// container needs to be rebuilt.

docs/reference/api/agents.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/api/schemas.md

Lines changed: 34 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/api/typesGenerated.ts

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/modules/resources/AgentDevcontainerCard.stories.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Meta, StoryObj } from "@storybook/react";
2-
import { within, userEvent } from "@storybook/test";
32
import {
43
MockWorkspace,
54
MockWorkspaceAgent,
@@ -48,14 +47,8 @@ export const Recreating: Story = {
4847
container: {
4948
...MockWorkspaceAgentContainer,
5049
devcontainer_dirty: true,
50+
devcontainer_status: "starting",
5151
ports: MockWorkspaceAgentContainerPorts,
5252
},
5353
},
54-
play: async ({ canvasElement }) => {
55-
const canvas = within(canvasElement);
56-
const recreateButton = await canvas.findByRole("button", {
57-
name: /recreate/i,
58-
});
59-
await userEvent.click(recreateButton);
60-
},
6154
};

site/src/modules/resources/AgentDevcontainerCard.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from "components/Tooltip/Tooltip";
2121
import { ExternalLinkIcon, Loader2Icon } from "lucide-react";
2222
import type { FC } from "react";
23-
import { useState } from "react";
23+
import { useEffect, useState } from "react";
2424
import { portForwardURL } from "utils/portForward";
2525
import { AgentButton } from "./AgentButton";
2626
import { AgentDevcontainerSSHButton } from "./SSHButton/SSHButton";
@@ -78,6 +78,15 @@ export const AgentDevcontainerCard: FC<AgentDevcontainerCardProps> = ({
7878
}
7979
};
8080

81+
// If the container is starting, reflect this in the recreate button.
82+
useEffect(() => {
83+
if (container.devcontainer_status === "starting") {
84+
setIsRecreating(true);
85+
} else {
86+
setIsRecreating(false);
87+
}
88+
}, [container.devcontainer_status]);
89+
8190
return (
8291
<section
8392
className="border border-border border-dashed rounded p-6 "

0 commit comments

Comments
 (0)