-
Notifications
You must be signed in to change notification settings - Fork 894
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
Changes from 1 commit
4079fc5
a6cbf34
1f1d388
e8e88b4
2e2b89d
585ece4
5041d5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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) | ||
} | ||
}, | ||
}) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
but There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm that's a good idea. Let me try it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @BrunoQuaresma Woo that's a lot better! Thanks! |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.