Skip to content

Commit 0ba53bd

Browse files
committed
update deleting logic
1 parent ae3473d commit 0ba53bd

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

coderd/workspaces.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,13 +1207,18 @@ func convertWorkspaceTTLMillis(i sql.NullInt64) *int64 {
12071207
// Calculate the time of the upcoming workspace deletion, if applicable; otherwise, return nil.
12081208
// Workspaces may have impending deletions if InactivityTTL feature is turned on and the workspace is inactive.
12091209
func calculateDeletingAt(workspace database.Workspace, template database.Template) *time.Time {
1210-
var (
1211-
year, month, day = time.Now().Date()
1212-
beginningOfToday = time.Date(year, month, day, 0, 0, 0, 0, time.Now().Location())
1213-
)
1210+
// Workspace is recently inactive but hasn't been inactive for longer than
1211+
// the specified template.InactivityTTL threshold
1212+
1213+
fmt.Println("last used at before now (aka inactive)", workspace.LastUsedAt.Before(time.Now()))
1214+
fmt.Println("last used at is more recent than now minus the TTL ", workspace.LastUsedAt.After(time.Now().Add(-time.Duration(template.InactivityTTL)*time.Nanosecond)))
1215+
1216+
workspaceRecentlyInactive := workspace.LastUsedAt.Before(time.Now()) &&
1217+
workspace.LastUsedAt.After(time.Now().Add(-time.Duration(template.InactivityTTL)*time.Nanosecond))
1218+
12141219
// If InactivityTTL is turned off (set to 0), if the workspace has already been deleted,
1215-
// or if the workspace was used sometime within the last day, there is no impending deletion
1216-
if template.InactivityTTL == 0 || workspace.Deleted || workspace.LastUsedAt.After(beginningOfToday) {
1220+
// or if the workspace is only recently inactive, there is no impending deletion
1221+
if template.InactivityTTL == 0 || workspace.Deleted || workspaceRecentlyInactive {
12171222
return nil
12181223
}
12191224

0 commit comments

Comments
 (0)