Skip to content

Commit c3aef93

Browse files
authored
feat: add locked TTL field to template meta (#8020)
1 parent 1ecc371 commit c3aef93

27 files changed

+291
-90
lines changed

coderd/apidoc/docs.go

+9-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+9-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dump.sql

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BEGIN;
2+
ALTER TABLE templates DROP COLUMN locked_ttl;
3+
COMMIT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BEGIN;
2+
ALTER TABLE templates ADD COLUMN locked_ttl BIGINT NOT NULL DEFAULT 0;
3+
COMMIT;

coderd/database/modelqueries.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
5454
pq.Array(arg.IDs),
5555
)
5656
if err != nil {
57-
return nil, err
57+
return nil, xerrors.Errorf("query context: %w", err)
5858
}
5959
defer rows.Close()
6060
var items []Template
@@ -82,16 +82,17 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
8282
&i.AllowUserAutostop,
8383
&i.FailureTTL,
8484
&i.InactivityTTL,
85+
&i.LockedTTL,
8586
); err != nil {
86-
return nil, err
87+
return nil, xerrors.Errorf("scan: %w", err)
8788
}
8889
items = append(items, i)
8990
}
9091
if err := rows.Close(); err != nil {
91-
return nil, err
92+
return nil, xerrors.Errorf("close: %w", err)
9293
}
9394
if err := rows.Err(); err != nil {
94-
return nil, err
95+
return nil, xerrors.Errorf("rows err: %w", err)
9596
}
9697
return items, nil
9798
}

coderd/database/models.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+20-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/templates.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ SET
120120
default_ttl = $5,
121121
max_ttl = $6,
122122
failure_ttl = $7,
123-
inactivity_ttl = $8
123+
inactivity_ttl = $8,
124+
locked_ttl = $9
124125
WHERE
125126
id = $1
126127
RETURNING

coderd/database/sqlc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ overrides:
5656
failure_ttl: FailureTTL
5757
inactivity_ttl: InactivityTTL
5858
eof: EOF
59+
locked_ttl: LockedTTL
5960

6061
sql:
6162
- schema: "./dump.sql"

coderd/schedule/template.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ type TemplateScheduleOptions struct {
1818
//
1919
// If set, users cannot disable automatic workspace shutdown.
2020
MaxTTL time.Duration `json:"max_ttl"`
21-
// If FailureTTL is set, all failed workspaces will be stopped automatically after this time has elapsed.
21+
// FailureTTL dictates the duration after which failed workspaces will be stopped automatically.
2222
FailureTTL time.Duration `json:"failure_ttl"`
23-
// If InactivityTTL is set, all inactive workspaces will be deleted automatically after this time has elapsed.
23+
// InactivityTTL dictates the duration after which inactive workspaces will be locked.
2424
InactivityTTL time.Duration `json:"inactivity_ttl"`
25+
// LockedTTL dictates the duration after which locked workspaces will be permanently deleted.
26+
LockedTTL time.Duration `json:"locked_ttl"`
2527
}
2628

2729
// TemplateScheduleStore provides an interface for retrieving template
@@ -51,11 +53,12 @@ func (*agplTemplateScheduleStore) GetTemplateScheduleOptions(ctx context.Context
5153
UserAutostartEnabled: true,
5254
UserAutostopEnabled: true,
5355
DefaultTTL: time.Duration(tpl.DefaultTTL),
54-
// Disregard the values in the database, since MaxTTL, FailureTTL, and InactivityTTL are enterprise
56+
// Disregard the values in the database, since MaxTTL, FailureTTL, InactivityTTL, and LockedTTL are enterprise
5557
// features.
5658
MaxTTL: 0,
5759
FailureTTL: 0,
5860
InactivityTTL: 0,
61+
LockedTTL: 0,
5962
}, nil
6063
}
6164

@@ -76,5 +79,6 @@ func (*agplTemplateScheduleStore) SetTemplateScheduleOptions(ctx context.Context
7679
MaxTTL: tpl.MaxTTL,
7780
FailureTTL: tpl.FailureTTL,
7881
InactivityTTL: tpl.InactivityTTL,
82+
LockedTTL: tpl.LockedTTL,
7983
})
8084
}

coderd/templates.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
497497
if req.InactivityTTLMillis < 0 {
498498
validErrs = append(validErrs, codersdk.ValidationError{Field: "inactivity_ttl_ms", Detail: "Must be a positive integer."})
499499
}
500+
if req.InactivityTTLMillis < 0 {
501+
validErrs = append(validErrs, codersdk.ValidationError{Field: "inactivity_ttl_ms", Detail: "Must be a positive integer."})
502+
}
503+
if req.LockedTTLMillis < 0 {
504+
validErrs = append(validErrs, codersdk.ValidationError{Field: "locked_ttl_ms", Detail: "Must be a positive integer."})
505+
}
500506

501507
if len(validErrs) > 0 {
502508
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
@@ -518,7 +524,8 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
518524
req.DefaultTTLMillis == time.Duration(template.DefaultTTL).Milliseconds() &&
519525
req.MaxTTLMillis == time.Duration(template.MaxTTL).Milliseconds() &&
520526
req.FailureTTLMillis == time.Duration(template.FailureTTL).Milliseconds() &&
521-
req.InactivityTTLMillis == time.Duration(template.InactivityTTL).Milliseconds() {
527+
req.InactivityTTLMillis == time.Duration(template.InactivityTTL).Milliseconds() &&
528+
req.FailureTTLMillis == time.Duration(template.LockedTTL).Milliseconds() {
522529
return nil
523530
}
524531

@@ -546,11 +553,13 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
546553
maxTTL := time.Duration(req.MaxTTLMillis) * time.Millisecond
547554
failureTTL := time.Duration(req.FailureTTLMillis) * time.Millisecond
548555
inactivityTTL := time.Duration(req.InactivityTTLMillis) * time.Millisecond
556+
lockedTTL := time.Duration(req.LockedTTLMillis) * time.Millisecond
549557

550558
if defaultTTL != time.Duration(template.DefaultTTL) ||
551559
maxTTL != time.Duration(template.MaxTTL) ||
552560
failureTTL != time.Duration(template.FailureTTL) ||
553561
inactivityTTL != time.Duration(template.InactivityTTL) ||
562+
lockedTTL != time.Duration(template.LockedTTL) ||
554563
req.AllowUserAutostart != template.AllowUserAutostart ||
555564
req.AllowUserAutostop != template.AllowUserAutostop {
556565
updated, err = (*api.TemplateScheduleStore.Load()).SetTemplateScheduleOptions(ctx, tx, updated, schedule.TemplateScheduleOptions{
@@ -563,6 +572,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
563572
MaxTTL: maxTTL,
564573
FailureTTL: failureTTL,
565574
InactivityTTL: inactivityTTL,
575+
LockedTTL: lockedTTL,
566576
})
567577
if err != nil {
568578
return xerrors.Errorf("set template schedule options: %w", err)
@@ -716,5 +726,6 @@ func (api *API) convertTemplate(
716726
AllowUserCancelWorkspaceJobs: template.AllowUserCancelWorkspaceJobs,
717727
FailureTTLMillis: time.Duration(template.FailureTTL).Milliseconds(),
718728
InactivityTTLMillis: time.Duration(template.InactivityTTL).Milliseconds(),
729+
LockedTTLMillis: time.Duration(template.LockedTTL).Milliseconds(),
719730
}
720731
}

0 commit comments

Comments
 (0)