Skip to content

fix: exclude prebuilt workspaces from template-level lifecycle updates #19265

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 12 additions & 5 deletions coderd/database/queries.sql.go

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

17 changes: 12 additions & 5 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,15 @@ WHERE

-- name: UpdateWorkspacesTTLByTemplateID :exec
UPDATE
workspaces
workspaces
SET
ttl = $2
ttl = $2
WHERE
template_id = $1;
template_id = $1
-- Prebuilt workspaces (identified by having the prebuilds system user as owner_id)
-- should not have their TTL updated, as they are handled by the prebuilds
-- reconciliation loop.
AND workspaces.owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID;

-- name: UpdateWorkspaceLastUsedAt :exec
UPDATE
Expand Down Expand Up @@ -805,8 +809,11 @@ SET
dormant_at = CASE WHEN @dormant_at::timestamptz > '0001-01-01 00:00:00+00'::timestamptz THEN @dormant_at::timestamptz ELSE dormant_at END
WHERE
template_id = @template_id
AND
dormant_at IS NOT NULL
AND dormant_at IS NOT NULL
-- Prebuilt workspaces (identified by having the prebuilds system user as owner_id)
-- should not have their dormant or deleting at set, as these are handled by the
-- prebuilds reconciliation loop.
AND workspaces.owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID
RETURNING *;

-- name: UpdateTemplateWorkspacesLastUsedAt :exec
Expand Down
11 changes: 10 additions & 1 deletion enterprise/coderd/schedule/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
nextStartAts := []time.Time{}

for _, workspace := range workspaces {
// Skip prebuilt workspaces
if workspace.IsPrebuild() {
continue
}
nextStartAt := time.Time{}
if workspace.AutostartSchedule.Valid {
next, err := agpl.NextAllowedAutostart(s.now(), workspace.AutostartSchedule.String, templateSchedule)
Expand All @@ -255,7 +259,7 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
nextStartAts = append(nextStartAts, nextStartAt)
}

//nolint:gocritic // We need to be able to update information about all workspaces.
//nolint:gocritic // We need to be able to update information about regular user workspaces.
if err := db.BatchUpdateWorkspaceNextStartAt(dbauthz.AsSystemRestricted(ctx), database.BatchUpdateWorkspaceNextStartAtParams{
IDs: workspaceIDs,
NextStartAts: nextStartAts,
Expand Down Expand Up @@ -335,6 +339,11 @@ func (s *EnterpriseTemplateScheduleStore) updateWorkspaceBuild(ctx context.Conte
return xerrors.Errorf("get workspace %q: %w", build.WorkspaceID, err)
}

// Skip lifecycle updates for prebuilt workspaces
if workspace.IsPrebuild() {
return nil
}

job, err := db.GetProvisionerJobByID(ctx, build.JobID)
if err != nil {
return xerrors.Errorf("get provisioner job %q: %w", build.JobID, err)
Expand Down
Loading