Skip to content

Commit ec09fd8

Browse files
committed
fix: prebuilds lifecycle parameters on creation and claim
1 parent b200fc8 commit ec09fd8

File tree

14 files changed

+886
-503
lines changed

14 files changed

+886
-503
lines changed

cli/schedule.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ func (r *RootCmd) scheduleStart() *serpent.Command {
157157
return err
158158
}
159159

160+
// Autostart configuration is not supported for prebuilt workspaces.
161+
// Prebuild lifecycle is managed by the reconciliation loop, with scheduling behavior
162+
// defined per preset at the template level, not per workspace.
163+
if workspace.IsPrebuild {
164+
return xerrors.Errorf("autostart configuration is not supported for prebuilt workspaces")
165+
}
166+
160167
var schedStr *string
161168
if inv.Args[1] != "manual" {
162169
sched, err := parseCLISchedule(inv.Args[1:]...)
@@ -205,6 +212,13 @@ func (r *RootCmd) scheduleStop() *serpent.Command {
205212
return err
206213
}
207214

215+
// Autostop configuration is not supported for prebuilt workspaces.
216+
// Prebuild lifecycle is managed by the reconciliation loop, with scheduling behavior
217+
// defined per preset at the template level, not per workspace.
218+
if workspace.IsPrebuild {
219+
return xerrors.Errorf("autostop configuration is not supported for prebuilt workspaces")
220+
}
221+
208222
var durMillis *int64
209223
if inv.Args[1] != "manual" {
210224
dur, err := parseDuration(inv.Args[1])

coderd/database/dbauthz/dbauthz.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5141,6 +5141,11 @@ func (q *querier) UpdateWorkspaceBuildDeadlineByID(ctx context.Context, arg data
51415141
return err
51425142
}
51435143

5144+
// Skip deadline updates for prebuilt workspaces
5145+
if workspace.IsPrebuild() {
5146+
return nil
5147+
}
5148+
51445149
err = q.authorizeContext(ctx, policy.ActionUpdate, workspace.RBACObject())
51455150
if err != nil {
51465151
return err

coderd/database/querier.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 38 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/activitybump.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
--
66
-- Max deadline is respected, and the deadline will never be bumped past it.
77
-- The deadline will never decrease.
8+
-- NOTE: This query should only be called for regular user workspaces.
9+
-- Prebuilds are managed by the reconciliation loop and not subject to activity bumping.
810
-- name: ActivityBumpWorkspace :exec
911
WITH latest AS (
1012
SELECT

coderd/database/queries/prebuilds.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
UPDATE workspaces w
33
SET owner_id = @new_user_id::uuid,
44
name = @new_name::text,
5-
updated_at = NOW()
5+
updated_at = NOW(),
6+
-- Update last_used_at during claim to ensure the claimed workspace is treated as recently used.
7+
-- This avoids unintended dormancy caused by prebuilds having stale usage timestamps.
8+
last_used_at = NOW(),
9+
-- Clear dormant and deletion timestamps as a safeguard to ensure a clean lifecycle state after claim.
10+
-- These fields should not be set on prebuilds, but we defensively reset them here to prevent
11+
-- accidental dormancy or deletion by the lifecycle executor.
12+
dormant_at = NULL,
13+
deleting_at = NULL
614
WHERE w.id IN (
715
SELECT p.id
816
FROM workspace_prebuilds p

coderd/database/queries/workspacebuilds.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ WHERE
135135
id = $1;
136136

137137
-- name: UpdateWorkspaceBuildDeadlineByID :exec
138+
-- NOTE: This query should only be called for regular user workspaces.
139+
-- Prebuilds are managed by the reconciliation loop, not the lifecycle
140+
-- executor which handles deadline and max_deadline.
138141
UPDATE
139142
workspace_builds
140143
SET

coderd/database/queries/workspaces.sql

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ WHERE
512512
RETURNING *;
513513

514514
-- name: UpdateWorkspaceAutostart :exec
515+
-- NOTE: This query should only be called for regular user workspaces.
516+
-- Prebuilds are managed by the reconciliation loop, not the lifecycle
517+
-- executor which handles autostart_schedule and next_start_at.
515518
UPDATE
516519
workspaces
517520
SET
@@ -521,6 +524,9 @@ WHERE
521524
id = $1;
522525

523526
-- name: UpdateWorkspaceNextStartAt :exec
527+
-- NOTE: This query should only be called for regular user workspaces.
528+
-- Prebuilds are managed by the reconciliation loop, not the lifecycle
529+
-- executor which handles next_start_at.
524530
UPDATE
525531
workspaces
526532
SET
@@ -545,6 +551,9 @@ WHERE
545551
workspaces.id = batch.id;
546552

547553
-- name: UpdateWorkspaceTTL :exec
554+
-- NOTE: This query should only be called for regular user workspaces.
555+
-- Prebuilds are managed by the reconciliation loop, not the lifecycle
556+
-- executor which handles regular workspace's TTL.
548557
UPDATE
549558
workspaces
550559
SET
@@ -554,11 +563,15 @@ WHERE
554563

555564
-- name: UpdateWorkspacesTTLByTemplateID :exec
556565
UPDATE
557-
workspaces
566+
workspaces
558567
SET
559-
ttl = $2
568+
ttl = $2
560569
WHERE
561-
template_id = $1;
570+
template_id = $1
571+
-- Prebuilt workspaces (identified by having the prebuilds system user as owner_id)
572+
-- should not have their TTL updated, as they are handled by the prebuilds
573+
-- reconciliation loop.
574+
AND workspaces.owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID;
562575

563576
-- name: UpdateWorkspaceLastUsedAt :exec
564577
UPDATE
@@ -768,6 +781,9 @@ WHERE
768781
AND workspaces.owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID;
769782

770783
-- name: UpdateWorkspaceDormantDeletingAt :one
784+
-- NOTE: This query should only be called for regular user workspaces.
785+
-- Prebuilds are managed by the reconciliation loop, not the lifecycle
786+
-- executor which handles dormant_at and deleting_at.
771787
UPDATE
772788
workspaces
773789
SET
@@ -805,8 +821,11 @@ SET
805821
dormant_at = CASE WHEN @dormant_at::timestamptz > '0001-01-01 00:00:00+00'::timestamptz THEN @dormant_at::timestamptz ELSE dormant_at END
806822
WHERE
807823
template_id = @template_id
808-
AND
809-
dormant_at IS NOT NULL
824+
AND dormant_at IS NOT NULL
825+
-- Prebuilt workspaces (identified by having the prebuilds system user as owner_id)
826+
-- should not have their dormant or deleting at set, as these are handled by the
827+
-- prebuilds reconciliation loop.
828+
AND workspaces.owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID
810829
RETURNING *;
811830

812831
-- name: UpdateTemplateWorkspacesLastUsedAt :exec

0 commit comments

Comments
 (0)