- Notifications
You must be signed in to change notification settings - Fork914
feat: add deleted_at field to workspace model#7475
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -20,6 +20,8 @@ | ||
"codersdk", | ||
"cronstrue", | ||
"databasefake", | ||
"dbfake", | ||
"dbgen", | ||
"dbtype", | ||
"DERP", | ||
"derphttp", | ||
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 |
---|---|---|
@@ -1169,7 +1169,10 @@ func convertWorkspace( | ||
autostartSchedule = &workspace.AutostartSchedule.String | ||
} | ||
var ( | ||
ttlMillis = convertWorkspaceTTLMillis(workspace.Ttl) | ||
deletingAt = calculateDeletingAt(workspace, template) | ||
) | ||
return codersdk.Workspace{ | ||
ID: workspace.ID, | ||
CreatedAt: workspace.CreatedAt, | ||
@@ -1188,6 +1191,7 @@ func convertWorkspace( | ||
AutostartSchedule: autostartSchedule, | ||
TTLMillis: ttlMillis, | ||
LastUsedAt: workspace.LastUsedAt, | ||
DeletingAt: deletingAt, | ||
} | ||
} | ||
@@ -1200,6 +1204,22 @@ func convertWorkspaceTTLMillis(i sql.NullInt64) *int64 { | ||
return &millis | ||
} | ||
// Calculate the time of the upcoming workspace deletion, if applicable; otherwise, return nil. | ||
// Workspaces may have impending deletions if InactivityTTL feature is turned on and the workspace is inactive. | ||
func calculateDeletingAt(workspace database.Workspace, template database.Template) *time.Time { | ||
var ( | ||
year, month, day = time.Now().Date() | ||
beginningOfToday = time.Date(year, month, day, 0, 0, 0, 0, time.Now().Location()) | ||
) | ||
// If InactivityTTL is turned off (set to 0), if the workspace has already been deleted, | ||
// or if the workspace was used sometime within the last day, there is no impending deletion | ||
if template.InactivityTTL == 0 || workspace.Deleted || workspace.LastUsedAt.After(beginningOfToday) { | ||
return nil | ||
} | ||
return ptr.Ref(workspace.LastUsedAt.Add(time.Duration(template.InactivityTTL) * time.Nanosecond)) | ||
} | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
func validWorkspaceTTLMillis(millis *int64, templateDefault, templateMax time.Duration) (sql.NullInt64, error) { | ||
if templateDefault == 0 && templateMax != 0 || (templateMax > 0 && templateDefault > templateMax) { | ||
templateDefault = templateMax | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package coderd | ||
import ( | ||
"testing" | ||
"time" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/coderd/util/ptr" | ||
) | ||
func Test_calculateDeletingAt(t *testing.T) { | ||
t.Parallel() | ||
testCases := []struct { | ||
name string | ||
workspace database.Workspace | ||
template database.Template | ||
expected *time.Time | ||
}{ | ||
{ | ||
name: "DeletingAt", | ||
workspace: database.Workspace{ | ||
Deleted: false, | ||
LastUsedAt: time.Now().Add(time.Duration(-10) * time.Hour * 24), // 10 days ago | ||
}, | ||
template: database.Template{ | ||
InactivityTTL: int64(9 * 24 * time.Hour), // 9 days | ||
}, | ||
expected: ptr.Ref(time.Now().Add(time.Duration(-1) * time.Hour * 24)), // yesterday | ||
}, | ||
{ | ||
name: "InactivityTTLUnset", | ||
workspace: database.Workspace{ | ||
Deleted: false, | ||
LastUsedAt: time.Now().Add(time.Duration(-10) * time.Hour * 24), | ||
}, | ||
template: database.Template{ | ||
InactivityTTL: 0, | ||
}, | ||
expected: nil, | ||
}, | ||
{ | ||
name: "DeletedWorkspace", | ||
workspace: database.Workspace{ | ||
Deleted: true, | ||
LastUsedAt: time.Now().Add(time.Duration(-10) * time.Hour * 24), | ||
}, | ||
template: database.Template{ | ||
InactivityTTL: int64(9 * 24 * time.Hour), | ||
}, | ||
expected: nil, | ||
}, | ||
{ | ||
name: "ActiveWorkspace", | ||
workspace: database.Workspace{ | ||
Deleted: true, | ||
LastUsedAt: time.Now().Add(time.Duration(-5) * time.Hour), // 5 hours ago | ||
}, | ||
template: database.Template{ | ||
InactivityTTL: int64(1 * 24 * time.Hour), // 1 day | ||
}, | ||
expected: nil, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
found := calculateDeletingAt(tc.workspace, tc.template) | ||
if tc.expected == nil { | ||
require.Nil(t, found, "impending deletion should be nil") | ||
} else { | ||
require.NotNil(t, found) | ||
require.WithinDuration(t, *tc.expected, *found, time.Second, "incorrect impending deletion") | ||
} | ||
}) | ||
} | ||
} |