Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6a4a085

Browse files
committed
avoid extra db call for apps, data is already available
1 parent04d6c60 commit6a4a085

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

‎coderd/aitasks.go‎

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"cdr.dev/slog"
2323
"github.com/coder/coder/v2/coderd/audit"
2424
"github.com/coder/coder/v2/coderd/database"
25-
"github.com/coder/coder/v2/coderd/database/dbauthz"
2625
"github.com/coder/coder/v2/coderd/httpapi"
2726
"github.com/coder/coder/v2/coderd/httpapi/httperror"
2827
"github.com/coder/coder/v2/coderd/httpmw"
@@ -725,25 +724,21 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
725724
Message:"Task is not configured with a sidebar app.",
726725
})
727726
}
728-
sidebarAppID:=*build.AITaskSidebarAppID
729727

730728
// Find the sidebar app details to get the URL and validate app health.
731-
agentIDs:=make([]uuid.UUID,0,len(build.Resources))
732-
for_,res:=rangebuild.Resources {
733-
for_,agent:=rangeres.Agents {
734-
agentIDs=append(agentIDs,agent.ID)
729+
sidebarAppID:=*build.AITaskSidebarAppID
730+
agentID,sidebarApp,ok:=func() (uuid.UUID, codersdk.WorkspaceApp,bool) {
731+
for_,res:=rangebuild.Resources {
732+
for_,agent:=rangeres.Agents {
733+
for_,app:=rangeagent.Apps {
734+
ifapp.ID==sidebarAppID {
735+
returnagent.ID,app,true
736+
}
737+
}
738+
}
735739
}
736-
}
737-
// TODO(mafredri): Can we avoid dbauthz.AsSystemRestricted(ctx)?
738-
//nolint:gocritic // GetWorkspaceAppsByAgentIDs requires system restricted context.
739-
apps,err:=api.Database.GetWorkspaceAppsByAgentIDs(dbauthz.AsSystemRestricted(ctx),agentIDs)
740-
iferr!=nil&&!errors.Is(err,sql.ErrNoRows) {
741-
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
742-
Message:"Internal error fetching workspace apps.",
743-
Detail:err.Error(),
744-
})
745-
}
746-
sidebarApp,ok:=slice.Find(apps,func(app database.WorkspaceApp)bool {returnapp.ID==sidebarAppID })
740+
returnuuid.Nil, codersdk.WorkspaceApp{},false
741+
}()
747742
if!ok {
748743
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
749744
Message:"Task sidebar app not found in latest build.",
@@ -753,25 +748,25 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
753748
// Return an informative error if the app isn't healthy rather than trying
754749
// and failing.
755750
switchsidebarApp.Health {
756-
casedatabase.WorkspaceAppHealthDisabled:
751+
casecodersdk.WorkspaceAppHealthDisabled:
757752
// No health check, pass through.
758-
casedatabase.WorkspaceAppHealthInitializing:
753+
casecodersdk.WorkspaceAppHealthInitializing:
759754
returnhttperror.NewResponseError(http.StatusServiceUnavailable, codersdk.Response{
760755
Message:"Task sidebar app is initializing. Try again shortly.",
761756
})
762-
casedatabase.WorkspaceAppHealthUnhealthy:
757+
casecodersdk.WorkspaceAppHealthUnhealthy:
763758
returnhttperror.NewResponseError(http.StatusServiceUnavailable, codersdk.Response{
764759
Message:"Task sidebar app is unhealthy.",
765760
})
766761
}
767762

768763
// Build the direct app URL and dial the agent.
769-
if!sidebarApp.Url.Valid||sidebarApp.Url.String=="" {
764+
ifsidebarApp.URL=="" {
770765
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
771766
Message:"Task sidebar app URL is not configured.",
772767
})
773768
}
774-
parsedURL,err:=url.Parse(sidebarApp.Url.String)
769+
parsedURL,err:=url.Parse(sidebarApp.URL)
775770
iferr!=nil {
776771
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
777772
Message:"Internal error parsing task app URL.",
@@ -786,7 +781,7 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
786781

787782
dialCtx,dialCancel:=context.WithTimeout(ctx,time.Second*30)
788783
deferdialCancel()
789-
agentConn,release,err:=api.agentProvider.AgentConn(dialCtx,sidebarApp.AgentID)
784+
agentConn,release,err:=api.agentProvider.AgentConn(dialCtx,agentID)
790785
iferr!=nil {
791786
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
792787
Message:"Failed to reach task app endpoint.",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp