Skip to content

Commit 4baf40a

Browse files
committed
added impending_deletion workspace field
1 parent fc1bc37 commit 4baf40a

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

coderd/workspaces.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,10 @@ func convertWorkspace(
11691169
autostartSchedule = &workspace.AutostartSchedule.String
11701170
}
11711171

1172-
ttlMillis := convertWorkspaceTTLMillis(workspace.Ttl)
1172+
var (
1173+
ttlMillis = convertWorkspaceTTLMillis(workspace.Ttl)
1174+
impendingDeletion = calculateImpendingDeletion(workspace, template)
1175+
)
11731176
return codersdk.Workspace{
11741177
ID: workspace.ID,
11751178
CreatedAt: workspace.CreatedAt,
@@ -1188,6 +1191,7 @@ func convertWorkspace(
11881191
AutostartSchedule: autostartSchedule,
11891192
TTLMillis: ttlMillis,
11901193
LastUsedAt: workspace.LastUsedAt,
1194+
ImpendingDeletion: impendingDeletion,
11911195
}
11921196
}
11931197

@@ -1200,6 +1204,22 @@ func convertWorkspaceTTLMillis(i sql.NullInt64) *int64 {
12001204
return &millis
12011205
}
12021206

1207+
// Calculate the time of the upcoming working deletion, if applicable; otherwise, return an empty time.Time struct.
1208+
// Workspaces may have impending deletions if InactivityTTL feature is turned on and the workspace is inactive.
1209+
func calculateImpendingDeletion(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+
)
1214+
// 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) {
1217+
return time.Time{}
1218+
}
1219+
1220+
return workspace.LastUsedAt.Add(time.Duration(template.InactivityTTL) * time.Nanosecond)
1221+
}
1222+
12031223
func validWorkspaceTTLMillis(millis *int64, templateDefault, templateMax time.Duration) (sql.NullInt64, error) {
12041224
if templateDefault == 0 && templateMax != 0 || (templateMax > 0 && templateDefault > templateMax) {
12051225
templateDefault = templateMax

codersdk/workspaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Workspace struct {
3434
AutostartSchedule *string `json:"autostart_schedule,omitempty"`
3535
TTLMillis *int64 `json:"ttl_ms,omitempty"`
3636
LastUsedAt time.Time `json:"last_used_at" format:"date-time"`
37+
ImpendingDeletion time.Time `json:"impending_deletion" format:"date-time"`
3738
}
3839

3940
type WorkspacesRequest struct {

site/src/api/typesGenerated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ export interface Workspace {
11041104
readonly autostart_schedule?: string
11051105
readonly ttl_ms?: number
11061106
readonly last_used_at: string
1107+
readonly impending_deletion: string
11071108
}
11081109

11091110
// From codersdk/workspaceagents.go

site/src/testHelpers/entities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ export const MockWorkspace: TypesGen.Workspace = {
719719
ttl_ms: 2 * 60 * 60 * 1000,
720720
latest_build: MockWorkspaceBuild,
721721
last_used_at: "2022-05-16T15:29:10.302441433Z",
722+
impending_deletion: "0001-01-01T00:00:00Z",
722723
}
723724

724725
export const MockStoppedWorkspace: TypesGen.Workspace = {

0 commit comments

Comments
 (0)