Skip to content

Commit ea45dfd

Browse files
committed
chore: move template meta last_used_at update to workspacestats
1 parent bee4ece commit ea45dfd

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

coderd/schedule/template.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -114,38 +114,38 @@ func VerifyTemplateAutostartRequirement(days uint8) error {
114114
}
115115

116116
type TemplateScheduleOptions struct {
117-
UserAutostartEnabled bool `json:"user_autostart_enabled"`
118-
UserAutostopEnabled bool `json:"user_autostop_enabled"`
119-
DefaultTTL time.Duration `json:"default_ttl"`
117+
UserAutostartEnabled bool
118+
UserAutostopEnabled bool
119+
DefaultTTL time.Duration
120120
// ActivityBump dictates the duration to bump the workspace's deadline by if
121121
// Coder detects activity from the user. A value of 0 means no bumping.
122-
ActivityBump time.Duration `json:"activity_bump"`
122+
ActivityBump time.Duration
123123
// AutostopRequirement dictates when the workspace must be restarted. This
124124
// used to be handled by MaxTTL.
125-
AutostopRequirement TemplateAutostopRequirement `json:"autostop_requirement"`
125+
AutostopRequirement TemplateAutostopRequirement
126126
// AutostartRequirement dictates when the workspace can be auto started.
127-
AutostartRequirement TemplateAutostartRequirement `json:"autostart_requirement"`
127+
AutostartRequirement TemplateAutostartRequirement
128128
// FailureTTL dictates the duration after which failed workspaces will be
129129
// stopped automatically.
130-
FailureTTL time.Duration `json:"failure_ttl"`
130+
FailureTTL time.Duration
131131
// TimeTilDormant dictates the duration after which inactive workspaces will
132132
// go dormant.
133-
TimeTilDormant time.Duration `json:"time_til_dormant"`
133+
TimeTilDormant time.Duration
134134
// TimeTilDormantAutoDelete dictates the duration after which dormant workspaces will be
135135
// permanently deleted.
136-
TimeTilDormantAutoDelete time.Duration `json:"time_til_dormant_autodelete"`
136+
TimeTilDormantAutoDelete time.Duration
137137
// UpdateWorkspaceLastUsedAt updates the template's workspaces'
138138
// last_used_at field. This is useful for preventing updates to the
139139
// templates inactivity_ttl immediately triggering a dormant action against
140140
// workspaces whose last_used_at field violates the new template
141141
// inactivity_ttl threshold.
142-
UpdateWorkspaceLastUsedAt bool `json:"update_workspace_last_used_at"`
142+
UpdateWorkspaceLastUsedAt func(ctx context.Context, db database.Store, templateID uuid.UUID, lastUsedAt time.Time) error `json:"update_workspace_last_used_at"`
143143
// UpdateWorkspaceDormantAt updates the template's workspaces'
144144
// dormant_at field. This is useful for preventing updates to the
145145
// templates locked_ttl immediately triggering a delete action against
146146
// workspaces whose dormant_at field violates the new template time_til_dormant_autodelete
147147
// threshold.
148-
UpdateWorkspaceDormantAt bool `json:"update_workspace_dormant_at"`
148+
UpdateWorkspaceDormantAt bool
149149
}
150150

151151
// TemplateScheduleStore provides an interface for retrieving template

coderd/templates.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/coder/coder/v2/coderd/schedule"
2424
"github.com/coder/coder/v2/coderd/telemetry"
2525
"github.com/coder/coder/v2/coderd/util/ptr"
26+
"github.com/coder/coder/v2/coderd/workspacestats"
2627
"github.com/coder/coder/v2/codersdk"
2728
"github.com/coder/coder/v2/examples"
2829
)
@@ -726,6 +727,10 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
726727
failureTTL := time.Duration(req.FailureTTLMillis) * time.Millisecond
727728
inactivityTTL := time.Duration(req.TimeTilDormantMillis) * time.Millisecond
728729
timeTilDormantAutoDelete := time.Duration(req.TimeTilDormantAutoDeleteMillis) * time.Millisecond
730+
var updateWorkspaceLastUsedAt workspacestats.UpdateTemplateWorkspacesLastUsedAtFunc
731+
if req.UpdateWorkspaceLastUsedAt {
732+
updateWorkspaceLastUsedAt = workspacestats.UpdateTemplateWorkspacesLastUsedAt
733+
}
729734

730735
if defaultTTL != time.Duration(template.DefaultTTL) ||
731736
activityBump != time.Duration(template.ActivityBump) ||
@@ -755,7 +760,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
755760
FailureTTL: failureTTL,
756761
TimeTilDormant: inactivityTTL,
757762
TimeTilDormantAutoDelete: timeTilDormantAutoDelete,
758-
UpdateWorkspaceLastUsedAt: req.UpdateWorkspaceLastUsedAt,
763+
UpdateWorkspaceLastUsedAt: updateWorkspaceLastUsedAt,
759764
UpdateWorkspaceDormantAt: req.UpdateWorkspaceDormantAt,
760765
})
761766
if err != nil {

coderd/workspacestats/reporter.go

+13
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,16 @@ func (r *Reporter) ReportAgentStats(ctx context.Context, now time.Time, workspac
192192

193193
return nil
194194
}
195+
196+
type UpdateTemplateWorkspacesLastUsedAtFunc func(ctx context.Context, db database.Store, templateID uuid.UUID, lastUsedAt time.Time) error
197+
198+
func UpdateTemplateWorkspacesLastUsedAt(ctx context.Context, db database.Store, templateID uuid.UUID, lastUsedAt time.Time) error {
199+
err := db.UpdateTemplateWorkspacesLastUsedAt(ctx, database.UpdateTemplateWorkspacesLastUsedAtParams{
200+
TemplateID: templateID,
201+
LastUsedAt: lastUsedAt,
202+
})
203+
if err != nil {
204+
return xerrors.Errorf("update template workspaces last used at: %w", err)
205+
}
206+
return nil
207+
}

enterprise/coderd/schedule/template.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,10 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
168168
return xerrors.Errorf("update deleting_at of all workspaces for new time_til_dormant_autodelete %q: %w", opts.TimeTilDormantAutoDelete, err)
169169
}
170170

171-
if opts.UpdateWorkspaceLastUsedAt {
172-
// nolint:gocritic // (#13146) Will be moved soon as part of refactor.
173-
err = tx.UpdateTemplateWorkspacesLastUsedAt(ctx, database.UpdateTemplateWorkspacesLastUsedAtParams{
174-
TemplateID: tpl.ID,
175-
LastUsedAt: dbtime.Now(),
176-
})
171+
if opts.UpdateWorkspaceLastUsedAt != nil {
172+
err = opts.UpdateWorkspaceLastUsedAt(ctx, tx, tpl.ID, s.now())
177173
if err != nil {
178-
return xerrors.Errorf("update template workspaces last_used_at: %w", err)
174+
return xerrors.Errorf("update workspace last used at: %w", err)
179175
}
180176
}
181177

0 commit comments

Comments
 (0)