Skip to content

Commit

Permalink
fix: Subscribe to template updates on the workspace page (#4979)
Browse files Browse the repository at this point in the history
Fixes #4969.
  • Loading branch information
kylecarbs committed Nov 9, 2022
1 parent 5592f85 commit 3c10c7f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
17 changes: 17 additions & 0 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package coderd

import (
"context"
"database/sql"
"encoding/json"
"errors"
Expand All @@ -12,6 +13,8 @@ import (
"github.com/moby/moby/pkg/namesgenerator"
"golang.org/x/xerrors"

"cdr.dev/slog"

"github.com/coder/coder/coderd/audit"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
Expand Down Expand Up @@ -660,6 +663,8 @@ func (api *API) patchActiveTemplateVersion(rw http.ResponseWriter, r *http.Reque
newTemplate.ActiveVersionID = req.ID
aReq.New = newTemplate

api.publishTemplateUpdate(ctx, template.ID)

httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
Message: "Updated the active template version!",
})
Expand Down Expand Up @@ -946,3 +951,15 @@ func convertTemplateVersion(version database.TemplateVersion, job codersdk.Provi
CreatedBy: createdBy,
}
}

func watchTemplateChannel(id uuid.UUID) string {
return fmt.Sprintf("template:%s", id)
}

func (api *API) publishTemplateUpdate(ctx context.Context, templateID uuid.UUID) {
err := api.Pubsub.Publish(watchTemplateChannel(templateID), []byte{})
if err != nil {
api.Logger.Warn(ctx, "failed to publish template update",
slog.F("template_id", templateID), slog.Error(err))
}
}
22 changes: 19 additions & 3 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ func (api *API) watchWorkspace(rw http.ResponseWriter, r *http.Request) {
// Ignore all trace spans after this, they're not too useful.
ctx = trace.ContextWithSpan(ctx, tracing.NoopSpan)

cancelSubscribe, err := api.Pubsub.Subscribe(watchWorkspaceChannel(workspace.ID), func(_ context.Context, _ []byte) {
sendUpdate := func(_ context.Context, _ []byte) {
workspace, err := api.Database.GetWorkspaceByID(ctx, workspace.ID)
if err != nil {
_ = sendEvent(ctx, codersdk.ServerSentEvent{
Expand Down Expand Up @@ -920,7 +920,9 @@ func (api *API) watchWorkspace(rw http.ResponseWriter, r *http.Request) {
findUser(workspace.OwnerID, data.users),
),
})
})
}

cancelWorkspaceSubscribe, err := api.Pubsub.Subscribe(watchWorkspaceChannel(workspace.ID), sendUpdate)
if err != nil {
_ = sendEvent(ctx, codersdk.ServerSentEvent{
Type: codersdk.ServerSentEventTypeError,
Expand All @@ -931,7 +933,21 @@ func (api *API) watchWorkspace(rw http.ResponseWriter, r *http.Request) {
})
return
}
defer cancelSubscribe()
defer cancelWorkspaceSubscribe()

// This is required to show whether the workspace is up-to-date.
cancelTemplateSubscribe, err := api.Pubsub.Subscribe(watchTemplateChannel(workspace.TemplateID), sendUpdate)
if err != nil {
_ = sendEvent(ctx, codersdk.ServerSentEvent{
Type: codersdk.ServerSentEventTypeError,
Data: codersdk.Response{
Message: "Internal error subscribing to template events.",
Detail: err.Error(),
},
})
return
}
defer cancelTemplateSubscribe()

// An initial ping signals to the request that the server is now ready
// and the client can begin servicing a channel with data.
Expand Down
6 changes: 6 additions & 0 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,12 @@ func TestWorkspaceWatcher(t *testing.T) {
require.NoError(t, err)
wait()

err = client.UpdateActiveTemplateVersion(ctx, template.ID, codersdk.UpdateActiveTemplateVersion{
ID: template.ActiveVersionID,
})
require.NoError(t, err)
wait()

cancel()
}

Expand Down

0 comments on commit 3c10c7f

Please sign in to comment.