mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
chore: Add watch workspace endpoint (#1493)
This commit is contained in:
@ -9,6 +9,8 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/xerrors"
|
||||
"nhooyr.io/websocket"
|
||||
"nhooyr.io/websocket/wsjson"
|
||||
|
||||
"github.com/coder/coder/coderd/database"
|
||||
)
|
||||
@ -98,6 +100,36 @@ func (c *Client) WorkspaceBuildByName(ctx context.Context, workspace uuid.UUID,
|
||||
return workspaceBuild, json.NewDecoder(res.Body).Decode(&workspaceBuild)
|
||||
}
|
||||
|
||||
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 wc, nil
|
||||
}
|
||||
|
||||
// UpdateWorkspaceAutostartRequest is a request to update a workspace's autostart schedule.
|
||||
type UpdateWorkspaceAutostartRequest struct {
|
||||
Schedule string `json:"schedule"`
|
||||
|
Reference in New Issue
Block a user