8000 chore: Add watch workspace endpoint by f0ssel · Pull Request #1493 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

chore: Add watch workspace endpoint #1493

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
move to channel over WorkspaceWatcher
  • Loading branch information
f0ssel committed May 18, 2022
commit f029650d30328d483651bd2b63b858cf6d7ba5c5
17 changes: 8 additions & 9 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,15 +634,14 @@ func TestWorkspaceWatcher(t *testing.T) {
w, err := client.Workspace(context.Background(), workspace.ID)
require.NoError(t, err)

ww, err := client.WatchWorkspace(context.Background(), w.ID)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wc, err := client.WatchWorkspace(ctx, w.ID)
require.NoError(t, err)
defer ww.Close()
for i := 0; i < 5; i++ {
_, err := ww.Read(context.Background())
require.NoError(t, err)
for i := 0; i < 3; i++ {
_, more := <-wc
require.True(t, more)
}
err = ww.Close()
require.NoError(t, err)
_, err = ww.Read(context.Background())
require.Error(t, err)
cancel()
require.EqualValues(t, codersdk.Workspace{}, <-wc)
}
50 changes: 23 additions & 27 deletions codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,34 @@ func (c *Client) WorkspaceBuildByName(ctx context.Context, workspace uuid.UUID,
return workspaceBuild, json.NewDecoder(res.Body).Decode(&workspaceBuild)
}

type WorkspaceWatcher struct {
conn *websocket.Conn
}

func (w *WorkspaceWatcher) Read(ctx context.Context) (Workspace, error) {
var ws Workspace
err := wsjson.Read(ctx, w.conn, &ws)
if err != nil {
return ws, xerrors.Errorf("read workspace: %w", err)
}

return ws, nil
}

func (w *WorkspaceWatcher) Close() error {
err := w.conn.Close(websocket.StatusNormalClosure, "")
if err != nil {
return xerrors.Errorf("closing workspace watcher: %w", err)
}

return nil
}

func (c *Client) WatchWorkspace(ctx context.Context, id uuid.UUID) (*WorkspaceWatcher, error) {
func (c *Client) WatchWorkspace(ctx context.Context, id uuid.UUID) (<-chan Workspace, error) {
conn, err := c.dialWebsocket(ctx, fmt.Sprintf("/api/v2/workspaces/%s/watch", id))
if err != nil {
return nil, err
}
wc := make(chan Workspace, 256)

go func() {
defer close(wc)
defer conn.Close(websocket.StatusNormalClosure, "")

for {
select {
case <-ctx.Done():
return
default:
var ws Workspace
err := wsjson.Read(ctx, conn, &ws)
if err != nil {
conn.Close(websocket.StatusInternalError, "failed to read workspace")
return
}
wc <- ws
}
}
}()

return &WorkspaceWatcher{
conn: conn,
}, nil
return wc, nil
}

// UpdateWorkspaceAutostartRequest is a request to update a workspace's autostart schedule.
Expand Down
0