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

Commit6cb8387

Browse files
committed
feat(coderd): use task data model for send/logs
1 parent5bcd4ab commit6cb8387

File tree

2 files changed

+219
-274
lines changed

2 files changed

+219
-274
lines changed

‎coderd/aitasks.go‎

Lines changed: 35 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212
"time"
1313

14-
"github.com/go-chi/chi/v5"
1514
"github.com/google/uuid"
1615

1716
"cdr.dev/slog"
@@ -770,15 +769,7 @@ func (api *API) taskDelete(rw http.ResponseWriter, r *http.Request) {
770769
// workspace and validate the sidebar app health.
771770
func (api*API)taskSend(rw http.ResponseWriter,r*http.Request) {
772771
ctx:=r.Context()
773-
774-
idStr:=chi.URLParam(r,"id")
775-
taskID,err:=uuid.Parse(idStr)
776-
iferr!=nil {
777-
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
778-
Message:fmt.Sprintf("Invalid UUID %q for task ID.",idStr),
779-
})
780-
return
781-
}
772+
task:=httpmw.TaskParam(r)
782773

783774
varreq codersdk.TaskSendRequest
784775
if!httpapi.Read(ctx,rw,r,&req) {
@@ -791,7 +782,7 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) {
791782
return
792783
}
793784

794-
iferr=api.authAndDoWithTaskSidebarAppClient(r,taskID,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
785+
iferr:=api.authAndDoWithTaskAppClient(r,task,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
795786
agentAPIClient,err:=aiagentapi.NewClient(appURL.String(),aiagentapi.WithHTTPClient(client))
796787
iferr!=nil {
797788
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
@@ -850,18 +841,10 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) {
850841
// We enforce ApplicationConnect RBAC on the workspace and validate the sidebar app health.
851842
func (api*API)taskLogs(rw http.ResponseWriter,r*http.Request) {
852843
ctx:=r.Context()
853-
854-
idStr:=chi.URLParam(r,"id")
855-
taskID,err:=uuid.Parse(idStr)
856-
iferr!=nil {
857-
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
858-
Message:fmt.Sprintf("Invalid UUID %q for task ID.",idStr),
859-
})
860-
return
861-
}
844+
task:=httpmw.TaskParam(r)
862845

863846
varout codersdk.TaskLogsResponse
864-
iferr:=api.authAndDoWithTaskSidebarAppClient(r,taskID,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
847+
iferr:=api.authAndDoWithTaskAppClient(r,task,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
865848
agentAPIClient,err:=aiagentapi.NewClient(appURL.String(),aiagentapi.WithHTTPClient(client))
866849
iferr!=nil {
867850
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
@@ -909,7 +892,7 @@ func (api *API) taskLogs(rw http.ResponseWriter, r *http.Request) {
909892
httpapi.Write(ctx,rw,http.StatusOK,out)
910893
}
911894

912-
//authAndDoWithTaskSidebarAppClient centralizes the shared logic to:
895+
//authAndDoWithTaskAppClient centralizes the shared logic to:
913896
//
914897
// - Fetch the task workspace
915898
// - Authorize ApplicationConnect on the workspace
@@ -918,15 +901,31 @@ func (api *API) taskLogs(rw http.ResponseWriter, r *http.Request) {
918901
//
919902
// The provided callback receives the context, an HTTP client that dials via the
920903
// agent, and the base app URL (as a value URL) to perform any request.
921-
func (api*API)authAndDoWithTaskSidebarAppClient(
904+
func (api*API)authAndDoWithTaskAppClient(
922905
r*http.Request,
923-
taskID uuid.UUID,
906+
task database.Task,
924907
dofunc(ctx context.Context,client*http.Client,appURL*url.URL)error,
925908
)error {
926909
ctx:=r.Context()
927910

928-
workspaceID:=taskID
929-
workspace,err:=api.Database.GetWorkspaceByID(ctx,workspaceID)
911+
iftask.Status!=database.TaskStatusActive {
912+
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
913+
Message:"Task status must be active.",
914+
Detail:fmt.Sprintf("Task status is %q, it must be %q to interact with the task.",task.Status,codersdk.TaskStatusActive),
915+
})
916+
}
917+
if!task.WorkspaceID.Valid {
918+
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
919+
Message:"Task does not have a workspace.",
920+
})
921+
}
922+
if!task.WorkspaceAppID.Valid {
923+
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
924+
Message:"Task does not have a workspace app.",
925+
})
926+
}
927+
928+
workspace,err:=api.Database.GetWorkspaceByID(ctx,task.WorkspaceID.UUID)
930929
iferr!=nil {
931930
ifhttpapi.Is404Error(err) {
932931
returnhttperror.ErrResourceNotFound
@@ -942,65 +941,30 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
942941
returnhttperror.ErrResourceNotFound
943942
}
944943

945-
data,err:=api.workspaceData(ctx,[]database.Workspace{workspace})
944+
apps,err:=api.Database.GetWorkspaceAppsByAgentID(ctx,task.WorkspaceAgentID.UUID)
946945
iferr!=nil {
947946
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
948947
Message:"Internal error fetching workspace resources.",
949948
Detail:err.Error(),
950949
})
951950
}
952-
iflen(data.builds)==0||len(data.templates)==0 {
953-
returnhttperror.ErrResourceNotFound
954-
}
955-
build:=data.builds[0]
956-
ifbuild.HasAITask==nil||!*build.HasAITask||build.AITaskSidebarAppID==nil||*build.AITaskSidebarAppID==uuid.Nil {
957-
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
958-
Message:"Task is not configured with a sidebar app.",
959-
})
960-
}
961951

962-
// Find the sidebar app details to get the URL and validate app health.
963-
sidebarAppID:=*build.AITaskSidebarAppID
964-
agentID,sidebarApp,ok:=func() (uuid.UUID, codersdk.WorkspaceApp,bool) {
965-
for_,res:=rangebuild.Resources {
966-
for_,agent:=rangeres.Agents {
967-
for_,app:=rangeagent.Apps {
968-
ifapp.ID==sidebarAppID {
969-
returnagent.ID,app,true
970-
}
971-
}
972-
}
952+
varapp*database.WorkspaceApp
953+
for_,a:=rangeapps {
954+
ifa.ID==task.WorkspaceAppID.UUID {
955+
app=&a
956+
break
973957
}
974-
returnuuid.Nil, codersdk.WorkspaceApp{},false
975-
}()
976-
if!ok {
977-
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
978-
Message:"Task sidebar app not found in latest build.",
979-
})
980-
}
981-
982-
// Return an informative error if the app isn't healthy rather than trying
983-
// and failing.
984-
switchsidebarApp.Health {
985-
casecodersdk.WorkspaceAppHealthDisabled:
986-
// No health check, pass through.
987-
casecodersdk.WorkspaceAppHealthInitializing:
988-
returnhttperror.NewResponseError(http.StatusServiceUnavailable, codersdk.Response{
989-
Message:"Task sidebar app is initializing. Try again shortly.",
990-
})
991-
casecodersdk.WorkspaceAppHealthUnhealthy:
992-
returnhttperror.NewResponseError(http.StatusServiceUnavailable, codersdk.Response{
993-
Message:"Task sidebar app is unhealthy.",
994-
})
995958
}
996959

997960
// Build the direct app URL and dial the agent.
998-
ifsidebarApp.URL=="" {
961+
appURL:=app.Url.String
962+
ifappURL=="" {
999963
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
1000964
Message:"Task sidebar app URL is not configured.",
1001965
})
1002966
}
1003-
parsedURL,err:=url.Parse(sidebarApp.URL)
967+
parsedURL,err:=url.Parse(appURL)
1004968
iferr!=nil {
1005969
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
1006970
Message:"Internal error parsing task app URL.",
@@ -1015,7 +979,7 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
1015979

1016980
dialCtx,dialCancel:=context.WithTimeout(ctx,time.Second*30)
1017981
deferdialCancel()
1018-
agentConn,release,err:=api.agentProvider.AgentConn(dialCtx,agentID)
982+
agentConn,release,err:=api.agentProvider.AgentConn(dialCtx,task.WorkspaceAgentID.UUID)
1019983
iferr!=nil {
1020984
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
1021985
Message:"Failed to reach task app endpoint.",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp