Skip to content

feat: add customizable ttl_bump interval #10566

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add workspace ttl bump
  • Loading branch information
Emyrk committed Nov 9, 2023
commit 107b9c84c52a2fea1fed9f8633d3920c535e9f19
4 changes: 3 additions & 1 deletion cli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type templateTableRow struct {
ActiveVersionID uuid.UUID `json:"-" table:"active version id"`
UsedBy string `json:"-" table:"used by"`
DefaultTTL time.Duration `json:"-" table:"default ttl"`
DefaultTTLBump time.Duration `json:"-" table:"default ttl bump"`
}

// templateToRows converts a list of templates to a list of templateTableRow for
Expand All @@ -111,7 +112,8 @@ func templatesToRows(templates ...codersdk.Template) []templateTableRow {
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
UsedBy: pretty.Sprint(cliui.DefaultStyles.Fuchsia, formatActiveDevelopers(template.ActiveUserCount)),
DefaultTTL: (time.Duration(template.DefaultTTLMillis) * time.Millisecond),
DefaultTTL: time.Duration(template.DefaultTTLMillis) * time.Millisecond,
DefaultTTLBump: time.Duration(template.DefaultTTLBumpMillis) * time.Millisecond,
}
}

Expand Down
8 changes: 6 additions & 2 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ func (q *FakeQuerier) convertToWorkspaceRowsNoLock(ctx context.Context, workspac
Name: w.Name,
AutostartSchedule: w.AutostartSchedule,
Ttl: w.Ttl,
TtlBump: w.TtlBump,
LastUsedAt: w.LastUsedAt,
DormantAt: w.DormantAt,
DeletingAt: w.DeletingAt,
Expand Down Expand Up @@ -830,8 +831,8 @@ func (q *FakeQuerier) ActivityBumpWorkspace(ctx context.Context, workspaceID uui
}
if !template.AllowUserAutostop {
ttlDur = time.Duration(template.DefaultTTL)
if template.DefaultTtlBump > 0 {
ttlDur += time.Duration(template.DefaultTtlBump)
if template.DefaultTTLBump > 0 {
ttlDur += time.Duration(template.DefaultTTLBump)
}
}
if ttlDur <= 0 {
Expand Down Expand Up @@ -5076,6 +5077,7 @@ func (q *FakeQuerier) InsertWorkspace(_ context.Context, arg database.InsertWork
Name: arg.Name,
AutostartSchedule: arg.AutostartSchedule,
Ttl: arg.Ttl,
TtlBump: arg.TtlBump,
LastUsedAt: arg.LastUsedAt,
AutomaticUpdates: arg.AutomaticUpdates,
}
Expand Down Expand Up @@ -5947,6 +5949,7 @@ func (q *FakeQuerier) UpdateTemplateScheduleByID(_ context.Context, arg database
tpl.AllowUserAutostop = arg.AllowUserAutostop
tpl.UpdatedAt = dbtime.Now()
tpl.DefaultTTL = arg.DefaultTTL
tpl.DefaultTTLBump = arg.DefaultTTLBump
tpl.MaxTTL = arg.MaxTTL
tpl.AutostopRequirementDaysOfWeek = arg.AutostopRequirementDaysOfWeek
tpl.AutostopRequirementWeeks = arg.AutostopRequirementWeeks
Expand Down Expand Up @@ -6677,6 +6680,7 @@ func (q *FakeQuerier) UpdateWorkspaceTTL(_ context.Context, arg database.UpdateW
continue
}
workspace.Ttl = arg.Ttl
workspace.TtlBump = arg.TtlBump
q.workspaces[index] = workspace
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ WHERE
-- workspaces since they are considered soft-deleted.
AND CASE
WHEN @is_dormant :: text != '' THEN
dormant_at IS NOT NULL
dormant_at IS NOT NULL
ELSE
dormant_at IS NULL
END
Expand Down Expand Up @@ -299,11 +299,12 @@ INSERT INTO
name,
autostart_schedule,
ttl,
ttl_bump,
last_used_at,
automatic_updates
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *;
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING *;

-- name: UpdateWorkspaceDeletedByID :exec
UPDATE
Expand Down Expand Up @@ -335,7 +336,8 @@ WHERE
UPDATE
workspaces
SET
ttl = $2
ttl = $2,
ttl_bump = $3
WHERE
id = $1;

Expand Down
1 change: 1 addition & 0 deletions coderd/database/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ overrides:
group_acl: GroupACL
troubleshooting_url: TroubleshootingURL
default_ttl: DefaultTTL
default_ttl_bump: DefaultTTLBump
max_ttl: MaxTTL
template_max_ttl: TemplateMaxTTL
motd_file: MOTDFile
Expand Down
5 changes: 3 additions & 2 deletions coderd/schedule/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (*agplTemplateScheduleStore) Get(ctx context.Context, db database.Store, te
UserAutostartEnabled: true,
UserAutostopEnabled: true,
DefaultTTL: time.Duration(tpl.DefaultTTL),
DefaultTTLBump: time.Duration(tpl.DefaultTTLBump),
// Disregard the values in the database, since AutostopRequirement,
// FailureTTL, TimeTilDormant, and TimeTilDormantAutoDelete are enterprise features.
UseAutostopRequirement: false,
Expand All @@ -208,7 +209,7 @@ func (*agplTemplateScheduleStore) Set(ctx context.Context, db database.Store, tp
ctx, span := tracing.StartSpan(ctx)
defer span.End()

if int64(opts.DefaultTTL) == tpl.DefaultTTL && int64(opts.DefaultTTLBump) == tpl.DefaultTtlBump {
if int64(opts.DefaultTTL) == tpl.DefaultTTL && int64(opts.DefaultTTLBump) == tpl.DefaultTTLBump {
// Avoid updating the UpdatedAt timestamp if nothing will be changed.
return tpl, nil
}
Expand All @@ -219,7 +220,7 @@ func (*agplTemplateScheduleStore) Set(ctx context.Context, db database.Store, tp
ID: tpl.ID,
UpdatedAt: dbtime.Now(),
DefaultTTL: int64(opts.DefaultTTL),
DefaultTtlBump: int64(opts.DefaultTTLBump),
DefaultTTLBump: int64(opts.DefaultTTLBump),
// Don't allow changing these settings, but keep the value in the DB (to
// avoid clearing settings if the license has an issue).
MaxTTL: tpl.MaxTTL,
Expand Down
9 changes: 5 additions & 4 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
// If the user does not specify a value for the default bump,
// set it to the existing value.
if req.DefaultTTLBumpMillis == nil {
ms := time.Duration(template.DefaultTtlBump).Milliseconds()
ms := time.Duration(template.DefaultTTLBump).Milliseconds()
req.DefaultTTLBumpMillis = &ms
}

Expand Down Expand Up @@ -634,7 +634,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
var updated database.Template

err = api.Database.InTx(func(tx database.Store) error {
defaultTTLBump := time.Duration(template.DefaultTtlBump)
defaultTTLBump := time.Duration(template.DefaultTTLBump)
// This should never be nil at this point, but we'll check anyway.
if req.DefaultTTLBumpMillis != nil {
defaultTTLBump = time.Duration(*req.DefaultTTLBumpMillis) * time.Millisecond
Expand All @@ -648,7 +648,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
req.AllowUserAutostop == template.AllowUserAutostop &&
req.AllowUserCancelWorkspaceJobs == template.AllowUserCancelWorkspaceJobs &&
req.DefaultTTLMillis == time.Duration(template.DefaultTTL).Milliseconds() &&
defaultTTLBump == time.Duration(template.DefaultTtlBump) &&
defaultTTLBump == time.Duration(template.DefaultTTLBump) &&
req.MaxTTLMillis == time.Duration(template.MaxTTL).Milliseconds() &&
autostopRequirementDaysOfWeekParsed == scheduleOpts.AutostopRequirement.DaysOfWeek &&
autostartRequirementDaysOfWeekParsed == scheduleOpts.AutostartRequirement.DaysOfWeek &&
Expand Down Expand Up @@ -701,7 +701,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
timeTilDormantAutoDelete := time.Duration(req.TimeTilDormantAutoDeleteMillis) * time.Millisecond

if defaultTTL != time.Duration(template.DefaultTTL) ||
defaultTTLBump != time.Duration(template.DefaultTtlBump) ||
defaultTTLBump != time.Duration(template.DefaultTTLBump) ||
maxTTL != time.Duration(template.MaxTTL) ||
autostopRequirementDaysOfWeekParsed != scheduleOpts.AutostopRequirement.DaysOfWeek ||
autostartRequirementDaysOfWeekParsed != scheduleOpts.AutostartRequirement.DaysOfWeek ||
Expand Down Expand Up @@ -861,6 +861,7 @@ func (api *API) convertTemplate(
Description: template.Description,
Icon: template.Icon,
DefaultTTLMillis: time.Duration(template.DefaultTTL).Milliseconds(),
DefaultTTLBumpMillis: time.Duration(template.DefaultTTLBump).Milliseconds(),
MaxTTLMillis: time.Duration(template.MaxTTL).Milliseconds(),
CreatedByID: template.CreatedBy,
CreatedByName: template.CreatedByUsername,
Expand Down
Loading