-
Notifications
You must be signed in to change notification settings - Fork 943
chore(site): reduce fetch interval on workspaces page #18725
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 all commits
1708422
e1b59cb
641d001
10c484d
04f7090
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import { getErrorDetail, getErrorMessage } from "api/errors"; | |
import { workspacePermissionsByOrganization } from "api/queries/organizations"; | ||
import { templates } from "api/queries/templates"; | ||
import { workspaces } from "api/queries/workspaces"; | ||
import type { Workspace } from "api/typesGenerated"; | ||
import type { Workspace, WorkspaceStatus } from "api/typesGenerated"; | ||
import { useFilter } from "components/Filter/Filter"; | ||
import { useUserFilterMenu } from "components/Filter/UserFilter"; | ||
import { displayError } from "components/GlobalSnackbar/utils"; | ||
|
@@ -22,6 +22,18 @@ import { WorkspacesPageView } from "./WorkspacesPageView"; | |
import { useBatchActions } from "./batchActions"; | ||
import { useStatusFilterMenu, useTemplateFilterMenu } from "./filter/menus"; | ||
|
||
// To reduce the number of fetches, we reduce the fetch interval if there are no | ||
// active workspace builds. | ||
const ACTIVE_BUILD_STATUSES: WorkspaceStatus[] = [ | ||
"canceling", | ||
"deleting", | ||
"pending", | ||
"starting", | ||
"stopping", | ||
]; | ||
const ACTIVE_BUILDS_REFRESH_INTERVAL = 5_000; | ||
const NO_ACTIVE_BUILDS_REFRESH_INTERVAL = 30_000; | ||
|
||
function useSafeSearchParams() { | ||
// Have to wrap setSearchParams because React Router doesn't make sure that | ||
// the function's memory reference stays stable on each render, even though | ||
|
@@ -78,8 +90,23 @@ const WorkspacesPage: FC = () => { | |
const { data, error, refetch } = useQuery({ | ||
...workspacesQueryOptions, | ||
refetchInterval: ({ state }) => { | ||
return state.error ? false : 5_000; | ||
if (state.error) return false; | ||
|
||
// Default to 5s interval until first fetch completes | ||
if (!state.data) return ACTIVE_BUILDS_REFRESH_INTERVAL; | ||
|
||
// Check if any workspace has an active build | ||
const hasActiveBuilds = state.data.workspaces?.some((workspace) => { | ||
const status = workspace.latest_build.status; | ||
return ACTIVE_BUILD_STATUSES.includes(status); | ||
}); | ||
|
||
// Poll every 5s if there are active builds, otherwise every 30s | ||
return hasActiveBuilds | ||
? ACTIVE_BUILDS_REFRESH_INTERVAL | ||
: NO_ACTIVE_BUILDS_REFRESH_INTERVAL; | ||
}, | ||
refetchOnWindowFocus: "always", | ||
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. review: |
||
}); | ||
|
||
const [checkedWorkspaces, setCheckedWorkspaces] = useState< | ||
|
Uh oh!
There was an error while loading. Please reload this page.