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
going back to chaining in success callback
  • Loading branch information
Kira-Pilot committed Apr 26, 2023
commit e8e88b45455e7d4debad8f1dbca8d5e64e7c150b
36 changes: 6 additions & 30 deletions site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useActor } from "@xstate/react"
import { ProvisionerJobLog, WorkspaceBuild } from "api/typesGenerated"
import { ProvisionerJobLog } from "api/typesGenerated"
import { useDashboard } from "components/Dashboard/DashboardProvider"
import dayjs from "dayjs"
import { useFeatureVisibility } from "hooks/useFeatureVisibility"
Expand Down Expand Up @@ -28,9 +28,9 @@ import {
} from "../../xServices/workspace/workspaceXService"
import { UpdateBuildParametersDialog } from "./UpdateBuildParametersDialog"
import { ChangeVersionDialog } from "./ChangeVersionDialog"
import { useQuery, useMutation } from "@tanstack/react-query"
import { getTemplateVersions, stopWorkspace, startWorkspace } from "api/api"
import { waitForStop } from "./utils"
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 @@ -82,31 +82,7 @@ export const WorkspaceReadyPage = ({
Error | unknown | undefined
>(undefined)

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

const restartWorkspace = async () => {
try {
await requestStopWorkspace.mutateAsync(workspace.id)

if (!requestStopWorkspace.data) {
throw new Error("No stop build returned!")
}

await waitForStop(requestStopWorkspace.data)
await requestStartWorkspace.mutateAsync({
workspaceId: workspace.id,
templateVersionId: workspace.latest_build.template_version_id,
})
} catch (error) {
// No error handling needed if the build has been canceled
if ((error as WorkspaceBuild).status === "canceled") {
return
}

setRestartBuildError(error)
}
}
const { mutate: restartWorkspace } = useRestartWorkspace(setRestartBuildError)

// keep banner machine in sync with workspace
useEffect(() => {
Expand Down Expand Up @@ -154,7 +130,7 @@ export const WorkspaceReadyPage = ({
workspace={workspace}
handleStart={() => workspaceSend({ type: "START" })}
handleStop={() => workspaceSend({ type: "STOP" })}
handleRestart={restartWorkspace}
handleRestart={() => restartWorkspace(workspace.id)}
handleDelete={() => workspaceSend({ type: "ASK_DELETE" })}
handleUpdate={() => workspaceSend({ type: "UPDATE" })}
handleCancel={() => workspaceSend({ type: "CANCEL" })}
Expand Down
62 changes: 62 additions & 0 deletions site/src/pages/WorkspacePage/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
getWorkspaceBuildByNumber,
stopWorkspace,
startWorkspace,
} from "api/api"
import { delay } from "utils/delay"
import { ProvisionerJob, WorkspaceBuild } from "api/typesGenerated"
import { useMutation } from "@tanstack/react-query"

export function waitForStop(build: WorkspaceBuild) {
return new Promise((res, reject) => {
void (async () => {
let latestJobInfo: ProvisionerJob | undefined = undefined

while (latestJobInfo?.status !== "succeeded") {
const { job } = await getWorkspaceBuildByNumber(
build.workspace_owner_name,
build.workspace_name,
String(build.build_number),
)
latestJobInfo = job
console.log("latest job status", latestJobInfo.status)

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

await delay(1000)
}

console.log("resolving status status", latestJobInfo.status)

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

export const useRestartWorkspace = (
setRestartBuildError: (arg: Error | unknown | undefined) => void,
) => {
return useMutation({
mutationFn: stopWorkspace,
onSuccess: async (data) => {
try {
await waitForStop(data)
await startWorkspace({
workspaceId: data.workspace_id,
templateVersionId: 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 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!

32 changes: 0 additions & 32 deletions site/src/pages/WorkspacePage/utils.ts

This file was deleted.