Skip to content

feature: Load workspace build logs from streaming #1997

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 10 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Load logs from stream
  • Loading branch information
BrunoQuaresma committed Jun 2, 2022
commit a9fb43c6c8b4c9e8b42cc5c89e2980749b9e7889
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"VMID",
"weblinks",
"webrtc",
"workspacebuilds",
"xerrors",
"xstate",
"yamux"
Expand Down
16 changes: 16 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,19 @@ export const getWorkspaceBuildLogs = async (buildname: string): Promise<TypesGen
const response = await axios.get<TypesGen.ProvisionerJobLog[]>(`/api/v2/workspacebuilds/${buildname}/logs`)
return response.data
}

export const streamWorkspaceBuildLogs = async (buildname: string): Promise<ReadableStreamDefaultReader<string>> => {
// Axios does not support HTTP stream in the browser
// https://github.com/axios/axios/issues/1474
// So we are going to use window.fetch and return a "stream" reader
const reader = await window.fetch(`/api/v2/workspacebuilds/${buildname}/logs?follow=true`).then((res) => {
if (!res.body) {
throw new Error("No body returned from the response")
}

// eslint-disable-next-line compat/compat
return res.body.pipeThrough(new TextDecoderStream()).getReader()
})

return reader
}
79 changes: 60 additions & 19 deletions site/src/xServices/workspaceBuild/workspaceBuildXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@ type LogsContext = {
getBuildError?: Error | unknown
// Logs
logs?: ProvisionerJobLog[]
getBuildLogsError?: Error | unknown
}

type LogsEvent =
| {
type: "ADD_LOGS"
logs: ProvisionerJobLog[]
}
| {
type: "NO_MORE_LOGS"
}

export const workspaceBuildMachine = createMachine(
{
id: "workspaceBuildState",
schema: {
context: {} as LogsContext,
events: {} as LogsEvent,
services: {} as {
getWorkspaceBuild: {
data: WorkspaceBuild
}
getWorkspaceBuildLogs: {
getLogs: {
data: ProvisionerJobLog[]
}
},
Expand Down Expand Up @@ -50,25 +59,38 @@ export const workspaceBuildMachine = createMachine(
},
},
logs: {
initial: "gettingLogs",
initial: "gettingExistentLogs",
states: {
gettingLogs: {
entry: "clearGetBuildLogsError",
gettingExistentLogs: {
invoke: {
src: "getWorkspaceBuildLogs",
id: "getLogs",
src: "getLogs",
onDone: {
target: "idle",
actions: "assignLogs",
},
onError: {
target: "idle",
actions: "assignGetBuildLogsError",
actions: ["assignLogs"],
target: "watchingLogs",
},
},
},
idle: {},
watchingLogs: {
id: "watchingLogs",
invoke: {
id: "streamWorkspaceBuildLogs",
src: "streamWorkspaceBuildLogs",
},
},
},
on: {
ADD_LOGS: {
actions: "addNewLogs",
},
NO_MORE_LOGS: {
target: "loaded",
},
},
},
loaded: {
type: "final",
},
},
},
{
Expand All @@ -87,16 +109,35 @@ export const workspaceBuildMachine = createMachine(
assignLogs: assign({
logs: (_, event) => event.data,
}),
assignGetBuildLogsError: assign({
getBuildLogsError: (_, event) => event.data,
}),
clearGetBuildLogsError: assign({
getBuildLogsError: (_) => undefined,
addNewLogs: assign({
logs: (context, event) => {
const previousLogs = context.logs ?? []
return [...previousLogs, ...event.logs]
},
}),
},
services: {
getWorkspaceBuild: (ctx) => API.getWorkspaceBuild(ctx.buildId),
getWorkspaceBuildLogs: (ctx) => API.getWorkspaceBuildLogs(ctx.buildId),
getLogs: async (ctx) => API.getWorkspaceBuildLogs(ctx.buildId),
streamWorkspaceBuildLogs: (ctx) => async (callback) => {
const reader = await API.streamWorkspaceBuildLogs(ctx.buildId)

// Watching for the stream
// eslint-disable-next-line no-constant-condition
while (true) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to worry about error handling or cleanup if done never returns?

Copy link
Collaborator Author

@BrunoQuaresma BrunoQuaresma Jun 3, 2022

Choose a reason for hiding this comment

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

It is a good point. I think for now we can rely on done being always called, why:

  • We are using a polyfill(https://canjs.com/doc/can-ndjson-stream.html) because Firefox does not support some stream APIs
  • I took a look and I didn't find anything related to "read failure" or something related
  • If an error is thrown, it will stop the execution anyway
  • This error, in particular, looks tricky to handle so I would expect someone having issues with that to think of a proper solution
  • In summary, I don't know how to do that and it looks complex to handle 😆, so I would not invest too much time on that right now, but if you have an idea how to solve that, I would love to learn and implement it for sure.

Copy link
Member

Choose a reason for hiding this comment

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

Ha! Thanks for the breakdown - what you said makes sense and I think you're right to hold off. If this becomes an issue in the future, we can look into calling cancel on the ReadableStream object.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ahh, nice!

const { value, done } = await reader.read()

if (done) {
callback("NO_MORE_LOGS")
break
}

if (value) {
const logs = value.split("\n").map((jsonString) => JSON.parse(jsonString) as ProvisionerJobLog)
callback({ type: "ADD_LOGS", logs })
}
}
},
},
},
)