Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: set prebuilds lifecycle parameters on creation and claim#19252

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
15 commits
Select commitHold shift + click to select a range
6415069
fix: prebuilds lifecycle parameters on creation and claim
ssncferreiraAug 8, 2025
e322671
refactor: remove CLI schedule changes
ssncferreiraAug 8, 2025
85d24ab
refactor: remove activity bump changes
ssncferreiraAug 8, 2025
04a481d
refactor: remove lifecycle API endpoint changes
ssncferreiraAug 8, 2025
2761b7e
refactor: remove template lifecycle update changes
ssncferreiraAug 8, 2025
4c847c9
fix: add database constraints to prevent invalid lifecycle fields on …
ssncferreiraAug 8, 2025
2bc1070
Merge remote-tracking branch 'origin/main' into ssncferreira/fix-preb…
ssncferreiraAug 8, 2025
c42ef24
Merge remote-tracking branch 'origin/main' into ssncferreira/fix-preb…
ssncferreiraAug 11, 2025
8653a33
refactor: add lifecycle fields to prebuild claim database query
ssncferreiraAug 11, 2025
8486f3f
Merge remote-tracking branch 'origin/main' into ssncferreira/fix-preb…
ssncferreiraAug 11, 2025
b2c130f
fix: lifecycle executor prebuilds test
ssncferreiraAug 11, 2025
e54ba2a
refactor: use testutil.RequireReceive for receiving workspace lifecyc…
ssncferreiraAug 11, 2025
5781268
Merge remote-tracking branch 'origin/main' into ssncferreira/fix-preb…
ssncferreiraAug 12, 2025
837b02a
refactor: use api clock mock when creating workspace
ssncferreiraAug 12, 2025
23e753b
Merge remote-tracking branch 'origin/main' into ssncferreira/fix-preb…
ssncferreiraAug 12, 2025
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
35 changes: 26 additions & 9 deletionscoderd/autobuild/lifecycle_executor_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1251,15 +1251,23 @@ func TestExecutorPrebuilds(t *testing.T) {
}()

// Then: the prebuilt workspace should remain in a start transition
prebuildStats :=<-statsCh
prebuildStats :=testutil.RequireReceive(ctx, t,statsCh)
require.Len(t, prebuildStats.Errors, 0)
require.Len(t, prebuildStats.Transitions, 0)
require.Equal(t, codersdk.WorkspaceTransitionStart, prebuild.LatestBuild.Transition)
prebuild = coderdtest.MustWorkspace(t, client, prebuild.ID)
require.Equal(t, codersdk.BuildReasonInitiator, prebuild.LatestBuild.Reason)

// Given: a user claims the prebuilt workspace
dbWorkspace := dbgen.ClaimPrebuild(t, db, user.ID, "claimedWorkspace-autostop", preset.ID)
dbWorkspace := dbgen.ClaimPrebuild(
t, db,
clock.Now(),
user.ID,
"claimedWorkspace-autostop",
preset.ID,
sql.NullString{},
sql.NullTime{},
sql.NullInt64{})
workspace := coderdtest.MustWorkspace(t, client, dbWorkspace.ID)

// When: the autobuild executor ticks *after* the deadline:
Expand All@@ -1269,7 +1277,7 @@ func TestExecutorPrebuilds(t *testing.T) {
}()

// Then: the workspace should be stopped
workspaceStats :=<-statsCh
workspaceStats :=testutil.RequireReceive(ctx, t,statsCh)
require.Len(t, workspaceStats.Errors, 0)
require.Len(t, workspaceStats.Transitions, 1)
require.Contains(t, workspaceStats.Transitions, workspace.ID)
Expand DownExpand Up@@ -1336,7 +1344,7 @@ func TestExecutorPrebuilds(t *testing.T) {
}()

// Then: the prebuilt workspace should remain in a stop transition
prebuildStats :=<-statsCh
prebuildStats :=testutil.RequireReceive(ctx, t,statsCh)
require.Len(t, prebuildStats.Errors, 0)
require.Len(t, prebuildStats.Transitions, 0)
require.Equal(t, codersdk.WorkspaceTransitionStop, prebuild.LatestBuild.Transition)
Expand All@@ -1353,7 +1361,15 @@ func TestExecutorPrebuilds(t *testing.T) {
database.WorkspaceTransitionStart)

// Given: a user claims the prebuilt workspace
dbWorkspace := dbgen.ClaimPrebuild(t, db, user.ID, "claimedWorkspace-autostart", preset.ID)
dbWorkspace := dbgen.ClaimPrebuild(
t, db,
clock.Now(),
user.ID,
"claimedWorkspace-autostart",
preset.ID,
autostartSched,
sql.NullTime{},
sql.NullInt64{})
workspace := coderdtest.MustWorkspace(t, client, dbWorkspace.ID)

// Given: the prebuilt workspace goes to a stop status
Expand All@@ -1374,7 +1390,7 @@ func TestExecutorPrebuilds(t *testing.T) {
}()

// Then: the workspace should eventually be started
workspaceStats :=<-statsCh
workspaceStats :=testutil.RequireReceive(ctx, t,statsCh)
require.Len(t, workspaceStats.Errors, 0)
require.Len(t, workspaceStats.Transitions, 1)
require.Contains(t, workspaceStats.Transitions, workspace.ID)
Expand DownExpand Up@@ -1486,8 +1502,8 @@ func setupTestDBWorkspaceBuild(
Architecture: "i386",
OperatingSystem: "linux",
LifecycleState: database.WorkspaceAgentLifecycleStateReady,
StartedAt: sql.NullTime{Time:time.Now().Add(time.Hour), Valid: true},
ReadyAt: sql.NullTime{Time:time.Now().Add(-1 * time.Hour), Valid: true},
StartedAt: sql.NullTime{Time:clock.Now().Add(time.Hour), Valid: true},
ReadyAt: sql.NullTime{Time:clock.Now().Add(-1 * time.Hour), Valid: true},
APIKeyScope: database.AgentKeyScopeEnumAll,
})

Expand DownExpand Up@@ -1524,8 +1540,9 @@ func setupTestDBPrebuiltWorkspace(
OrganizationID: orgID,
OwnerID: database.PrebuildsSystemUserID,
Deleted: false,
CreatedAt:time.Now().Add(-time.Hour * 2),
CreatedAt:clock.Now().Add(-time.Hour * 2),
AutostartSchedule: options.AutostartSchedule,
LastUsedAt: clock.Now(),
})
setupTestDBWorkspaceBuild(ctx, t, clock, db, ps, orgID, workspace.ID, templateVersionID, presetID, buildTransition)

Expand Down
22 changes: 18 additions & 4 deletionscoderd/database/dbgen/dbgen.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1436,11 +1436,25 @@ func UserSecret(t testing.TB, db database.Store, seed database.UserSecret) datab
return userSecret
}

func ClaimPrebuild(t testing.TB, db database.Store, newUserID uuid.UUID, newName string, presetID uuid.UUID) database.ClaimPrebuiltWorkspaceRow {
func ClaimPrebuild(
t testing.TB,
db database.Store,
now time.Time,
newUserID uuid.UUID,
newName string,
presetID uuid.UUID,
autostartSchedule sql.NullString,
nextStartAt sql.NullTime,
ttl sql.NullInt64,
) database.ClaimPrebuiltWorkspaceRow {
claimedWorkspace, err := db.ClaimPrebuiltWorkspace(genCtx, database.ClaimPrebuiltWorkspaceParams{
NewUserID: newUserID,
NewName: newName,
PresetID: presetID,
NewUserID: newUserID,
NewName: newName,
Now: now,
PresetID: presetID,
AutostartSchedule: autostartSchedule,
NextStartAt: nextStartAt,
WorkspaceTtl: ttl,
})
require.NoError(t, err, "claim prebuilt workspace")

Expand Down
69 changes: 59 additions & 10 deletionscoderd/database/queries.sql.go
View file
Open in desktop

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

15 changes: 14 additions & 1 deletioncoderd/database/queries/prebuilds.sql
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down
10 changes: 9 additions & 1 deletioncoderd/database/queries/workspacebuilds.sql
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down
28 changes: 22 additions & 6 deletionscoderd/database/queries/workspaces.sql
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -518,15 +518,23 @@ 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;
Comment on lines +521 to +525
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This seems more related to#19264?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

As discussed internally will keep this constraint in this PR as per the PR description:Updated lifecycle-related SQL update queries to explicitly exclude prebuilt workspaces.


-- name: UpdateWorkspaceNextStartAt :exec
UPDATE
workspaces
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
Expand All@@ -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
Expand DownExpand Up@@ -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.*;

Expand Down
13 changes: 12 additions & 1 deletioncoderd/prebuilds/api.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@ package prebuilds

import (
"context"
"database/sql"
"time"

"github.com/google/uuid"
"golang.org/x/xerrors"
Expand DownExpand Up@@ -54,6 +56,15 @@ type StateSnapshotter interface {
}

type Claimer interface {
Claim(ctx context.Context, userID uuid.UUID, name string, presetID uuid.UUID) (*uuid.UUID, error)
Claim(
ctx context.Context,
now time.Time,
userID uuid.UUID,
name string,
presetID uuid.UUID,
autostartSchedule sql.NullString,
nextStartAt sql.NullTime,
ttl sql.NullInt64,
) (*uuid.UUID, error)
Initiator() uuid.UUID
}
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp