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

fix(coderd/agentapi): publish workspace update event on sub agent#18461

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
mafredri wants to merge1 commit intomain
base:main
Choose a base branch
Loading
frommafredri/fix-coderd-agentapi-workspaceupdate
Draft
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletionscoderd/agentapi/api.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -196,13 +196,14 @@ func New(opts Options) *API {
}

api.SubAgentAPI = &SubAgentAPI{
OwnerID: opts.OwnerID,
OrganizationID: opts.OrganizationID,
AgentID: opts.AgentID,
AgentFn: api.agent,
Log: opts.Log,
Clock: opts.Clock,
Database: opts.Database,
OwnerID: opts.OwnerID,
OrganizationID: opts.OrganizationID,
AgentID: opts.AgentID,
AgentFn: api.agent,
PublishWorkspaceUpdateFn: api.publishWorkspaceUpdate,
Log: opts.Log,
Clock: opts.Clock,
Database: opts.Database,
}

return api
Expand Down
45 changes: 41 additions & 4 deletionscoderd/agentapi/subagent.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,16 +15,18 @@ import (
agentproto "github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/wspubsub"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisioner"
"github.com/coder/quartz"
)

type SubAgentAPI struct {
OwnerID uuid.UUID
OrganizationID uuid.UUID
AgentID uuid.UUID
AgentFn func(context.Context) (database.WorkspaceAgent, error)
OwnerID uuid.UUID
OrganizationID uuid.UUID
AgentID uuid.UUID
AgentFn func(context.Context) (database.WorkspaceAgent, error)
PublishWorkspaceUpdateFn func(context.Context, *database.WorkspaceAgent, wspubsub.WorkspaceEventKind) error

Log slog.Logger
Clock quartz.Clock
Expand DownExpand Up@@ -216,6 +218,18 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create
}
}

if a.PublishWorkspaceUpdateFn != nil {
err = a.PublishWorkspaceUpdateFn(ctx, &subAgent, wspubsub.WorkspaceEventKindStateChange)
if err != nil {
a.Log.Error(ctx, "failed to publish workspace update after creating sub agent",
slog.Error(err),
slog.F("sub_agent_id", subAgent.ID),
slog.F("sub_agent_name", subAgent.Name),
)
// We don't return an error here because we created the sub agent.
}
}

return &agentproto.CreateSubAgentResponse{
Agent: &agentproto.SubAgent{
Name: subAgent.Name,
Expand All@@ -235,10 +249,33 @@ func (a *SubAgentAPI) DeleteSubAgent(ctx context.Context, req *agentproto.Delete
return nil, err
}

subAgent, err := a.Database.GetWorkspaceAgentByID(ctx, subAgentID)
if err != nil {
if xerrors.Is(err, sql.ErrNoRows) {
return nil, codersdk.ValidationError{
Field: "id",
Detail: fmt.Sprintf("sub agent with id %q does not exist", req.Id),
}
}
return nil, xerrors.Errorf("get workspace agent by id %q: %w", subAgentID, err)
}

if err := a.Database.DeleteWorkspaceSubAgentByID(ctx, subAgentID); err != nil {
return nil, err
}

if a.PublishWorkspaceUpdateFn != nil {
err = a.PublishWorkspaceUpdateFn(ctx, &subAgent, wspubsub.WorkspaceEventKindStateChange)
if err != nil {
a.Log.Error(ctx, "failed to publish workspace update after deleting sub agent",
slog.Error(err),
slog.F("sub_agent_id", subAgent.ID),
slog.F("sub_agent_name", subAgent.Name),
)
// We don't return an error here because we deleted the sub agent.
}
}

return &agentproto.DeleteSubAgentResponse{}, nil
}

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp