Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: delay pending-in-queue banner #8309

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 5 commits into from
Jul 6, 2023
Merged
Changes from 2 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
44 changes: 37 additions & 7 deletions site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ActiveTransition,
WorkspaceBuildProgress,
} from "components/WorkspaceBuildProgress/WorkspaceBuildProgress"
import { FC } from "react"
import { FC, useEffect, useState } from "react"
import { useTranslation } from "react-i18next"
import { useNavigate } from "react-router-dom"
import * as TypesGen from "../../api/typesGenerated"
Expand All @@ -32,6 +32,7 @@ import { useLocalStorage } from "hooks"
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne"
import AlertTitle from "@mui/material/AlertTitle"
import { Maybe } from "components/Conditionals/Maybe"
import dayjs from "dayjs"

export enum WorkspaceErrors {
GET_BUILDS_ERROR = "getBuildsError",
Expand Down Expand Up @@ -131,6 +132,40 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
if (template !== undefined) {
transitionStats = ActiveTransition(template, workspace)
}

const [showAlertPendingInQueue, setShowAlertPendingInQueue] = useState(false)
const now = dayjs()
useEffect(() => {
if (
workspace.latest_build.status === "pending" &&
workspace.latest_build.job.queue_size > 0 &&
dayjs(workspace.latest_build.created_at).isBefore(
now.subtract(5, "seconds"),
)
) {
setShowAlertPendingInQueue(true)
return
}

if (
workspace.latest_build.status === "pending" &&
workspace.latest_build.job.queue_size > 0
Copy link
Member

Choose a reason for hiding this comment

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

I noticed we're not setting setShowAlertPendingInQueue(false) anywhere? Perhaps this condition could be inverted and placed at the top with an false + early return. Should simplify the other checks as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

) {
const timer = setTimeout(() => {
if (
workspace.latest_build.status !== "pending" ||
workspace.latest_build.job.queue_size === 0
) {
return
}
setShowAlertPendingInQueue(true)
}, 5000)
Copy link
Member

Choose a reason for hiding this comment

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

How about using the difference here (now.subtract 5 sec) so that loading a new workspace doesn't reset the timer to 5 seconds?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure that I understand what you mean. Could you please elaborate a bit more about the case you have in mind?

Copy link
Member

Choose a reason for hiding this comment

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

So let's say you:

  1. Start a workspace build
  2. Wait 3 seconds
  3. Reload the workspace page
  4. Build still pending, we start the timer with hard-coded 5 second wait
  5. Queue banner is shown (time since build started 8 seconds vs 5 seconds)

Does this make sense?

Copy link
Member Author

Choose a reason for hiding this comment

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

Make sense now, thanks.

I used the diff, feel free to review that part.


return () => {
clearTimeout(timer)
}
}
}, [workspace, now])
return (
<>
<FullWidthPageHeader>
Expand Down Expand Up @@ -208,12 +243,7 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({

<TemplateVersionWarnings warnings={templateWarnings} />

<Maybe
condition={
workspace.latest_build.status === "pending" &&
workspace.latest_build.job.queue_size > 0
}
>
<Maybe condition={showAlertPendingInQueue}>
<Alert severity="info">
<AlertTitle>Workspace build is pending</AlertTitle>
<AlertDetail>
Expand Down