mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: set prebuilds lifecycle parameters on creation and claim (#19252)
## Description This PR ensures that prebuilt workspaces are properly excluded from the lifecycle executor and treated as a separate class of workspaces, fully managed by the prebuild reconciliation loop. It introduces two lifecycle guarantees: * When a prebuilt workspace is created (i.e., when the workspace build completes), all lifecycle-related fields are unset, ensuring the workspace does not participate in TTL, autostop, autostart, dormancy, or auto-deletion logic. * When a prebuilt workspace is claimed, it transitions into a regular user workspace. At this point, all lifecycle fields are correctly populated according to template-level configurations, allowing the workspace to be managed by the lifecycle executor as expected. ## Changes * Prebuilt workspaces now have all lifecycle-relevant fields unset during creation * When a prebuild is claimed: * Lifecycle fields are set based on template and workspace level configurations. This ensures a clean transition into the standard workspace lifecycle flow. * Updated lifecycle-related SQL update queries to explicitly exclude prebuilt workspaces. ## Relates Related issue: https://github.com/coder/coder/issues/18898 To reduce the scope of this PR and make the review process more manageable, the original implementation has been split into the following focused PRs: * https://github.com/coder/coder/pull/19259 * https://github.com/coder/coder/pull/19263 * https://github.com/coder/coder/pull/19264 * https://github.com/coder/coder/pull/19265 These PRs should be considered in conjunction with this one to understand the complete set of lifecycle separation changes for prebuilt workspaces.
This commit is contained in:
@@ -2,7 +2,20 @@
|
||||
UPDATE workspaces w
|
||||
SET owner_id = @new_user_id::uuid,
|
||||
name = @new_name::text,
|
||||
updated_at = NOW()
|
||||
updated_at = @now::timestamptz,
|
||||
-- Update autostart_schedule, next_start_at and ttl according to template and workspace-level
|
||||
-- configurations, allowing the workspace to be managed by the lifecycle executor as expected.
|
||||
autostart_schedule = @autostart_schedule,
|
||||
next_start_at = @next_start_at,
|
||||
ttl = @workspace_ttl,
|
||||
-- Update last_used_at during claim to ensure the claimed workspace is treated as recently used.
|
||||
-- This avoids unintended dormancy caused by prebuilds having stale usage timestamps.
|
||||
last_used_at = @now::timestamptz,
|
||||
-- Clear dormant and deletion timestamps as a safeguard to ensure a clean lifecycle state after claim.
|
||||
-- These fields should not be set on prebuilds, but we defensively reset them here to prevent
|
||||
-- accidental dormancy or deletion by the lifecycle executor.
|
||||
dormant_at = NULL,
|
||||
deleting_at = NULL
|
||||
WHERE w.id IN (
|
||||
SELECT p.id
|
||||
FROM workspace_prebuilds p
|
||||
|
||||
@@ -127,7 +127,15 @@ SET
|
||||
deadline = @deadline::timestamptz,
|
||||
max_deadline = @max_deadline::timestamptz,
|
||||
updated_at = @updated_at::timestamptz
|
||||
WHERE id = @id::uuid;
|
||||
FROM
|
||||
workspaces
|
||||
WHERE
|
||||
workspace_builds.id = @id::uuid
|
||||
AND workspace_builds.workspace_id = workspaces.id
|
||||
-- Prebuilt workspaces (identified by having the prebuilds system user as owner_id)
|
||||
-- are managed by the reconciliation loop, not the lifecycle executor which handles
|
||||
-- deadline and max_deadline
|
||||
AND workspaces.owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID;
|
||||
|
||||
-- name: UpdateWorkspaceBuildProvisionerStateByID :exec
|
||||
UPDATE
|
||||
|
||||
@@ -518,7 +518,11 @@ SET
|
||||
autostart_schedule = $2,
|
||||
next_start_at = $3
|
||||
WHERE
|
||||
id = $1;
|
||||
id = $1
|
||||
-- Prebuilt workspaces (identified by having the prebuilds system user as owner_id)
|
||||
-- are managed by the reconciliation loop, not the lifecycle executor which handles
|
||||
-- autostart_schedule and next_start_at
|
||||
AND owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID;
|
||||
|
||||
-- name: UpdateWorkspaceNextStartAt :exec
|
||||
UPDATE
|
||||
@@ -526,7 +530,11 @@ UPDATE
|
||||
SET
|
||||
next_start_at = $2
|
||||
WHERE
|
||||
id = $1;
|
||||
id = $1
|
||||
-- Prebuilt workspaces (identified by having the prebuilds system user as owner_id)
|
||||
-- are managed by the reconciliation loop, not the lifecycle executor which handles
|
||||
-- next_start_at
|
||||
AND owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID;
|
||||
|
||||
-- name: BatchUpdateWorkspaceNextStartAt :exec
|
||||
UPDATE
|
||||
@@ -550,15 +558,19 @@ UPDATE
|
||||
SET
|
||||
ttl = $2
|
||||
WHERE
|
||||
id = $1;
|
||||
id = $1
|
||||
-- Prebuilt workspaces (identified by having the prebuilds system user as owner_id)
|
||||
-- are managed by the reconciliation loop, not the lifecycle executor which handles
|
||||
-- ttl
|
||||
AND owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID;
|
||||
|
||||
-- name: UpdateWorkspacesTTLByTemplateID :exec
|
||||
UPDATE
|
||||
workspaces
|
||||
workspaces
|
||||
SET
|
||||
ttl = $2
|
||||
ttl = $2
|
||||
WHERE
|
||||
template_id = $1;
|
||||
template_id = $1;
|
||||
|
||||
-- name: UpdateWorkspaceLastUsedAt :exec
|
||||
UPDATE
|
||||
@@ -791,6 +803,10 @@ FROM
|
||||
WHERE
|
||||
workspaces.id = $1
|
||||
AND templates.id = workspaces.template_id
|
||||
-- Prebuilt workspaces (identified by having the prebuilds system user as owner_id)
|
||||
-- are managed by the reconciliation loop, not the lifecycle executor which handles
|
||||
-- dormant_at and deleting_at
|
||||
AND owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID
|
||||
RETURNING
|
||||
workspaces.*;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user