Skip to content

chore: watch workspace endpoint #4060

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 34 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9c5c047
fix: match term color
f0ssel Aug 25, 2022
b3a171b
fmt:
f0ssel Aug 25, 2022
4e4a365
Add resources to workspace watch endpoint
f0ssel Aug 25, 2022
fae7dd2
better wrapped errors
f0ssel Aug 25, 2022
8e5cda7
support sse
f0ssel Aug 25, 2022
e82e201
correct schema
f0ssel Aug 25, 2022
8635e4a
share SetupSSE func
f0ssel Aug 25, 2022
784b361
fix events
f0ssel Sep 14, 2022
f32cddd
better err
f0ssel Sep 14, 2022
800e0a8
Fix casting
f0ssel Sep 14, 2022
efcbbab
fix test
f0ssel Sep 14, 2022
cf59581
remove dead code
f0ssel Sep 14, 2022
35f5894
panic on bad cast
f0ssel Sep 14, 2022
316f0ba
fix flusher
f0ssel Sep 14, 2022
953bb62
rename serversideeventtype
f0ssel Sep 14, 2022
e1f238c
formatting
f0ssel Sep 14, 2022
0ebb9aa
make gen
f0ssel Sep 14, 2022
ec97a71
fix var
f0ssel Sep 14, 2022
d717208
server-sent
f0ssel Sep 15, 2022
a1646b7
ts types
f0ssel Sep 15, 2022
5ca27e5
new convert workspace build
f0ssel Sep 16, 2022
19ff07e
consolidate
f0ssel Sep 16, 2022
13a6e3f
add workspace build converter
f0ssel Sep 16, 2022
03cc931
consolidate data collection
f0ssel Sep 16, 2022
7efae96
more consolidation
f0ssel Sep 16, 2022
4178f5a
split conversion and data fetching
f0ssel Sep 16, 2022
e7614dd
fix missing owner
f0ssel Sep 16, 2022
1f7440b
fix errors
f0ssel Sep 16, 2022
c38f55b
fix js tests
f0ssel Sep 16, 2022
eebd889
revert webpack changes
f0ssel Sep 16, 2022
86da07d
pr comments
f0ssel Sep 16, 2022
ef37372
make gen
f0ssel Sep 16, 2022
026dd2f
save some queries
f0ssel Sep 16, 2022
4e95a99
always return slive
f0ssel Sep 16, 2022
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
16 changes: 16 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,22 @@ func (q *fakeQuerier) GetWorkspaceResourcesByJobID(_ context.Context, jobID uuid
return resources, nil
}

func (q *fakeQuerier) GetWorkspaceResourcesByJobIDs(_ context.Context, jobIDs []uuid.UUID) ([]database.WorkspaceResource, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

resources := make([]database.WorkspaceResource, 0)
for _, resource := range q.provisionerJobResources {
for _, jobID := range jobIDs {
if resource.JobID != jobID {
continue
}
resources = append(resources, resource)
}
}
return resources, nil
}

func (q *fakeQuerier) GetWorkspaceResourcesCreatedAfter(_ context.Context, after time.Time) ([]database.WorkspaceResource, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down
1 change: 1 addition & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions coderd/database/queries/workspaceresources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ FROM
WHERE
job_id = $1;

-- name: GetWorkspaceResourcesByJobIDs :many
SELECT
*
FROM
workspace_resources
WHERE
job_id = ANY(@ids :: uuid [ ]);

-- name: GetWorkspaceResourcesCreatedAfter :many
SELECT * FROM workspace_resources WHERE created_at > $1;

Expand Down
76 changes: 76 additions & 0 deletions coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package httpapi

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"strings"
"sync"
"time"

"github.com/go-playground/validator/v10"

Expand Down Expand Up @@ -144,3 +148,75 @@ func WebsocketCloseSprintf(format string, vars ...any) string {

return msg
}

func ServerSentEventSender(rw http.ResponseWriter, r *http.Request) (func(ctx context.Context, sse codersdk.ServerSentEvent) error, error) {
var mu sync.Mutex
h := rw.Header()
h.Set("Content-Type", "text/event-stream")
h.Set("Cache-Control", "no-cache")
h.Set("Connection", "keep-alive")
h.Set("X-Accel-Buffering", "no")

f, ok := rw.(http.Flusher)
if !ok {
panic("http.ResponseWriter is not http.Flusher")
}

// Send a heartbeat every 15 seconds to avoid the connection being killed.
go func() {
ticker := time.NewTicker(time.Second * 15)
defer ticker.Stop()

for {
select {
case <-r.Context().Done():
return
case <-ticker.C:
mu.Lock()
_, err := io.WriteString(rw, fmt.Sprintf("event: %s\n\n", codersdk.ServerSentEventTypePing))
if err != nil {
mu.Unlock()
return
}
f.Flush()
mu.Unlock()
}
}
}()

sendEvent := func(ctx context.Context, sse codersdk.ServerSentEvent) error {
if ctx.Err() != nil {
return ctx.Err()
}

buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)

_, err := buf.Write([]byte(fmt.Sprintf("event: %s\ndata: ", sse.Type)))
if err != nil {
return err
}

err = enc.Encode(sse.Data)
if err != nil {
return err
}

err = buf.WriteByte('\n')
if err != nil {
return err
}

mu.Lock()
defer mu.Unlock()
_, err = rw.Write(buf.Bytes())
if err != nil {
return err
}
f.Flush()

return nil
}

return sendEvent, nil
}
8 changes: 8 additions & 0 deletions coderd/tracing/status_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ func (w *StatusWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
func (w *StatusWriter) ResponseBody() []byte {
return w.responseBody
}

func (w *StatusWriter) Flush() {
f, ok := w.ResponseWriter.(http.Flusher)
if !ok {
panic("http.ResponseWriter is not http.Flusher")
}
f.Flush()
}
Loading