- Notifications
You must be signed in to change notification settings - Fork929
feat: Make workspace watching realtime instead of polling#4922
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -634,6 +634,8 @@ func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
api.publishWorkspaceUpdate(ctx, workspace.ID) | ||
aReq.New = newWorkspace | ||
rw.WriteHeader(http.StatusNoContent) | ||
} | ||
@@ -839,7 +841,7 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) { | ||
return err | ||
} | ||
if_,err := s.UpdateWorkspaceBuildByID(ctx, database.UpdateWorkspaceBuildByIDParams{ | ||
ID: build.ID, | ||
UpdatedAt: build.UpdatedAt, | ||
ProvisionerState: build.ProvisionerState, | ||
@@ -883,48 +885,65 @@ 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) { | ||
workspace, err := api.Database.GetWorkspaceByID(ctx, workspace.ID) | ||
if err != nil { | ||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeError, | ||
Data: codersdk.Response{ | ||
Message: "Internal error fetching workspace.", | ||
Detail: err.Error(), | ||
}, | ||
}) | ||
return | ||
} | ||
data, err := api.workspaceData(ctx, []database.Workspace{workspace}) | ||
if err != nil { | ||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeError, | ||
Data: codersdk.Response{ | ||
Message: "Internal error fetching workspace data.", | ||
Detail: err.Error(), | ||
}, | ||
}) | ||
return | ||
} | ||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeData, | ||
Data: convertWorkspace( | ||
workspace, | ||
data.builds[0], | ||
data.templates[0], | ||
findUser(workspace.OwnerID, data.users), | ||
), | ||
}) | ||
}) | ||
if err != nil { | ||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeError, | ||
Data: codersdk.Response{ | ||
Message: "Internal error subscribing to workspace events.", | ||
Detail: err.Error(), | ||
}, | ||
}) | ||
return | ||
} | ||
defer cancelSubscribe() | ||
// An initial ping signals to the request that the server is now ready | ||
// and the client can begin servicing a channel with data. | ||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypePing, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Is this needed because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think we want the caller to indicate readiness by sending the If we don't a race can occur where the client's request has been completed but Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Seems like this is a bit trickier than I thought... because we use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I added a comment and will leave it as-is for now because nothing comes to mind as being cleaner... if you have ideas leave em here and I'll implement! Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Hmm, I don't fully understand the race scenario, but I just realized that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Fixed that error case! Goooood catch | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-senderClosed: | ||
return | ||
} | ||
} | ||
} | ||
@@ -1213,3 +1232,15 @@ func splitQueryParameterByDelimiter(query string, delimiter rune, maintainQuotes | ||
return parts | ||
} | ||
func watchWorkspaceChannel(id uuid.UUID) string { | ||
return fmt.Sprintf("workspace:%s", id) | ||
} | ||
func (api *API) publishWorkspaceUpdate(ctx context.Context, workspaceID uuid.UUID) { | ||
err := api.Pubsub.Publish(watchWorkspaceChannel(workspaceID), []byte{}) | ||
if err != nil { | ||
api.Logger.Warn(ctx, "failed to publish workspace update", | ||
slog.F("workspace_id", workspaceID), slog.Error(err)) | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.