8000 fix(UI): workspace restart button stops build before starting a new one by Kira-Pilot · Pull Request #7301 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix(UI): workspace restart button stops build before starting a new one #7301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added restart hook
  • Loading branch information
Kira-Pilot committed Apr 25, 2023
commit a6cbf3486f571b7e64f8e3b4cc50b34903cbc048
2 changes: 1 addition & 1 deletion site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface WorkspaceProps {
}
handleStart: () => void
handleStop: () => void
handleRestart: () => void
handleRestart: any
handleDelete: () => void
handleUpdate: () => void
handleCancel: () => void
Expand Down
6 changes: 3 additions & 3 deletions site/src/components/WorkspaceActions/Buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { LoadingButton } from "components/LoadingButton/LoadingButton"
import { FC, PropsWithChildren } from "react"
import { useTranslation } from "react-i18next"
import { makeStyles } from "@material-ui/core/styles"
// import { UseMutateFunction } from "@tanstack/react-query"
// import { WorkspaceBuild } from "api/typesGenerated"

interface WorkspaceAction {
handleAction: () => void
Expand Down Expand Up @@ -67,9 +69,7 @@ export const StopButton: FC<PropsWithChildren<WorkspaceAction>> = ({
)
}

export const RestartButton: FC<PropsWithChildren<WorkspaceAction>> = ({
handleAction,
}) => {
export const RestartButton: FC<PropsWithChildren<any>> = ({ handleAction }) => {
const { t } = useTranslation("workspacePage")
const styles = useStyles()

Expand Down
8 changes: 5 additions & 3 deletions site/src/components/WorkspaceActions/WorkspaceActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { makeStyles } from "@material-ui/core/styles"
import MoreVertOutlined from "@material-ui/icons/MoreVertOutlined"
import { FC, ReactNode, useRef, useState } from "react"
import { useTranslation } from "react-i18next"
import { WorkspaceStatus } from "../../api/typesGenerated"
import { WorkspaceStatus } from "api/typesGenerated"
import {
ActionLoadingButton,
CancelButton,
Expand All @@ -29,7 +29,7 @@ export interface WorkspaceActionsProps {
isOutdated: boolean
handleStart: () => void
handleStop: () => void
handleRestart: () => void
handleRestart: any
handleDelete: () => void
handleUpdate: () => void
handleCancel: () => void
Expand Down Expand Up @@ -133,7 +133,9 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
(isUpdating
? buttonMapping[ButtonTypesEnum.updating]
: buttonMapping[ButtonTypesEnum.update])}
{actionsByStatus.map((action) => buttonMapping[action])}
{actionsByStatus.map((action) => (
<span key={action}>{buttonMapping[action]}</span>
))}
{canCancel && <CancelButton handleAction={handleCancel} />}
<div>
<Button
Expand Down
11 changes: 9 additions & 2 deletions site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { UpdateBuildParametersDialog } from "./UpdateBuildParametersDialog"
import { ChangeVersionDialog } from "./ChangeVersionDialog"
import { useQuery } from "@tanstack/react-query"
import { getTemplateVersions } from "api/api"
import { useRestartWorkspace } from "./hooks"

interface WorkspaceReadyPageProps {
workspaceState: StateFrom<typeof workspaceMachine>
Expand Down Expand Up @@ -77,6 +78,12 @@ export const WorkspaceReadyPage = ({
enabled: changeVersionDialogOpen,
})

const [restartBuildError, setRestartBuildError] = useState<
Error | unknown | undefined
>(undefined)

const { mutate: restartWorkspace } = useRestartWorkspace(setRestartBuildError)

// keep banner machine in sync with workspace
useEffect(() => {
bannerSend({ type: "REFRESH_WORKSPACE", workspace })
Expand Down Expand Up @@ -123,7 +130,7 @@ export const WorkspaceReadyPage = ({
workspace={workspace}
handleStart={() => workspaceSend({ type: "START" })}
handleStop={() => workspaceSend({ type: "STOP" })}
handleRestart={() => workspaceSend({ type: "START" })}
handleRestart={() => restartWorkspace(workspace.id)}
handleDelete={() => workspaceSend({ type: "ASK_DELETE" })}
handleUpdate={() => workspaceSend({ type: "UPDATE" })}
handleCancel={() => workspaceSend({ type: "CANCEL" })}
Expand All @@ -141,7 +148,7 @@ export const WorkspaceReadyPage = ({
hideVSCodeDesktopButton={featureVisibility["browser_only"]}
workspaceErrors={{
[WorkspaceErrors.GET_BUILDS_ERROR]: getBuildsError,
[WorkspaceErrors.BUILD_ERROR]: buildError,
[WorkspaceErrors.BUILD_ERROR]: buildError ?? restartBuildError,
[WorkspaceErrors.CANCELLATION_ERROR]: cancellationError,
}}
buildInfo={buildInfo}
Expand Down
64 changes: 64 additions & 0 deletions site/src/pages/WorkspacePage/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { useMutation } from "@tanstack/react-query"
import {
stopWorkspace,
getWorkspaceBuildByNumber,
startWorkspace,
} from "api/api"
import { delay } from "utils/delay"
import { ProvisionerJob, WorkspaceBuild } from "api/typesGenerated"

function waitForStop(
username: string,
workspaceName: string,
buildNumber: string,
) {
return new Promise((res, reject) => {
void (async () => {
let latestJobInfo: ProvisionerJob | undefined = undefined

while (latestJobInfo?.status !== "succeeded") {
const { job } = await getWorkspaceBuildByNumber(
username,
workspaceName,
buildNumber,
)
latestJobInfo = job

if (
["failed", "canceled"].some((status) =>
latestJobInfo?.status.includes(status),
)
) {
return reject(latestJobInfo)
}

await delay(1000)
}

return res(latestJobInfo)
})()
})
}

export const useRestartWorkspace = (
setRestartBuildError: (arg: Error | unknown | undefined) => void,
) => {
return useMutation({
mutationFn: stopWorkspace,
onSuccess: async (data) => {
try {
await waitForStop(
data.workspace_owner_name,
data.workspace_name,
String(data.build_number),
)
await startWorkspace(data.workspace_id, data.template_version_id)
} catch (error) {
if ((error as WorkspaceBuild).status === "canceled") {
return
}
setRestartBuildError(error)
}
},
})
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I wanted 9E81 to use an implementation closer to the following, which react query's docs suggest is possible:

 const requestStopWorkspace = useMutation({ mutationFn: stopWorkspace })
 const requestStartWorkspace = useMutation({ mutationFn: startWorkspace })

  const restartWorkspace = async () => {
    try {
      await requestStopWorkspace.mutateAsync(workspace.id)
      await waitForStop(requestStopWorkspace.data)
      await requestStartWorkspace.mutateAsync({
        workspaceId,
        templateVersionId,
      })
    } catch (error) {
      ...   
    }
  }

but mutateAsync does not seem to be working and the queries are not treated asynchronously. @BrunoQuaresma is this something you've run into before?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put a comment above, I think you don't need to have each API call into a "query". You can have a single function to restart the workspace and wrap this function into a query.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm that's a good idea. Let me try it!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BrunoQuaresma Woo that's a lot better! Thanks!

0