Skip to content

feat: offer to restart workspace when ttl is changed #5391

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 19 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
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
5 changes: 4 additions & 1 deletion site/src/pages/WorkspacePage/WorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useMachine } from "@xstate/react"
import { AlertBanner } from "components/AlertBanner/AlertBanner"
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne"
import { FC, useEffect } from "react"
import { useParams } from "react-router-dom"
import { useLocation, useParams } from "react-router-dom"
import { Loader } from "components/Loader/Loader"
import { firstOrItem } from "util/array"
import { workspaceMachine } from "xServices/workspace/workspaceXService"
Expand All @@ -15,6 +15,8 @@ export const WorkspacePage: FC = () => {
useParams()
const username = firstOrItem(usernameQueryParam, null)
const workspaceName = firstOrItem(workspaceQueryParam, null)
const shouldRestartWorkspace =
useLocation().state?.shouldRestartWorkspace ?? false
const [workspaceState, workspaceSend] = useMachine(workspaceMachine)
const {
workspace,
Expand Down Expand Up @@ -69,6 +71,7 @@ export const WorkspacePage: FC = () => {
workspaceState={workspaceState}
quotaState={quotaState}
workspaceSend={workspaceSend}
shouldRestartWorkspace={shouldRestartWorkspace}
/>
</Cond>
<Cond>
Expand Down
7 changes: 7 additions & 0 deletions site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ interface WorkspaceReadyPageProps {
workspaceState: StateFrom<typeof workspaceMachine>
quotaState: StateFrom<typeof quotaMachine>
workspaceSend: (event: WorkspaceEvent) => void
shouldRestartWorkspace: boolean
}

export const WorkspaceReadyPage = ({
workspaceState,
quotaState,
workspaceSend,
shouldRestartWorkspace,
}: WorkspaceReadyPageProps): JSX.Element => {
const [bannerState, bannerSend] = useActor(
workspaceState.children["scheduleBannerMachine"],
Expand Down Expand Up @@ -71,6 +73,11 @@ export const WorkspaceReadyPage = ({
bannerSend({ type: "REFRESH_WORKSPACE", workspace })
}, [bannerSend, workspace])

useEffect(() => {
// workspaces can be started without being stopped first
shouldRestartWorkspace && workspaceSend({ type: "START" })
}, [shouldRestartWorkspace, workspaceSend])

return (
<>
<Helmet>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {
MockUser,
MockWorkspace,
renderWithAuth,
} from "testHelpers/renderHelpers"
import userEvent from "@testing-library/user-event"
import { screen } from "@testing-library/react"
import {
formValuesToAutoStartRequest,
formValuesToTTLRequest,
Expand All @@ -8,7 +15,14 @@ import {
} from "pages/WorkspaceSchedulePage/schedule"
import { AutoStop, ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
import * as TypesGen from "../../api/typesGenerated"
import { WorkspaceScheduleFormValues } from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
import {
WorkspaceScheduleFormValues,
Language as FormLanguage,
} from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
import {
WorkspaceSchedulePage,
Language as PageLanguage,
} from "./WorkspaceSchedulePage"

const validValues: WorkspaceScheduleFormValues = {
autoStartEnabled: true,
Expand Down Expand Up @@ -241,4 +255,42 @@ describe("WorkspaceSchedulePage", () => {
expect(ttlMsToAutoStop(ttlMs)).toEqual(autoStop)
})
})

describe("autoStop change dialog", () => {
it("shows if autoStop is changed", async () => {
renderWithAuth(<WorkspaceSchedulePage />, {
route: `/@${MockUser.username}/${MockWorkspace.name}/schedule`,
path: "/@:username/:workspace/schedule",
})
const user = userEvent.setup()
const autoStopToggle = await screen.findByLabelText(
FormLanguage.stopSwitch,
)
await user.click(autoStopToggle)
const submitButton = await screen.findByRole("button", {
name: /submit/i,
})
await user.click(submitButton)
const dialog = await screen.findByText(PageLanguage.dialogTitle)
expect(dialog).toBeInTheDocument()
})

it("doesn't show if autoStop is not changed", async () => {
renderWithAuth(<WorkspaceSchedulePage />, {
route: `/@${MockUser.username}/${MockWorkspace.name}/schedule`,
path: "/@:username/:workspace/schedule",
})
const user = userEvent.setup()
const autoStartToggle = await screen.findByLabelText(
FormLanguage.startSwitch,
)
await user.click(autoStartToggle)
const submitButton = await screen.findByRole("button", {
name: /submit/i,
})
await user.click(submitButton)
const dialog = screen.queryByText(PageLanguage.dialogTitle)
expect(dialog).not.toBeInTheDocument()
})
})
})
66 changes: 46 additions & 20 deletions site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useMachine } from "@xstate/react"
import { AlertBanner } from "components/AlertBanner/AlertBanner"
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
import { scheduleToAutoStart } from "pages/WorkspaceSchedulePage/schedule"
import { ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
import React, { useEffect, useState } from "react"
import React, { useEffect } from "react"
import { Navigate, useNavigate, useParams } from "react-router-dom"
import { scheduleChanged } from "util/schedule"
import * as TypesGen from "../../api/typesGenerated"
Expand All @@ -15,13 +16,23 @@ import {
formValuesToTTLRequest,
} from "./formToRequest"

const Language = {
export const Language = {
forbiddenError:
"You don't have permissions to update the schedule for this workspace.",
getWorkspaceError: "Failed to fetch workspace.",
checkPermissionsError: "Failed to fetch permissions.",
dialogTitle: "Restart workspace?",
dialogDescription: `Would you like to restart your workspace now to apply your new auto-stop setting,
or let it apply after your next workspace start?`,
restart: "Restart workspace now",
applyLater: "Apply update later",
}

const getAutoStart = (workspace?: TypesGen.Workspace) =>
scheduleToAutoStart(workspace?.autostart_schedule)
const getAutoStop = (workspace?: TypesGen.Workspace) =>
ttlMsToAutoStop(workspace?.ttl_ms)

export const WorkspaceSchedulePage: React.FC = () => {
const { username: usernameQueryParam, workspace: workspaceQueryParam } =
useParams()
Expand All @@ -35,6 +46,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
getWorkspaceError,
permissions,
workspace,
shouldRestartWorkspace,
} = scheduleState.context

// Get workspace on mount and whenever the args for getting a workspace change.
Expand All @@ -45,19 +57,6 @@ export const WorkspaceSchedulePage: React.FC = () => {
scheduleSend({ type: "GET_WORKSPACE", username, workspaceName })
}, [username, workspaceName, scheduleSend])

const getAutoStart = (workspace?: TypesGen.Workspace) =>
scheduleToAutoStart(workspace?.autostart_schedule)
const getAutoStop = (workspace?: TypesGen.Workspace) =>
ttlMsToAutoStop(workspace?.ttl_ms)

const [autoStart, setAutoStart] = useState(getAutoStart(workspace))
const [autoStop, setAutoStop] = useState(getAutoStop(workspace))

useEffect(() => {
setAutoStart(getAutoStart(workspace))
setAutoStop(getAutoStop(workspace))
}, [workspace])

if (!username || !workspaceName) {
return <Navigate to="/workspaces" />
}
Expand Down Expand Up @@ -101,7 +100,10 @@ export const WorkspaceSchedulePage: React.FC = () => {
return (
<WorkspaceScheduleForm
submitScheduleError={submitScheduleError}
initialValues={{ ...autoStart, ...autoStop }}
initialValues={{
...getAutoStart(workspace),
...getAutoStop(workspace),
}}
isLoading={scheduleState.tags.has("loading")}
onCancel={() => {
navigate(`/@${username}/${workspaceName}`)
Expand All @@ -111,16 +113,40 @@ export const WorkspaceSchedulePage: React.FC = () => {
type: "SUBMIT_SCHEDULE",
autoStart: formValuesToAutoStartRequest(values),
ttl: formValuesToTTLRequest(values),
autoStartChanged: scheduleChanged(autoStart, values),
autoStopChanged: scheduleChanged(autoStop, values),
autoStartChanged: scheduleChanged(getAutoStart(workspace), values),
autoStopChanged: scheduleChanged(getAutoStop(workspace), values),
})
}}
/>
)
}

if (scheduleState.matches("submitSuccess")) {
return <Navigate to={`/@${username}/${workspaceName}`} />
if (scheduleState.matches("showingRestartDialog")) {
return (
<ConfirmDialog
open
title={Language.dialogTitle}
description={Language.dialogDescription}
confirmText={Language.restart}
cancelText={Language.applyLater}
hideCancel={false}
onConfirm={() => {
scheduleSend("RESTART_WORKSPACE")
}}
onClose={() => {
scheduleSend("APPLY_LATER")
}}
/>
)
}

if (scheduleState.matches("done")) {
return (
<Navigate
to={`/@${username}/${workspaceName}`}
state={{ shouldRestartWorkspace }}
/>
)
}

// Theoretically impossible - log and bail
Expand Down
1 change: 1 addition & 0 deletions site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export const handlers = [
const permissions = [
...Object.keys(permissionsToCheck),
"canUpdateTemplate",
"updateWorkspace",
]
const response = permissions.reduce((obj, permission) => {
return {
Expand Down
Loading