Skip to content

Commit 7e1abd1

Browse files
committed
Claim prebuild and rename
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent eebbeb5 commit 7e1abd1

File tree

12 files changed

+87
-50
lines changed

12 files changed

+87
-50
lines changed

coderd/apidoc/docs.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbauthz/dbauthz.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1078,9 +1078,11 @@ func (q *querier) BulkMarkNotificationMessagesSent(ctx context.Context, arg data
10781078
return q.db.BulkMarkNotificationMessagesSent(ctx, arg)
10791079
}
10801080

1081-
func (q *querier) ClaimPrebuild(ctx context.Context, newOwnerID uuid.UUID) (uuid.UUID, error) {
1081+
func (q *querier) ClaimPrebuild(ctx context.Context, newOwnerID database.ClaimPrebuildParams) (database.ClaimPrebuildRow, error) {
10821082
if err := q.authorizeContext(ctx, policy.ActionDelete, rbac.ResourceWorkspace); err != nil {
1083-
return uuid.Nil, err
1083+
return database.ClaimPrebuildRow{
1084+
ID: uuid.Nil,
1085+
}, err
10841086
}
10851087
return q.db.ClaimPrebuild(ctx, newOwnerID)
10861088
}

coderd/database/dbmem/dbmem.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,7 @@ func (*FakeQuerier) BulkMarkNotificationMessagesSent(_ context.Context, arg data
15851585
return int64(len(arg.IDs)), nil
15861586
}
15871587

1588-
func (q *FakeQuerier) ClaimPrebuild(ctx context.Context, newOwnerID uuid.UUID) (uuid.UUID, error) {
1588+
func (q *FakeQuerier) ClaimPrebuild(ctx context.Context, newOwnerID database.ClaimPrebuildParams) (database.ClaimPrebuildRow, error) {
15891589
panic("not implemented")
15901590
}
15911591

coderd/database/dbmetrics/querymetrics.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+30-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/prebuilds.sql

+13-11
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,17 @@ GROUP BY t.using_active_version, t.template_id, t.template_version_id, p.count,
7373

7474
-- name: ClaimPrebuild :one
7575
UPDATE workspaces w
76-
SET owner_id = @new_owner_id::uuid, updated_at = NOW() -- TODO: annoying; having two input params breaks dbgen
76+
SET owner_id = @new_user_id::uuid,
77+
name = @new_name::text,
78+
updated_at = NOW()
7779
WHERE w.id IN (SELECT p.id
78-
FROM workspace_prebuilds p
79-
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
80-
INNER JOIN provisioner_jobs pj ON b.job_id = pj.id
81-
INNER JOIN templates t ON p.template_id = t.id
82-
WHERE (b.transition = 'start'::workspace_transition
83-
AND pj.job_status IN ('succeeded'::provisioner_job_status))
84-
AND b.template_version_id = t.active_version_id
85-
ORDER BY random()
86-
LIMIT 1 FOR UPDATE OF p SKIP LOCKED)
87-
RETURNING w.id;
80+
FROM workspace_prebuilds p
81+
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
82+
INNER JOIN provisioner_jobs pj ON b.job_id = pj.id
83+
INNER JOIN templates t ON p.template_id = t.id
84+
WHERE (b.transition = 'start'::workspace_transition
85+
AND pj.job_status IN ('succeeded'::provisioner_job_status))
86+
AND b.template_version_id = t.active_version_id
87+
ORDER BY random()
88+
LIMIT 1 FOR UPDATE OF p SKIP LOCKED)
89+
RETURNING w.id, w.name;

coderd/prebuilds/claim.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package prebuilds
22

33
import (
44
"context"
5+
"database/sql"
6+
"errors"
57
"github.com/coder/coder/v2/coderd/database"
68
"github.com/google/uuid"
79
"golang.org/x/xerrors"
810
)
911

10-
func Claim(ctx context.Context, store database.Store, userID uuid.UUID) (*uuid.UUID, error) {
12+
func Claim(ctx context.Context, store database.Store, userID uuid.UUID, name string) (*uuid.UUID, error) {
1113
var prebuildID *uuid.UUID
1214
err := store.InTx(func(db database.Store) error {
1315
// TODO: do we need this?
@@ -17,14 +19,22 @@ func Claim(ctx context.Context, store database.Store, userID uuid.UUID) (*uuid.U
1719
// return xerrors.Errorf("acquire claim lock for user %q: %w", userID.String(), err)
1820
//}
1921

20-
id, err := db.ClaimPrebuild(ctx, userID)
22+
result, err := db.ClaimPrebuild(ctx, database.ClaimPrebuildParams{
23+
NewUserID: userID,
24+
NewName: name,
25+
})
2126
if err != nil {
22-
return xerrors.Errorf("claim prebuild for user %q: %w", userID.String(), err)
27+
switch {
28+
// No eligible prebuilds found
29+
case errors.Is(err, sql.ErrNoRows):
30+
// Exit, this will result in a nil prebuildID being returned, which is fine
31+
return nil
32+
default:
33+
return xerrors.Errorf("claim prebuild for user %q: %w", userID.String(), err)
34+
}
2335
}
2436

25-
if id != uuid.Nil {
26-
prebuildID = &id
27-
}
37+
prebuildID = &result.ID
2838

2939
return nil
3040
}, &database.TxOptions{

coderd/workspaces.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ func createWorkspace(
645645
claimCtx, cancel := context.WithTimeout(ownerCtx, time.Second*10) // TODO: don't use elevated authz context
646646
defer cancel()
647647

648-
claimedID, err := prebuilds.Claim(claimCtx, db, owner.ID)
648+
// TODO: pass down rich params for matching
649+
claimedID, err := prebuilds.Claim(claimCtx, db, owner.ID, req.Name)
649650
if err != nil {
650651
// TODO: enhance this by clarifying whether this *specific* prebuild failed or whether there are none to claim.
651652
api.Logger.Error(ctx, "failed to claim a prebuild", slog.Error(err))

docs/reference/api/schemas.md

+11-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/api/workspaces.md

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)