Skip to content

Commit 64cf76b

Browse files
committed
Apply Danny review suggestions
1 parent 0701572 commit 64cf76b

File tree

4 files changed

+46
-41
lines changed

4 files changed

+46
-41
lines changed

coderd/autobuild/lifecycle_executor.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,6 @@ func (e *Executor) runOnce(t time.Time) Stats {
220220
},
221221
})
222222

223-
dormancy.NotifyWorkspaceDormant(
224-
e.ctx,
225-
e.log,
226-
e.notificationsEnqueuer,
227-
dormancy.WorkspaceDormantNotification{
228-
Workspace: ws,
229-
Initiator: "system",
230-
Reason: "breached the template's threshold for inactivity",
231-
CreatedBy: "lifecycleexecutor",
232-
},
233-
)
234-
235223
auditLog = &auditParams{
236224
Old: wsOld,
237225
New: ws,
@@ -245,6 +233,18 @@ func (e *Executor) runOnce(t time.Time) Stats {
245233
slog.F("time_til_dormant", templateSchedule.TimeTilDormant),
246234
slog.F("since_last_used_at", time.Since(ws.LastUsedAt)),
247235
)
236+
237+
dormancy.NotifyWorkspaceDormant(
238+
e.ctx,
239+
e.log,
240+
e.notificationsEnqueuer,
241+
dormancy.WorkspaceDormantNotification{
242+
Workspace: ws,
243+
Initiator: "system",
244+
Reason: "breached the template's threshold for inactivity",
245+
CreatedBy: "lifecycleexecutor",
246+
},
247+
)
248248
}
249249

250250
if reason == database.BuildReasonAutodelete {

coderd/workspaces.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -958,19 +958,19 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) {
958958
initiator, err := api.Database.GetUserByID(ctx, apiKey.UserID)
959959
if err != nil {
960960
api.Logger.Warn(ctx, "failed to get the initiator by id", slog.Error(err))
961-
return
961+
} else {
962+
dormancy.NotifyWorkspaceDormant(
963+
ctx,
964+
api.Logger,
965+
api.NotificationsEnqueuer,
966+
dormancy.WorkspaceDormantNotification{
967+
Workspace: workspace,
968+
Initiator: initiator.Username,
969+
Reason: "requested by user",
970+
CreatedBy: "api",
971+
},
972+
)
962973
}
963-
dormancy.NotifyWorkspaceDormant(
964-
ctx,
965-
api.Logger,
966-
api.NotificationsEnqueuer,
967-
dormancy.WorkspaceDormantNotification{
968-
Workspace: workspace,
969-
Initiator: initiator.Username,
970-
Reason: "requested by user",
971-
CreatedBy: "api",
972-
},
973-
)
974974
}
975975

976976
data, err := api.workspaceData(ctx, []database.Workspace{workspace})

enterprise/coderd/schedule/template.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
132132
return database.Template{}, xerrors.Errorf("verify autostart requirement: %w", err)
133133
}
134134

135-
var template database.Template
135+
var (
136+
template database.Template
137+
dormantWorkspaces []database.Workspace
138+
)
136139
err = db.InTx(func(tx database.Store) error {
137140
ctx, span := tracing.StartSpanWithName(ctx, "(*schedule.EnterpriseTemplateScheduleStore).Set()-InTx()")
138141
defer span.End()
@@ -166,27 +169,14 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
166169
// to ensure workspaces are being cleaned up correctly. Similarly if we are
167170
// disabling it (by passing 0), then we want to delete nullify the deleting_at
168171
// fields of all the template workspaces.
169-
dormantWorspaces, err := tx.UpdateWorkspacesDormantDeletingAtByTemplateID(ctx, database.UpdateWorkspacesDormantDeletingAtByTemplateIDParams{
172+
dormantWorkspaces, err = tx.UpdateWorkspacesDormantDeletingAtByTemplateID(ctx, database.UpdateWorkspacesDormantDeletingAtByTemplateIDParams{
170173
TemplateID: tpl.ID,
171174
TimeTilDormantAutodeleteMs: opts.TimeTilDormantAutoDelete.Milliseconds(),
172175
DormantAt: dormantAt,
173176
})
174177
if err != nil {
175178
return xerrors.Errorf("update deleting_at of all workspaces for new time_til_dormant_autodelete %q: %w", opts.TimeTilDormantAutoDelete, err)
176179
}
177-
for _, workspace := range dormantWorspaces {
178-
dormancy.NotifyWorkspaceDormant(
179-
ctx,
180-
logger,
181-
ntf,
182-
dormancy.WorkspaceDormantNotification{
183-
Workspace: workspace,
184-
Initiator: "system",
185-
Reason: "template schedule update",
186-
CreatedBy: "scheduletemplate",
187-
},
188-
)
189-
}
190180

191181
if opts.UpdateWorkspaceLastUsedAt != nil {
192182
err = opts.UpdateWorkspaceLastUsedAt(ctx, tx, tpl.ID, s.now())
@@ -213,6 +203,20 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
213203
return database.Template{}, err
214204
}
215205

206+
for _, workspace := range dormantWorkspaces {
207+
dormancy.NotifyWorkspaceDormant(
208+
ctx,
209+
logger,
210+
ntf,
211+
dormancy.WorkspaceDormantNotification{
212+
Workspace: workspace,
213+
Initiator: "system",
214+
Reason: "template schedule update",
215+
CreatedBy: "scheduletemplate",
216+
},
217+
)
218+
}
219+
216220
return template, nil
217221
}
218222

enterprise/coderd/schedule/template_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"cdr.dev/slog"
12+
"cdr.dev/slog/sloggers/slogtest"
1213
"github.com/google/uuid"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
@@ -298,7 +299,7 @@ func TestTemplateUpdateBuildDeadlines(t *testing.T) {
298299
FailureTTL: 0,
299300
TimeTilDormant: 0,
300301
TimeTilDormantAutoDelete: 0,
301-
}, nil, slog.Logger{})
302+
}, nil, slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug))
302303
require.NoError(t, err)
303304

304305
// Check that the workspace build has the expected deadlines.
@@ -578,7 +579,7 @@ func TestTemplateUpdateBuildDeadlinesSkip(t *testing.T) {
578579
FailureTTL: 0,
579580
TimeTilDormant: 0,
580581
TimeTilDormantAutoDelete: 0,
581-
}, nil, slog.Logger{})
582+
}, nil, slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug))
582583
require.NoError(t, err)
583584

584585
// Check each build.

0 commit comments

Comments
 (0)