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

Commitd46a3c7

Browse files
committed
feat(coderd): use task data model for send/logs
Updatescoder/internal#976
1 parentf9dbe5a commitd46a3c7

File tree

4 files changed

+218
-281
lines changed

4 files changed

+218
-281
lines changed

‎cli/exp_task_logs_test.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323
funcTest_TaskLogs(t*testing.T) {
2424
t.Parallel()
2525

26-
t.Skip("TODO(mafredri): Remove, fixed down-stack!")
27-
2826
testMessages:= []agentapisdk.Message{
2927
{
3028
Id:0,

‎cli/exp_task_send_test.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
funcTest_TaskSend(t*testing.T) {
2323
t.Parallel()
2424

25-
t.Skip("TODO(mafredri): Remove, fixed down-stack!")
26-
2725
t.Run("ByWorkspaceName_WithArgument",func(t*testing.T) {
2826
t.Parallel()
2927
ctx:=testutil.Context(t,testutil.WaitLong)

‎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"
@@ -755,15 +754,7 @@ func (api *API) taskDelete(rw http.ResponseWriter, r *http.Request) {
755754
// workspace and validate the sidebar app health.
756755
func (api*API)taskSend(rw http.ResponseWriter,r*http.Request) {
757756
ctx:=r.Context()
758-
759-
idStr:=chi.URLParam(r,"id")
760-
taskID,err:=uuid.Parse(idStr)
761-
iferr!=nil {
762-
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
763-
Message:fmt.Sprintf("Invalid UUID %q for task ID.",idStr),
764-
})
765-
return
766-
}
757+
task:=httpmw.TaskParam(r)
767758

768759
varreq codersdk.TaskSendRequest
769760
if!httpapi.Read(ctx,rw,r,&req) {
@@ -776,7 +767,7 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) {
776767
return
777768
}
778769

779-
iferr=api.authAndDoWithTaskSidebarAppClient(r,taskID,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
770+
iferr:=api.authAndDoWithTaskAppClient(r,task,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
780771
agentAPIClient,err:=aiagentapi.NewClient(appURL.String(),aiagentapi.WithHTTPClient(client))
781772
iferr!=nil {
782773
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
@@ -835,18 +826,10 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) {
835826
// We enforce ApplicationConnect RBAC on the workspace and validate the sidebar app health.
836827
func (api*API)taskLogs(rw http.ResponseWriter,r*http.Request) {
837828
ctx:=r.Context()
838-
839-
idStr:=chi.URLParam(r,"id")
840-
taskID,err:=uuid.Parse(idStr)
841-
iferr!=nil {
842-
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
843-
Message:fmt.Sprintf("Invalid UUID %q for task ID.",idStr),
844-
})
845-
return
846-
}
829+
task:=httpmw.TaskParam(r)
847830

848831
varout codersdk.TaskLogsResponse
849-
iferr:=api.authAndDoWithTaskSidebarAppClient(r,taskID,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
832+
iferr:=api.authAndDoWithTaskAppClient(r,task,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
850833
agentAPIClient,err:=aiagentapi.NewClient(appURL.String(),aiagentapi.WithHTTPClient(client))
851834
iferr!=nil {
852835
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
@@ -894,7 +877,7 @@ func (api *API) taskLogs(rw http.ResponseWriter, r *http.Request) {
894877
httpapi.Write(ctx,rw,http.StatusOK,out)
895878
}
896879

897-
//authAndDoWithTaskSidebarAppClient centralizes the shared logic to:
880+
//authAndDoWithTaskAppClient centralizes the shared logic to:
898881
//
899882
// - Fetch the task workspace
900883
// - Authorize ApplicationConnect on the workspace
@@ -903,15 +886,31 @@ func (api *API) taskLogs(rw http.ResponseWriter, r *http.Request) {
903886
//
904887
// The provided callback receives the context, an HTTP client that dials via the
905888
// agent, and the base app URL (as a value URL) to perform any request.
906-
func (api*API)authAndDoWithTaskSidebarAppClient(
889+
func (api*API)authAndDoWithTaskAppClient(
907890
r*http.Request,
908-
taskID uuid.UUID,
891+
task database.Task,
909892
dofunc(ctx context.Context,client*http.Client,appURL*url.URL)error,
910893
)error {
911894
ctx:=r.Context()
912895

913-
workspaceID:=taskID
914-
workspace,err:=api.Database.GetWorkspaceByID(ctx,workspaceID)
896+
iftask.Status!=database.TaskStatusActive {
897+
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
898+
Message:"Task status must be active.",
899+
Detail:fmt.Sprintf("Task status is %q, it must be %q to interact with the task.",task.Status,codersdk.TaskStatusActive),
900+
})
901+
}
902+
if!task.WorkspaceID.Valid {
903+
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
904+
Message:"Task does not have a workspace.",
905+
})
906+
}
907+
if!task.WorkspaceAppID.Valid {
908+
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
909+
Message:"Task does not have a workspace app.",
910+
})
911+
}
912+
913+
workspace,err:=api.Database.GetWorkspaceByID(ctx,task.WorkspaceID.UUID)
915914
iferr!=nil {
916915
ifhttpapi.Is404Error(err) {
917916
returnhttperror.ErrResourceNotFound
@@ -927,65 +926,30 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
927926
returnhttperror.ErrResourceNotFound
928927
}
929928

930-
data,err:=api.workspaceData(ctx,[]database.Workspace{workspace})
929+
apps,err:=api.Database.GetWorkspaceAppsByAgentID(ctx,task.WorkspaceAgentID.UUID)
931930
iferr!=nil {
932931
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
933932
Message:"Internal error fetching workspace resources.",
934933
Detail:err.Error(),
935934
})
936935
}
937-
iflen(data.builds)==0||len(data.templates)==0 {
938-
returnhttperror.ErrResourceNotFound
939-
}
940-
build:=data.builds[0]
941-
ifbuild.HasAITask==nil||!*build.HasAITask||build.AITaskSidebarAppID==nil||*build.AITaskSidebarAppID==uuid.Nil {
942-
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
943-
Message:"Task is not configured with a sidebar app.",
944-
})
945-
}
946936

947-
// Find the sidebar app details to get the URL and validate app health.
948-
sidebarAppID:=*build.AITaskSidebarAppID
949-
agentID,sidebarApp,ok:=func() (uuid.UUID, codersdk.WorkspaceApp,bool) {
950-
for_,res:=rangebuild.Resources {
951-
for_,agent:=rangeres.Agents {
952-
for_,app:=rangeagent.Apps {
953-
ifapp.ID==sidebarAppID {
954-
returnagent.ID,app,true
955-
}
956-
}
957-
}
937+
varapp*database.WorkspaceApp
938+
for_,a:=rangeapps {
939+
ifa.ID==task.WorkspaceAppID.UUID {
940+
app=&a
941+
break
958942
}
959-
returnuuid.Nil, codersdk.WorkspaceApp{},false
960-
}()
961-
if!ok {
962-
returnhttperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
963-
Message:"Task sidebar app not found in latest build.",
964-
})
965-
}
966-
967-
// Return an informative error if the app isn't healthy rather than trying
968-
// and failing.
969-
switchsidebarApp.Health {
970-
casecodersdk.WorkspaceAppHealthDisabled:
971-
// No health check, pass through.
972-
casecodersdk.WorkspaceAppHealthInitializing:
973-
returnhttperror.NewResponseError(http.StatusServiceUnavailable, codersdk.Response{
974-
Message:"Task sidebar app is initializing. Try again shortly.",
975-
})
976-
casecodersdk.WorkspaceAppHealthUnhealthy:
977-
returnhttperror.NewResponseError(http.StatusServiceUnavailable, codersdk.Response{
978-
Message:"Task sidebar app is unhealthy.",
979-
})
980943
}
981944

982945
// Build the direct app URL and dial the agent.
983-
ifsidebarApp.URL=="" {
946+
appURL:=app.Url.String
947+
ifappURL=="" {
984948
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
985949
Message:"Task sidebar app URL is not configured.",
986950
})
987951
}
988-
parsedURL,err:=url.Parse(sidebarApp.URL)
952+
parsedURL,err:=url.Parse(appURL)
989953
iferr!=nil {
990954
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
991955
Message:"Internal error parsing task app URL.",
@@ -1000,7 +964,7 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
1000964

1001965
dialCtx,dialCancel:=context.WithTimeout(ctx,time.Second*30)
1002966
deferdialCancel()
1003-
agentConn,release,err:=api.agentProvider.AgentConn(dialCtx,agentID)
967+
agentConn,release,err:=api.agentProvider.AgentConn(dialCtx,task.WorkspaceAgentID.UUID)
1004968
iferr!=nil {
1005969
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
1006970
Message:"Failed to reach task app endpoint.",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp