mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: send workspace pubsub events by owner id (#14964)
We currently send empty payloads to pubsub channels of the form `workspace:<workspace_id>` to notify listeners of updates to workspaces (such as for refreshing the workspace dashboard). To support https://github.com/coder/coder/issues/14716, we'll instead send `WorkspaceEvent` payloads to pubsub channels of the form `workspace_owner:<owner_id>`. This enables a listener to receive events for all workspaces owned by a user. This PR replaces the usage of the old channels without modifying any existing behaviors. ``` type WorkspaceEvent struct { Kind WorkspaceEventKind `json:"kind"` WorkspaceID uuid.UUID `json:"workspace_id" format:"uuid"` // AgentID is only set for WorkspaceEventKindAgent* events // (excluding AgentTimeout) AgentID *uuid.UUID `json:"agent_id,omitempty" format:"uuid"` } ``` We've defined `WorkspaceEventKind`s based on how the old channel was used, but it's not yet necessary to inspect the types of any of the events, as the existing listeners are designed to fire off any of them. ``` WorkspaceEventKindStateChange WorkspaceEventKind = "state_change" WorkspaceEventKindStatsUpdate WorkspaceEventKind = "stats_update" WorkspaceEventKindMetadataUpdate WorkspaceEventKind = "mtd_update" WorkspaceEventKindAppHealthUpdate WorkspaceEventKind = "app_health" WorkspaceEventKindAgentLifecycleUpdate WorkspaceEventKind = "agt_lifecycle_update" WorkspaceEventKindAgentLogsUpdate WorkspaceEventKind = "agt_logs_update" WorkspaceEventKindAgentConnectionUpdate WorkspaceEventKind = "agt_connection_update" WorkspaceEventKindAgentLogsOverflow WorkspaceEventKind = "agt_logs_overflow" WorkspaceEventKindAgentTimeout WorkspaceEventKind = "agt_timeout" ```
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/httpmw"
|
||||
"github.com/coder/coder/v2/coderd/telemetry"
|
||||
"github.com/coder/coder/v2/coderd/util/ptr"
|
||||
"github.com/coder/coder/v2/coderd/wspubsub"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
"github.com/coder/coder/v2/tailnet"
|
||||
tailnetproto "github.com/coder/coder/v2/tailnet/proto"
|
||||
@@ -132,11 +133,13 @@ func (api *API) workspaceAgentRPC(rw http.ResponseWriter, r *http.Request) {
|
||||
|
||||
closeCtx, closeCtxCancel := context.WithCancel(ctx)
|
||||
defer closeCtxCancel()
|
||||
monitor := api.startAgentYamuxMonitor(closeCtx, workspaceAgent, build, mux)
|
||||
monitor := api.startAgentYamuxMonitor(closeCtx, workspace, workspaceAgent, build, mux)
|
||||
defer monitor.close()
|
||||
|
||||
agentAPI := agentapi.New(agentapi.Options{
|
||||
AgentID: workspaceAgent.ID,
|
||||
AgentID: workspaceAgent.ID,
|
||||
OwnerID: workspace.OwnerID,
|
||||
WorkspaceID: workspace.ID,
|
||||
|
||||
Ctx: api.ctx,
|
||||
Log: logger,
|
||||
@@ -160,7 +163,6 @@ func (api *API) workspaceAgentRPC(rw http.ResponseWriter, r *http.Request) {
|
||||
Experiments: api.Experiments,
|
||||
|
||||
// Optional:
|
||||
WorkspaceID: build.WorkspaceID, // saves the extra lookup later
|
||||
UpdateAgentMetricsFn: api.UpdateAgentMetrics,
|
||||
})
|
||||
|
||||
@@ -225,11 +227,14 @@ func (y *yamuxPingerCloser) Ping(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (api *API) startAgentYamuxMonitor(ctx context.Context,
|
||||
workspaceAgent database.WorkspaceAgent, workspaceBuild database.WorkspaceBuild,
|
||||
workspace database.Workspace,
|
||||
workspaceAgent database.WorkspaceAgent,
|
||||
workspaceBuild database.WorkspaceBuild,
|
||||
mux *yamux.Session,
|
||||
) *agentConnectionMonitor {
|
||||
monitor := &agentConnectionMonitor{
|
||||
apiCtx: api.ctx,
|
||||
workspace: workspace,
|
||||
workspaceAgent: workspaceAgent,
|
||||
workspaceBuild: workspaceBuild,
|
||||
conn: &yamuxPingerCloser{mux: mux},
|
||||
@@ -250,7 +255,7 @@ func (api *API) startAgentYamuxMonitor(ctx context.Context,
|
||||
}
|
||||
|
||||
type workspaceUpdater interface {
|
||||
publishWorkspaceUpdate(ctx context.Context, workspaceID uuid.UUID)
|
||||
publishWorkspaceUpdate(ctx context.Context, ownerID uuid.UUID, event wspubsub.WorkspaceEvent)
|
||||
}
|
||||
|
||||
type pingerCloser interface {
|
||||
@@ -262,6 +267,7 @@ type agentConnectionMonitor struct {
|
||||
apiCtx context.Context
|
||||
cancel context.CancelFunc
|
||||
wg sync.WaitGroup
|
||||
workspace database.Workspace
|
||||
workspaceAgent database.WorkspaceAgent
|
||||
workspaceBuild database.WorkspaceBuild
|
||||
conn pingerCloser
|
||||
@@ -393,7 +399,11 @@ func (m *agentConnectionMonitor) monitor(ctx context.Context) {
|
||||
)
|
||||
}
|
||||
}
|
||||
m.updater.publishWorkspaceUpdate(finalCtx, m.workspaceBuild.WorkspaceID)
|
||||
m.updater.publishWorkspaceUpdate(finalCtx, m.workspace.OwnerID, wspubsub.WorkspaceEvent{
|
||||
Kind: wspubsub.WorkspaceEventKindAgentConnectionUpdate,
|
||||
WorkspaceID: m.workspaceBuild.WorkspaceID,
|
||||
AgentID: &m.workspaceAgent.ID,
|
||||
})
|
||||
}()
|
||||
reason := "disconnect"
|
||||
defer func() {
|
||||
@@ -407,7 +417,11 @@ func (m *agentConnectionMonitor) monitor(ctx context.Context) {
|
||||
reason = err.Error()
|
||||
return
|
||||
}
|
||||
m.updater.publishWorkspaceUpdate(ctx, m.workspaceBuild.WorkspaceID)
|
||||
m.updater.publishWorkspaceUpdate(ctx, m.workspace.OwnerID, wspubsub.WorkspaceEvent{
|
||||
Kind: wspubsub.WorkspaceEventKindAgentConnectionUpdate,
|
||||
WorkspaceID: m.workspaceBuild.WorkspaceID,
|
||||
AgentID: &m.workspaceAgent.ID,
|
||||
})
|
||||
|
||||
ticker := time.NewTicker(m.pingPeriod)
|
||||
defer ticker.Stop()
|
||||
@@ -441,7 +455,11 @@ func (m *agentConnectionMonitor) monitor(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
if connectionStatusChanged {
|
||||
m.updater.publishWorkspaceUpdate(ctx, m.workspaceBuild.WorkspaceID)
|
||||
m.updater.publishWorkspaceUpdate(ctx, m.workspace.OwnerID, wspubsub.WorkspaceEvent{
|
||||
Kind: wspubsub.WorkspaceEventKindAgentConnectionUpdate,
|
||||
WorkspaceID: m.workspaceBuild.WorkspaceID,
|
||||
AgentID: &m.workspaceAgent.ID,
|
||||
})
|
||||
}
|
||||
err = checkBuildIsLatest(ctx, m.db, m.workspaceBuild)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user