Skip to content

Commit 4d29e22

Browse files
committed
Replace the sse-based autoreload in bulk actions and queues page with the simpler autoRevalidate hook
1 parent 56a8424 commit 4d29e22

File tree

6 files changed

+38
-225
lines changed

6 files changed

+38
-225
lines changed

apps/webapp/app/env.server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,9 @@ const EnvironmentSchema = z
10281028
TASK_EVENT_PARTITIONING_ENABLED: z.string().default("0"),
10291029
TASK_EVENT_PARTITIONED_WINDOW_IN_SECONDS: z.coerce.number().int().default(60), // 1 minute
10301030

1031-
QUEUE_SSE_AUTORELOAD_INTERVAL_MS: z.coerce.number().int().default(5_000),
1032-
QUEUE_SSE_AUTORELOAD_TIMEOUT_MS: z.coerce.number().int().default(60_000),
1033-
10341031
DEPLOYMENTS_AUTORELOAD_POLL_INTERVAL_MS: z.coerce.number().int().default(5_000),
1032+
BULK_ACTION_AUTORELOAD_POLL_INTERVAL_MS: z.coerce.number().int().default(1_000),
1033+
QUEUES_AUTORELOAD_POLL_INTERVAL_MS: z.coerce.number().int().default(5_000),
10351034

10361035
SLACK_BOT_TOKEN: z.string().optional(),
10371036
SLACK_SIGNUP_REASON_CHANNEL_ID: z.string().optional(),

apps/webapp/app/hooks/useAutoRevalidate.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { useEffect } from "react";
44
type UseAutoRevalidateOptions = {
55
interval?: number; // in milliseconds
66
onFocus?: boolean;
7+
disabled?: boolean;
78
};
89

910
export function useAutoRevalidate(options: UseAutoRevalidateOptions = {}) {
10-
const { interval = 5000, onFocus = true } = options;
11+
const { interval = 5000, onFocus = true, disabled = false } = options;
1112
const revalidator = useRevalidator();
1213

1314
useEffect(() => {
14-
if (!interval || interval <= 0) return;
15+
if (!interval || interval <= 0 || disabled) return;
1516

1617
const intervalId = setInterval(() => {
1718
if (revalidator.state === "loading") {
@@ -21,10 +22,10 @@ export function useAutoRevalidate(options: UseAutoRevalidateOptions = {}) {
2122
}, interval);
2223

2324
return () => clearInterval(intervalId);
24-
}, [interval]);
25+
}, [interval, disabled]);
2526

2627
useEffect(() => {
27-
if (!onFocus) return;
28+
if (!onFocus || disabled) return;
2829

2930
const handleFocus = () => {
3031
if (document.visibilityState === "visible" && revalidator.state !== "loading") {
@@ -41,7 +42,7 @@ export function useAutoRevalidate(options: UseAutoRevalidateOptions = {}) {
4142
document.removeEventListener("visibilitychange", handleFocus);
4243
window.removeEventListener("focus", handleFocus);
4344
};
44-
}, [onFocus]);
45+
}, [onFocus, disabled]);
4546

4647
return revalidator;
4748
}

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.bulk-actions.$bulkActionParam/route.tsx

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { ArrowPathIcon } from "@heroicons/react/20/solid";
2-
import { Form, useRevalidator } from "@remix-run/react";
2+
import { Form } from "@remix-run/react";
33
import { type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/server-runtime";
44
import { tryCatch } from "@trigger.dev/core";
55
import type { BulkActionType } from "@trigger.dev/database";
66
import { motion } from "framer-motion";
7-
import { useEffect } from "react";
87
import { typedjson, useTypedLoaderData } from "remix-typedjson";
98
import { z } from "zod";
109
import { ExitIcon } from "~/assets/icons/ExitIcon";
@@ -18,8 +17,9 @@ import { Paragraph } from "~/components/primitives/Paragraph";
1817
import * as Property from "~/components/primitives/PropertyTable";
1918
import { BulkActionStatusCombo, BulkActionTypeCombo } from "~/components/runs/v3/BulkAction";
2019
import { UserAvatar } from "~/components/UserProfilePhoto";
20+
import { env } from "~/env.server";
21+
import { useAutoRevalidate } from "~/hooks/useAutoRevalidate";
2122
import { useEnvironment } from "~/hooks/useEnvironment";
22-
import { useEventSource } from "~/hooks/useEventSource";
2323
import { useOrganization } from "~/hooks/useOrganizations";
2424
import { useProject } from "~/hooks/useProject";
2525
import { redirectWithErrorMessage, redirectWithSuccessMessage } from "~/models/message.server";
@@ -72,7 +72,9 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
7272
throw new Error(error.message);
7373
}
7474

75-
return typedjson({ bulkAction: data });
75+
const autoReloadPollIntervalMs = env.BULK_ACTION_AUTORELOAD_POLL_INTERVAL_MS;
76+
77+
return typedjson({ bulkAction: data, autoReloadPollIntervalMs });
7678
} catch (error) {
7779
console.error(error);
7880
throw new Response(undefined, {
@@ -130,30 +132,16 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
130132
};
131133

132134
export default function Page() {
133-
const { bulkAction } = useTypedLoaderData<typeof loader>();
135+
const { bulkAction, autoReloadPollIntervalMs } = useTypedLoaderData<typeof loader>();
134136
const organization = useOrganization();
135137
const project = useProject();
136138
const environment = useEnvironment();
137139

138-
const disabled = bulkAction.status !== "PENDING";
139-
140-
const streamedEvents = useEventSource(
141-
`/resources/orgs/${organization.slug}/projects/${project.slug}/env/${environment.id}/runs/bulkaction/${bulkAction.friendlyId}/stream`,
142-
{
143-
event: "progress",
144-
disabled,
145-
}
146-
);
147-
148-
const revalidation = useRevalidator();
149-
150-
useEffect(() => {
151-
if (disabled || streamedEvents === null) {
152-
return;
153-
}
154-
155-
revalidation.revalidate();
156-
}, [streamedEvents, disabled]);
140+
useAutoRevalidate({
141+
interval: autoReloadPollIntervalMs,
142+
onFocus: true,
143+
disabled: bulkAction.status !== "PENDING",
144+
});
157145

158146
return (
159147
<div className="grid h-full max-h-full grid-rows-[2.5rem_2.5rem_1fr_3.25rem] overflow-hidden bg-background-bright">

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.queues/route.tsx

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@ import {
88
RectangleStackIcon,
99
} from "@heroicons/react/20/solid";
1010
import { DialogClose } from "@radix-ui/react-dialog";
11-
import {
12-
Form,
13-
useNavigate,
14-
useNavigation,
15-
useRevalidator,
16-
useSearchParams,
17-
type MetaFunction,
18-
} from "@remix-run/react";
11+
import { Form, useNavigation, useSearchParams, type MetaFunction } from "@remix-run/react";
1912
import { type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/server-runtime";
2013
import type { RuntimeEnvironmentType } from "@trigger.dev/database";
2114
import { useEffect, useState } from "react";
@@ -30,7 +23,7 @@ import { Feedback } from "~/components/Feedback";
3023
import { PageBody, PageContainer } from "~/components/layout/AppLayout";
3124
import { BigNumber } from "~/components/metrics/BigNumber";
3225
import { Badge } from "~/components/primitives/Badge";
33-
import { Button, ButtonVariant, LinkButton } from "~/components/primitives/Buttons";
26+
import { Button, type ButtonVariant, LinkButton } from "~/components/primitives/Buttons";
3427
import { Callout } from "~/components/primitives/Callout";
3528
import { Dialog, DialogContent, DialogHeader, DialogTrigger } from "~/components/primitives/Dialog";
3629
import { FormButtons } from "~/components/primitives/FormButtons";
@@ -56,7 +49,6 @@ import {
5649
TooltipTrigger,
5750
} from "~/components/primitives/Tooltip";
5851
import { useEnvironment } from "~/hooks/useEnvironment";
59-
import { useEventSource } from "~/hooks/useEventSource";
6052
import { useOrganization } from "~/hooks/useOrganizations";
6153
import { useProject } from "~/hooks/useProject";
6254
import { redirectWithErrorMessage, redirectWithSuccessMessage } from "~/models/message.server";
@@ -74,6 +66,8 @@ import { Header3 } from "~/components/primitives/Headers";
7466
import { Input } from "~/components/primitives/Input";
7567
import { useThrottle } from "~/hooks/useThrottle";
7668
import { RunsIcon } from "~/assets/icons/RunsIcon";
69+
import { useAutoRevalidate } from "~/hooks/useAutoRevalidate";
70+
import { env } from "~/env.server";
7771

7872
const SearchParamsSchema = z.object({
7973
query: z.string().optional(),
@@ -121,9 +115,12 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
121115

122116
const environmentQueuePresenter = new EnvironmentQueuePresenter();
123117

118+
const autoReloadPollIntervalMs = env.QUEUES_AUTORELOAD_POLL_INTERVAL_MS;
119+
124120
return typedjson({
125121
...queues,
126122
environment: await environmentQueuePresenter.call(environment),
123+
autoReloadPollIntervalMs,
127124
});
128125
} catch (error) {
129126
console.error(error);
@@ -217,28 +214,23 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
217214
};
218215

219216
export default function Page() {
220-
const { environment, queues, success, pagination, code, totalQueues, hasFilters } =
221-
useTypedLoaderData<typeof loader>();
217+
const {
218+
environment,
219+
queues,
220+
success,
221+
pagination,
222+
code,
223+
totalQueues,
224+
hasFilters,
225+
autoReloadPollIntervalMs,
226+
} = useTypedLoaderData<typeof loader>();
222227

223228
const organization = useOrganization();
224229
const project = useProject();
225230
const env = useEnvironment();
226231
const plan = useCurrentPlan();
227232

228-
// Reload the page periodically
229-
const streamedEvents = useEventSource(
230-
`/resources/orgs/${organization.slug}/projects/${project.slug}/env/${env.slug}/queues/stream`,
231-
{
232-
event: "update",
233-
}
234-
);
235-
236-
const revalidation = useRevalidator();
237-
useEffect(() => {
238-
if (streamedEvents) {
239-
revalidation.revalidate();
240-
}
241-
}, [streamedEvents]);
233+
useAutoRevalidate({ interval: autoReloadPollIntervalMs, onFocus: true });
242234

243235
const limitStatus =
244236
environment.running === environment.concurrencyLimit * environment.burstFactor

apps/webapp/app/routes/resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.queues.stream.tsx

Lines changed: 0 additions & 64 deletions
This file was deleted.

apps/webapp/app/routes/resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.runs.bulkaction.$bulkActionParam.stream.tsx

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)