- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: delete pending canceled prebuilds#20499
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -57,6 +57,24 @@ type StoreReconciler struct { | ||
| var _ prebuilds.ReconciliationOrchestrator = &StoreReconciler{} | ||
| type DeprovisionMode int | ||
| const ( | ||
| DeprovisionModeNormal DeprovisionMode = iota | ||
| DeprovisionModeOrphan | ||
| ) | ||
| func (d DeprovisionMode) String() string { | ||
| switch d { | ||
| case DeprovisionModeOrphan: | ||
| return "orphan" | ||
| case DeprovisionModeNormal: | ||
| return "normal" | ||
| default: | ||
| return "unknown" | ||
| } | ||
| } | ||
| func NewStoreReconciler(store database.Store, | ||
| ps pubsub.Pubsub, | ||
| fileCache *files.Cache, | ||
| @@ -642,34 +660,7 @@ func (c *StoreReconciler) executeReconciliationAction(ctx context.Context, logge | ||
| return multiErr.ErrorOrNil() | ||
| case prebuilds.ActionTypeCancelPending: | ||
| return c.cancelAndOrphanDeletePendingPrebuilds(ctx, ps.Preset.TemplateID, ps.Preset.TemplateVersionID, ps.Preset.ID) | ||
| default: | ||
| return xerrors.Errorf("unknown action type: %v", action.ActionType) | ||
| @@ -717,33 +708,100 @@ func (c *StoreReconciler) createPrebuiltWorkspace(ctx context.Context, prebuiltW | ||
| c.logger.Info(ctx, "attempting to create prebuild", slog.F("name", name), | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| slog.F("workspace_id", prebuiltWorkspaceID.String()), slog.F("preset_id", presetID.String())) | ||
| return c.provision(ctx, db, prebuiltWorkspaceID, template, presetID, database.WorkspaceTransitionStart, workspace, DeprovisionModeNormal) | ||
| }, &database.TxOptions{ | ||
| Isolation: sql.LevelRepeatableRead, | ||
| ReadOnly: false, | ||
| }) | ||
| } | ||
| // provisionDelete provisions a delete transition for a prebuilt workspace. | ||
| // | ||
| // If mode is DeprovisionModeOrphan, the builder will not send Terraform state to the provisioner. | ||
| // This allows the workspace to be deleted even when no provisioners are available, and is safe | ||
| // when no Terraform resources were actually created (e.g., for pending prebuilds that were canceled | ||
| // before provisioning started). | ||
| // | ||
| // IMPORTANT: This function must be called within a database transaction. It does not create its own transaction. | ||
| // The caller is responsible for managing the transaction boundary via db.InTx(). | ||
| func (c *StoreReconciler) provisionDelete(ctx context.Context, db database.Store, workspaceID uuid.UUID, templateID uuid.UUID, presetID uuid.UUID, mode DeprovisionMode) error { | ||
| workspace, err := db.GetWorkspaceByID(ctx, workspaceID) | ||
| if err != nil { | ||
| return xerrors.Errorf("get workspace by ID: %w", err) | ||
| } | ||
Comment on lines +728 to +731 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I was going to mention that since you changed the | ||
| template, err := db.GetTemplateByID(ctx, templateID) | ||
| if err != nil { | ||
| return xerrors.Errorf("failed to get template: %w", err) | ||
| } | ||
| if workspace.OwnerID != database.PrebuildsSystemUserID { | ||
| return xerrors.Errorf("prebuilt workspace is not owned by prebuild user anymore, probably it was claimed") | ||
| } | ||
| c.logger.Info(ctx, "attempting to delete prebuild", slog.F("orphan", mode.String()), | ||
| slog.F("name", workspace.Name), slog.F("workspace_id", workspaceID.String()), slog.F("preset_id", presetID.String())) | ||
| return c.provision(ctx, db, workspaceID, template, presetID, | ||
| database.WorkspaceTransitionDelete, workspace, mode) | ||
| } | ||
| // cancelAndOrphanDeletePendingPrebuilds cancels pending prebuild jobs from inactive template versions | ||
| // and orphan-deletes their associated workspaces. | ||
| // | ||
| // The cancel operation uses a criteria-based update to ensure only jobs that are still pending at | ||
| // execution time are canceled, avoiding race conditions where jobs may have transitioned to running. | ||
| // | ||
| // Since these jobs were never processed by a provisioner, no Terraform resources were created, | ||
| // making it safe to orphan-delete the workspaces (skipping Terraform destroy). | ||
| func (c *StoreReconciler) cancelAndOrphanDeletePendingPrebuilds(ctx context.Context, templateID uuid.UUID, templateVersionID uuid.UUID, presetID uuid.UUID) error { | ||
| return c.store.InTx(func(db database.Store) error { | ||
| canceledJobs, err := db.UpdatePrebuildProvisionerJobWithCancel( | ||
| ctx, | ||
| database.UpdatePrebuildProvisionerJobWithCancelParams{ | ||
| Now: c.clock.Now(), | ||
| PresetID: uuid.NullUUID{ | ||
| UUID: presetID, | ||
| Valid: true, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| c.logger.Error(ctx, "failed to cancel pending prebuild jobs", | ||
| slog.F("template_id", templateID.String()), | ||
| slog.F("template_version_id", templateVersionID.String()), | ||
| slog.F("preset_id", presetID.String()), | ||
| slog.Error(err)) | ||
| return err | ||
| } | ||
| if len(canceledJobs) > 0 { | ||
| c.logger.Info(ctx, "canceled pending prebuild jobs for inactive version", | ||
| slog.F("template_id", templateID.String()), | ||
| slog.F("template_version_id", templateVersionID.String()), | ||
| slog.F("preset_id", presetID.String()), | ||
| slog.F("count", len(canceledJobs))) | ||
| } | ||
| var multiErr multierror.Error | ||
| for _, job := range canceledJobs { | ||
| err = c.provisionDelete(ctx, db, job.WorkspaceID, job.TemplateID, presetID, DeprovisionModeOrphan) | ||
| if err != nil { | ||
| c.logger.Error(ctx, "failed to orphan delete canceled prebuild", | ||
| slog.F("workspace_id", job.WorkspaceID.String()), slog.Error(err)) | ||
| multiErr.Errors = append(multiErr.Errors, err) | ||
| } | ||
| } | ||
| return multiErr.ErrorOrNil() | ||
| }, &database.TxOptions{ | ||
| Isolation: sql.LevelRepeatableRead, | ||
| ReadOnly: false, | ||
| }) | ||
| } | ||
| func (c *StoreReconciler) deletePrebuiltWorkspace(ctx context.Context, prebuiltWorkspaceID uuid.UUID, templateID uuid.UUID, presetID uuid.UUID) error { | ||
| return c.store.InTx(func(db database.Store) error { | ||
| return c.provisionDelete(ctx, db, prebuiltWorkspaceID, templateID, presetID, DeprovisionModeNormal) | ||
| }, &database.TxOptions{ | ||
| Isolation: sql.LevelRepeatableRead, | ||
| ReadOnly: false, | ||
| @@ -758,6 +816,7 @@ func (c *StoreReconciler) provision( | ||
| presetID uuid.UUID, | ||
| transition database.WorkspaceTransition, | ||
| workspace database.Workspace, | ||
| mode DeprovisionMode, | ||
| ) error { | ||
| tvp, err := db.GetPresetParametersByTemplateVersionID(ctx, template.ActiveVersionID) | ||
| if err != nil { | ||
| @@ -795,6 +854,11 @@ func (c *StoreReconciler) provision( | ||
| builder = builder.RichParameterValues(params) | ||
| } | ||
| // Use orphan mode for deletes when no Terraform resources exist | ||
| if transition == database.WorkspaceTransitionDelete && mode == DeprovisionModeOrphan { | ||
| builder = builder.Orphan() | ||
| } | ||
| _, provisionerJob, _, err := builder.Build( | ||
| ctx, | ||
| db, | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.