mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
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" ```
72 lines
2.3 KiB
Go
72 lines
2.3 KiB
Go
package wspubsub
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
// WorkspaceEventChannel can be used to subscribe to events for
|
|
// workspaces owned by the provided user ID.
|
|
func WorkspaceEventChannel(ownerID uuid.UUID) string {
|
|
return fmt.Sprintf("workspace_owner:%s", ownerID)
|
|
}
|
|
|
|
func HandleWorkspaceEvent(cb func(ctx context.Context, payload WorkspaceEvent, err error)) func(ctx context.Context, message []byte, err error) {
|
|
return func(ctx context.Context, message []byte, err error) {
|
|
if err != nil {
|
|
cb(ctx, WorkspaceEvent{}, xerrors.Errorf("workspace event pubsub: %w", err))
|
|
return
|
|
}
|
|
var payload WorkspaceEvent
|
|
if err := json.Unmarshal(message, &payload); err != nil {
|
|
cb(ctx, WorkspaceEvent{}, xerrors.Errorf("unmarshal workspace event"))
|
|
return
|
|
}
|
|
if err := payload.Validate(); err != nil {
|
|
cb(ctx, payload, xerrors.Errorf("validate workspace event"))
|
|
return
|
|
}
|
|
cb(ctx, payload, err)
|
|
}
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
type WorkspaceEventKind string
|
|
|
|
const (
|
|
WorkspaceEventKindStateChange WorkspaceEventKind = "state_change"
|
|
WorkspaceEventKindStatsUpdate WorkspaceEventKind = "stats_update"
|
|
WorkspaceEventKindMetadataUpdate WorkspaceEventKind = "mtd_update"
|
|
WorkspaceEventKindAppHealthUpdate WorkspaceEventKind = "app_health"
|
|
|
|
WorkspaceEventKindAgentLifecycleUpdate WorkspaceEventKind = "agt_lifecycle_update"
|
|
WorkspaceEventKindAgentConnectionUpdate WorkspaceEventKind = "agt_connection_update"
|
|
WorkspaceEventKindAgentFirstLogs WorkspaceEventKind = "agt_first_logs"
|
|
WorkspaceEventKindAgentLogsOverflow WorkspaceEventKind = "agt_logs_overflow"
|
|
WorkspaceEventKindAgentTimeout WorkspaceEventKind = "agt_timeout"
|
|
)
|
|
|
|
func (w *WorkspaceEvent) Validate() error {
|
|
if w.WorkspaceID == uuid.Nil {
|
|
return xerrors.New("workspaceID must be set")
|
|
}
|
|
if w.Kind == "" {
|
|
return xerrors.New("kind must be set")
|
|
}
|
|
if w.Kind == WorkspaceEventKindAgentLifecycleUpdate && w.AgentID == nil {
|
|
return xerrors.New("agentID must be set for Agent events")
|
|
}
|
|
return nil
|
|
}
|