Skip to content

Commit 1195f31

Browse files
authored
chore(site): reduce fetch interval on workspaces page (#18725)
Relates to coder/internal#720 * Reduces workspaces data refetch interval if no builds are pending * Sets `refetchOnWindowFocus: always` to mitigate impact of reduced polling duration
1 parent 8202514 commit 1195f31

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

site/src/pages/WorkspacesPage/WorkspacesPage.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getErrorDetail, getErrorMessage } from "api/errors";
22
import { workspacePermissionsByOrganization } from "api/queries/organizations";
33
import { templates } from "api/queries/templates";
44
import { workspaces } from "api/queries/workspaces";
5-
import type { Workspace } from "api/typesGenerated";
5+
import type { Workspace, WorkspaceStatus } from "api/typesGenerated";
66
import { useFilter } from "components/Filter/Filter";
77
import { useUserFilterMenu } from "components/Filter/UserFilter";
88
import { displayError } from "components/GlobalSnackbar/utils";
@@ -22,6 +22,18 @@ import { WorkspacesPageView } from "./WorkspacesPageView";
2222
import { useBatchActions } from "./batchActions";
2323
import { useStatusFilterMenu, useTemplateFilterMenu } from "./filter/menus";
2424

25+
// To reduce the number of fetches, we reduce the fetch interval if there are no
26+
// active workspace builds.
27+
const ACTIVE_BUILD_STATUSES: WorkspaceStatus[] = [
28+
"canceling",
29+
"deleting",
30+
"pending",
31+
"starting",
32+
"stopping",
33+
];
34+
const ACTIVE_BUILDS_REFRESH_INTERVAL = 5_000;
35+
const NO_ACTIVE_BUILDS_REFRESH_INTERVAL = 30_000;
36+
2537
function useSafeSearchParams() {
2638
// Have to wrap setSearchParams because React Router doesn't make sure that
2739
// the function's memory reference stays stable on each render, even though
@@ -78,8 +90,23 @@ const WorkspacesPage: FC = () => {
7890
const { data, error, refetch } = useQuery({
7991
...workspacesQueryOptions,
8092
refetchInterval: ({ state }) => {
81-
return state.error ? false : 5_000;
93+
if (state.error) return false;
94+
95+
// Default to 5s interval until first fetch completes
96+
if (!state.data) return ACTIVE_BUILDS_REFRESH_INTERVAL;
97+
98+
// Check if any workspace has an active build
99+
const hasActiveBuilds = state.data.workspaces?.some((workspace) => {
100+
const status = workspace.latest_build.status;
101+
return ACTIVE_BUILD_STATUSES.includes(status);
102+
});
103+
104+
// Poll every 5s if there are active builds, otherwise every 30s
105+
return hasActiveBuilds
106+
? ACTIVE_BUILDS_REFRESH_INTERVAL
107+
: NO_ACTIVE_BUILDS_REFRESH_INTERVAL;
82108
},
109+
refetchOnWindowFocus: "always",
83110
});
84111

85112
const [checkedWorkspaces, setCheckedWorkspaces] = useState<

0 commit comments

Comments
 (0)