-
Notifications
You must be signed in to change notification settings - Fork 888
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
Changes from 9 commits
a9fb43c
5478411
600d5bf
79758da
ec8c088
7232e65
1041f37
3d7acf1
6b74a7f
27d1020
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 |
---|---|---|
|
@@ -72,6 +72,7 @@ | |
"VMID", | ||
"weblinks", | ||
"webrtc", | ||
"workspacebuilds", | ||
"xerrors", | ||
"xstate", | ||
"yamux" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module "can-ndjson-stream" { | ||
function ndjsonStream<TValueType>(body: ReadableStream<Uint8Array> | null): Promise<ReadableStream<TValueType>> | ||
export default ndjsonStream | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,19 +9,28 @@ type LogsContext = { | |
getBuildError?: Error | unknown | ||
// Logs | ||
logs?: ProvisionerJobLog[] | ||
getBuildLogsError?: Error | unknown | ||
} | ||
|
||
type LogsEvent = | ||
| { | ||
type: "ADD_LOG" | ||
log: 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[] | ||
} | ||
}, | ||
|
@@ -50,23 +59,36 @@ 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", | ||
}, | ||
}, | ||
loaded: { | ||
type: "final", | ||
}, | ||
}, | ||
on: { | ||
ADD_LOG: { | ||
actions: "addLog", | ||
}, | ||
NO_MORE_LOGS: { | ||
target: "logs.loaded", | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
@@ -87,16 +109,32 @@ export const workspaceBuildMachine = createMachine( | |
assignLogs: assign({ | ||
logs: (_, event) => event.data, | ||
}), | ||
assignGetBuildLogsError: assign({ | ||
getBuildLogsError: (_, event) => event.data, | ||
}), | ||
clearGetBuildLogsError: assign({ | ||
getBuildLogsError: (_) => undefined, | ||
addLog: assign({ | ||
logs: (context, event) => { | ||
const previousLogs = context.logs ?? [] | ||
return [...previousLogs, event.log] | ||
}, | ||
}), | ||
}, | ||
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, @typescript-eslint/no-unnecessary-condition | ||
while (true) { | ||
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. Do we need to worry about error handling or cleanup if 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. It is a good point. I think for now we can rely on done being always called, why:
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. 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 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. Ahh, nice! |
||
const { value, done } = await reader.read() | ||
|
||
if (done) { | ||
callback("NO_MORE_LOGS") | ||
break | ||
} | ||
|
||
callback({ type: "ADD_LOG", log: value }) | ||
} | ||
}, | ||
}, | ||
}, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["node_modules", "_jest"], | ||
"include": ["**/*.stories.tsx", "**/*.test.tsx"] | ||
"include": ["**/*.stories.tsx", "**/*.test.tsx", "**/*.d.ts"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we put "seconds" in a
Language
block?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definetely!