- Notifications
You must be signed in to change notification settings - Fork928
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
a9fb43c
5478411
600d5bf
79758da
ec8c088
7232e65
1041f37
3d7acf1
6b74a7f
27d1020
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
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[] | ||
} | ||
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 | ||
} | ||
getLogs: { | ||
data: ProvisionerJobLog[] | ||
} | ||
}, | ||
@@ -50,23 +59,36 @@ export const workspaceBuildMachine = createMachine( | ||
}, | ||
}, | ||
logs: { | ||
initial: "gettingExistentLogs", | ||
states: { | ||
gettingExistentLogs: { | ||
invoke: { | ||
id: "getLogs", | ||
src: "getLogs", | ||
onDone: { | ||
actions: ["assignLogs"], | ||
target: "watchingLogs", | ||
}, | ||
}, | ||
}, | ||
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, | ||
}), | ||
addLog: assign({ | ||
logs: (context, event) =>{ | ||
const previousLogs = context.logs ?? [] | ||
return [...previousLogs, event.log] | ||
}, | ||
}), | ||
}, | ||
services: { | ||
getWorkspaceBuild: (ctx) => API.getWorkspaceBuild(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 CollaboratorAuthor
| ||
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", "**/*.d.ts"] | ||
} |