-
Notifications
You must be signed in to change notification settings - Fork 937
fix!: remove startup logs eof for streaming #8528
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
10000
|
@@ -280,81 +280,61 @@ func (api *API) patchWorkspaceAgentStartupLogs(rw http.ResponseWriter, r *http.R | |
level = append(level, parsedLevel) | ||
} | ||
|
||
var logs []database.WorkspaceAgentStartupLog | ||
// Ensure logs are not written after script ended. | ||
scriptEndedError := xerrors.New("startup script has ended") | ||
err := api.Database.InTx(func(db database.Store) error { | ||
state, err := db.GetWorkspaceAgentLifecycleStateByID(ctx, workspaceAgent.ID) | ||
if err != nil { | ||
return xerrors.Errorf("workspace agent startup script status: %w", err) | ||
logs, err := api.Database.InsertWorkspaceAgentStartupLogs(ctx, database.InsertWorkspaceAgentStartupLogsParams{ | ||
AgentID: workspaceAgent.ID, | ||
CreatedAt: createdAt, | ||
Output: output, | ||
Level: level, | ||
OutputLength: int32(outputLength), | ||
}) | ||
if err != nil { | ||
if !database.IsStartupLogsLimitError(err) { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to upload startup logs", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
if state.ReadyAt.Valid { | ||
// The agent startup script has already ended, so we don't want to | ||
// process any more logs. | ||
return scriptEndedError | ||
if workspaceAgent.StartupLogsOverflowed { | ||
httpapi.Write(ctx, rw, http.StatusRequestEntityTooLarge, codersdk.Response{ | ||
Message: "Startup logs limit exceeded", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
logs, err = db.InsertWorkspaceAgentStartupLogs(ctx, database.InsertWorkspaceAgentStartupLogsParams{ | ||
AgentID: workspaceAgent.ID, | ||
CreatedAt: createdAt, | ||
Output: output, | ||
Level: level, | ||
OutputLength: int32(outputLength), | ||
err := api.Database.UpdateWorkspaceAgentStartupLogOverflowByID(ctx, database.UpdateWorkspaceAgentStartupLogOverflowByIDParams{ | ||
ID: workspaceAgent.ID, | ||
StartupLogsOverflowed: true, | ||
}) | ||
return err | ||
}, nil) | ||
if err != nil { | ||
if errors.Is(err, scriptEndedError) { | ||
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ | ||
Message: "Failed to upload logs, startup script has already ended.", | ||
if err != nil { | ||
// We don't want to return here, because the agent will retry | ||
// on failure and this isn't a huge deal. The overflow state | ||
// is just a hint to the user that the logs are incomplete. | ||
api.Logger.Warn(ctx, "failed to update workspace agent startup log overflow", slog.Error(err)) | ||
} | ||
|
||
resource, err := api.Database.GetWorkspaceResourceByID(ctx, workspaceAgent.ResourceID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Failed to get workspace resource.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
if database.IsStartupLogsLimitError(err) { | ||
if !workspaceAgent.StartupLogsOverflowed { | ||
err := api.Database.UpdateWorkspaceAgentStartupLogOverflowByID(ctx, database.UpdateWorkspaceAgentStartupLogOverflowByIDParams{ | ||
ID: workspaceAgent.ID, | ||
StartupLogsOverflowed: true, | ||
}) | ||
if err != nil { | ||
// We don't want to return here, because the agent will retry | ||
// on failure and this isn't a huge deal. The overflow state | ||
// is just a hint to the user that the logs are incomplete. | ||
api.Logger.Warn(ctx, "failed to update workspace agent startup log overflow", slog.Error(err)) | ||
} | ||
|
||
resource, err := api.Database.GetWorkspaceResourceByID(ctx, workspaceAgent.ResourceID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Failed to get workspace resource.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
build, err := api.Database.GetWorkspaceBuildByJobID(ctx, resource.JobID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Internal error fetching workspace build job.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
api.publishWorkspaceUpdate(ctx, build.WorkspaceID) | ||
} | ||
|
||
httpapi.Write(ctx, rw, http.StatusRequestEntityTooLarge, codersdk.Response{ | ||
Message: "Startup logs limit exceeded", | ||
build, err := api.Database.GetWorkspaceBuildByJobID(ctx, resource.JobID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Internal error fetching workspace build job.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to upload startup logs", | ||
Detail: err.Error(), | ||
|
||
api.publishWorkspaceUpdate(ctx, build.WorkspaceID) | ||
|
||
httpapi.Write(ctx, rw, http.StatusRequestEntityTooLarge, codersdk.Response{ | ||
Message: "Startup logs limit exceeded", | ||
}) | ||
return | ||
} | ||
|
@@ -497,18 +477,6 @@ func (api *API) workspaceAgentStartupLogs(rw http.ResponseWriter, r *http.Reques | |
return | ||
} | ||
|
||
if workspaceAgent.ReadyAt.Valid { | ||
// Fast path, the startup script has finished running, so we can close | ||
// the connection. | ||
return | ||
} | ||
if !codersdk.WorkspaceAgentLifecycle(workspaceAgent.LifecycleState).Starting() { | ||
// Backwards compatibility: Avoid waiting forever in case this agent is | ||
// older than the current release and has already reported the ready | ||
// state. | ||
return | ||
} | ||
|
||
lastSentLogID := after | ||
if len(logs) > 0 { | ||
lastSentLogID = logs[len(logs)-1].ID | ||
|
@@ -543,11 +511,9 @@ func (api *API) workspaceAgentStartupLogs(rw http.ResponseWriter, r *http.Reques | |
t := time.NewTicker(recheckInterval) | ||
defer t.Stop() | ||
|
||
var state database.GetWorkspaceAgentLifecycleStateByIDRow | ||
go func() { | ||
defer close(bufferedLogs) | ||
|
||
var err error | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
|
@@ -557,17 +523,6 @@ func (api *API) workspaceAgentStartupLogs(rw http.ResponseWriter, r *http.Reques | |
t.Reset(recheckInterval) | ||
} | ||
|
||
if !state.ReadyAt.Valid { | ||
state, err = api.Database.GetWorkspaceAgentLifecycleStateByID(ctx, workspaceAgent.ID) | ||
if err != nil { | ||
if xerrors.Is(err, context.Canceled) { | ||
return | ||
} | ||
logger.Warn(ctx, "failed to get workspace agent lifecycle state", slog.Error(err)) | ||
continue | ||
} | ||
} | ||
|
||
logs, err := api.Database.GetWorkspaceAgentStartupLogsAfter(ctx, database.GetWorkspaceAgentStartupLogsAfterParams{ | ||
AgentID: workspaceAgent.ID, | ||
CreatedAfter: lastSentLogID, | ||
|
@@ -580,9 +535,7 @@ func (api *API) workspaceAgentStartupLogs(rw http.ResponseWriter, r *http.Reques | |
continue | ||
} | ||
if len(logs) == 0 { | ||
if state.ReadyAt.Valid { | ||
return | ||
} | ||
// Just keep listening - more logs might come in the future! | ||
continue | ||
} | ||
|
||
|
@@ -1689,12 +1642,6 @@ func (api *API) workspaceAgentReportLifecycle(rw http.ResponseWriter, r *http.Re | |
return | ||
} | ||
|
||
if readyAt.Valid { | ||
api.publishWorkspaceAgentStartupLogsUpdate(ctx, workspaceAgent.ID, agentsdk.StartupLogsNotifyMessage{ | ||
EndOfLogs: true, | ||
}) | ||
} | ||
|
||
api.publishWorkspaceUpdate(ctx, workspace.ID) | ||
|
||
httpapi.Write(ctx, rw, http.StatusNoContent, nil) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We can remove the ready state stuff higher up too.