Skip to content

Commit 6113f97

Browse files
committed
feat: add deleting_at column to workspaces
1 parent 9aae983 commit 6113f97

15 files changed

+179
-44
lines changed

coderd/autobuild/lifecycle_executor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,13 @@ func (e *Executor) runOnce(t time.Time) Stats {
178178
// Lock the workspace if it has breached the template's
179179
// threshold for inactivity.
180180
if reason == database.BuildReasonAutolock {
181-
err = tx.UpdateWorkspaceLockedAt(e.ctx, database.UpdateWorkspaceLockedAtParams{
181+
err = tx.UpdateWorkspaceLockedDeletingAt(e.ctx, database.UpdateWorkspaceLockedDeletingAtParams{
182182
ID: ws.ID,
183183
LockedAt: sql.NullTime{
184184
Time: database.Now(),
185185
Valid: true,
186186
},
187+
LockedTtlMs: templateSchedule.LockedTTL.Milliseconds(),
187188
})
188189
if err != nil {
189190
log.Error(e.ctx, "unable to lock workspace",
@@ -347,11 +348,11 @@ func isEligibleForLockedStop(ws database.Workspace, templateSchedule schedule.Te
347348

348349
func isEligibleForDelete(ws database.Workspace, templateSchedule schedule.TemplateScheduleOptions, currentTick time.Time) bool {
349350
// Only attempt to delete locked workspaces.
350-
return ws.LockedAt.Valid &&
351+
return ws.LockedAt.Valid && ws.DeletingAt.Valid &&
351352
// Locked workspaces should only be deleted if a locked_ttl is specified.
352353
templateSchedule.LockedTTL > 0 &&
353354
// The workspace must breach the locked_ttl.
354-
currentTick.Sub(ws.LockedAt.Time) > templateSchedule.LockedTTL
355+
currentTick.After(ws.DeletingAt.Time)
355356
}
356357

357358
// isEligibleForFailedStop returns true if the workspace is eligible to be stopped

coderd/database/dbauthz/dbauthz.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,11 +2509,11 @@ func (q *querier) UpdateWorkspaceLastUsedAt(ctx context.Context, arg database.Up
25092509
return update(q.log, q.auth, fetch, q.db.UpdateWorkspaceLastUsedAt)(ctx, arg)
25102510
}
25112511

2512-
func (q *querier) UpdateWorkspaceLockedAt(ctx context.Context, arg database.UpdateWorkspaceLockedAtParams) error {
2513-
fetch := func(ctx context.Context, arg database.UpdateWorkspaceLockedAtParams) (database.Workspace, error) {
2512+
func (q *querier) UpdateWorkspaceLockedDeletingAt(ctx context.Context, arg database.UpdateWorkspaceLockedDeletingAtParams) error {
2513+
fetch := func(ctx context.Context, arg database.UpdateWorkspaceLockedDeletingAtParams) (database.Workspace, error) {
25142514
return q.db.GetWorkspaceByID(ctx, arg.ID)
25152515
}
2516-
return update(q.log, q.auth, fetch, q.db.UpdateWorkspaceLockedAt)(ctx, arg)
2516+
return update(q.log, q.auth, fetch, q.db.UpdateWorkspaceLockedDeletingAt)(ctx, arg)
25172517
}
25182518

25192519
func (q *querier) UpdateWorkspaceProxy(ctx context.Context, arg database.UpdateWorkspaceProxyParams) (database.WorkspaceProxy, error) {
@@ -2544,6 +2544,16 @@ func (q *querier) UpdateWorkspaceTTLToBeWithinTemplateMax(ctx context.Context, a
25442544
return fetchAndExec(q.log, q.auth, rbac.ActionUpdate, fetch, q.db.UpdateWorkspaceTTLToBeWithinTemplateMax)(ctx, arg)
25452545
}
25462546

2547+
func (q *querier) UpdateWorkspacesDeletingAtByTemplateID(ctx context.Context, arg database.UpdateWorkspacesDeletingAtByTemplateIDParams) error {
2548+
// TODO: This is kinda messed up. We want to update this whenever we update the locked_ttl on a template. But a template
2549+
// admin may not have permissions to edit workspaces so this won't work.
2550+
fetch := func(ctx context.Context, arg database.UpdateWorkspacesDeletingAtByTemplateIDParams) (database.Template, error) {
2551+
return q.db.GetTemplateByID(ctx, arg.TemplateID)
2552+
}
2553+
2554+
return fetchAndExec(q.log, q.auth, rbac.ActionUpdate, fetch, q.db.UpdateWorkspacesDeletingAtByTemplateID)(ctx, arg)
2555+
}
2556+
25472557
func (q *querier) UpsertAppSecurityKey(ctx context.Context, data string) error {
25482558
// No authz checks as this is done during startup
25492559
return q.db.UpsertAppSecurityKey(ctx, data)

coderd/database/dbmetrics/dbmetrics.go

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

coderd/database/dbmock/dbmock.go

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

coderd/database/dump.sql

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE workspaces DROP COLUMN deleting_at;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE workspaces ADD COLUMN deleting_at timestamptz NULL;

coderd/database/models.go

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

coderd/database/querier.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)