From 42b722d49dd1d9e1b95bddd14f7bec92a80cef57 Mon Sep 17 00:00:00 2001 From: BrunoQuaresma Date: Tue, 3 Sep 2024 16:51:09 +0000 Subject: [PATCH 1/2] fix(site): fix agent logs streaming for third party apps --- .../resources/AgentLogs/useAgentLogs.test.tsx | 11 +++++------ .../resources/AgentLogs/useAgentLogs.ts | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/site/src/modules/resources/AgentLogs/useAgentLogs.test.tsx b/site/src/modules/resources/AgentLogs/useAgentLogs.test.tsx index b88196fd732aa..e1aaccc40d6f7 100644 --- a/site/src/modules/resources/AgentLogs/useAgentLogs.test.tsx +++ b/site/src/modules/resources/AgentLogs/useAgentLogs.test.tsx @@ -28,7 +28,7 @@ describe("useAgentLogs", () => { expect(wsSpy).not.toHaveBeenCalled(); }); - it("should return existing logs without network calls", async () => { + it("should return existing logs without network calls if state is off", async () => { const queryClient = createTestQueryClient(); queryClient.setQueryData( agentLogsKey(MockWorkspace.id, MockWorkspaceAgent.id), @@ -39,7 +39,7 @@ describe("useAgentLogs", () => { const { result } = renderUseAgentLogs(queryClient, { workspaceId: MockWorkspace.id, agentId: MockWorkspaceAgent.id, - agentLifeCycleState: "ready", + agentLifeCycleState: "off", }); await waitFor(() => { expect(result.current).toHaveLength(5); @@ -48,12 +48,12 @@ describe("useAgentLogs", () => { expect(wsSpy).not.toHaveBeenCalled(); }); - it("should fetch logs when empty and should not connect to WebSocket when not starting", async () => { + it("should fetch logs when empty", async () => { const queryClient = createTestQueryClient(); const fetchSpy = jest .spyOn(API, "getWorkspaceAgentLogs") .mockResolvedValueOnce(generateLogs(5)); - const wsSpy = jest.spyOn(APIModule, "watchWorkspaceAgentLogs"); + jest.spyOn(APIModule, "watchWorkspaceAgentLogs"); const { result } = renderUseAgentLogs(queryClient, { workspaceId: MockWorkspace.id, agentId: MockWorkspaceAgent.id, @@ -63,10 +63,9 @@ describe("useAgentLogs", () => { expect(result.current).toHaveLength(5); }); expect(fetchSpy).toHaveBeenCalledWith(MockWorkspaceAgent.id); - expect(wsSpy).not.toHaveBeenCalled(); }); - it("should fetch logs and connect to websocket when agent is starting", async () => { + it("should fetch logs and connect to websocket", async () => { const queryClient = createTestQueryClient(); const logs = generateLogs(5); const fetchSpy = jest diff --git a/site/src/modules/resources/AgentLogs/useAgentLogs.ts b/site/src/modules/resources/AgentLogs/useAgentLogs.ts index c8e67e2824cec..7102f2b69f552 100644 --- a/site/src/modules/resources/AgentLogs/useAgentLogs.ts +++ b/site/src/modules/resources/AgentLogs/useAgentLogs.ts @@ -17,16 +17,13 @@ export type UseAgentLogsOptions = Readonly<{ /** * Defines a custom hook that gives you all workspace agent logs for a given - * workspace. - * - * Depending on the status of the workspace, all logs may or may not be - * available. + * workspace.Depending on the status of the workspace, all logs may or may not + * be available. */ export function useAgentLogs( options: UseAgentLogsOptions, ): readonly WorkspaceAgentLog[] | undefined { const { workspaceId, agentId, agentLifeCycleState, enabled = true } = options; - const queryClient = useQueryClient(); const queryOptions = agentLogs(workspaceId, agentId); const { data: logs, isFetched } = useQuery({ ...queryOptions, enabled }); @@ -55,7 +52,17 @@ export function useAgentLogs( }); useEffect(() => { - if (agentLifeCycleState !== "starting" || !isFetched) { + // Stream data only for new logs. Old logs should be loaded beforehand + // using a regular fetch to avoid overloading the websocket with all + // logs at once. + if (!isFetched) { + return; + } + + // If the agent is off, we don't need to stream logs. This is the only state + // where the Coder API can't receive logs for the agent from third-party + // apps like envbuilder. + if(agentLifeCycleState === "off") { return; } From 7e8bd3021de8124564adc6b9f9bae0c736ef1ca1 Mon Sep 17 00:00:00 2001 From: BrunoQuaresma Date: Tue, 3 Sep 2024 16:57:33 +0000 Subject: [PATCH 2/2] Fix fmt --- site/src/modules/resources/AgentLogs/useAgentLogs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/src/modules/resources/AgentLogs/useAgentLogs.ts b/site/src/modules/resources/AgentLogs/useAgentLogs.ts index 7102f2b69f552..a53f1d882dc60 100644 --- a/site/src/modules/resources/AgentLogs/useAgentLogs.ts +++ b/site/src/modules/resources/AgentLogs/useAgentLogs.ts @@ -62,7 +62,7 @@ export function useAgentLogs( // If the agent is off, we don't need to stream logs. This is the only state // where the Coder API can't receive logs for the agent from third-party // apps like envbuilder. - if(agentLifeCycleState === "off") { + if (agentLifeCycleState === "off") { return; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy