-
Notifications
You must be signed in to change notification settings - Fork 927
fix(coderd): allow workspaceAgentLogs
follow to return on non-latest-build
#9382
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 all commits
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"github.com/go-chi/chi/v5" | ||
"github.com/google/uuid" | ||
"golang.org/x/exp/maps" | ||
"golang.org/x/exp/slices" | ||
"golang.org/x/mod/semver" | ||
"golang.org/x/sync/errgroup" | ||
"golang.org/x/xerrors" | ||
|
@@ -481,6 +482,15 @@ func (api *API) workspaceAgentLogs(rw http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
workspace, err := api.Database.GetWorkspaceByAgentID(ctx, workspaceAgent.ID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching workspace by agent id.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
api.WebsocketWaitMutex.Lock() | ||
api.WebsocketWaitGroup.Add(1) | ||
api.WebsocketWaitMutex.Unlock() | ||
|
@@ -556,7 +566,8 @@ func (api *API) workspaceAgentLogs(rw http.ResponseWriter, r *http.Request) { | |
go func() { | ||
defer close(bufferedLogs) | ||
|
||
for { | ||
keepGoing := true | ||
for keepGoing { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
|
@@ -565,6 +576,18 @@ func (api *API) workspaceAgentLogs(rw http.ResponseWriter, r *http.Request) { | |
t.Reset(recheckInterval) | ||
} | ||
|
||
agents, err := api.Database.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, workspace.ID) | ||
if err != nil { | ||
if xerrors.Is(err, context.Canceled) { | ||
return | ||
} | ||
logger.Warn(ctx, "failed to get workspace agents in latest build", slog.Error(err)) | ||
continue | ||
} | ||
// If the agent is no longer in the latest build, we can stop after | ||
// checking once. | ||
keepGoing = slices.ContainsFunc(agents, func(agent database.WorkspaceAgent) bool { return agent.ID == workspaceAgent.ID }) | ||
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. Why use a bool instead of just 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. So that we do one last fetch of the logs, in case something arrived between last check and new build. |
||
|
||
logs, err := api.Database.GetWorkspaceAgentLogsAfter(ctx, database.GetWorkspaceAgentLogsAfterParams{ | ||
AgentID: workspaceAgent.ID, | ||
CreatedAfter: lastSentLogID, | ||
|
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.
What happens if the user wants to view logs for an old agent intentionally? I don't think we have any mechanism in the UI for this right now so it's probably fine to leave it like this
Uh oh!
There was an error while loading. Please reload this page.
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.
It should just work, in this scenario essentially we (assuming
?follow=true
):GetWorkspaceAgentLogsAfter
(before websocket upgrade)GetWorkspaceAgentLogsAfter
one last time -> empty