Skip to content

Commit 9a35cbb

Browse files
committed
Notify dormant workspace on lifecycle executor
1 parent 40e5801 commit 9a35cbb

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

cli/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
10651065
autobuildTicker := time.NewTicker(vals.AutobuildPollInterval.Value())
10661066
defer autobuildTicker.Stop()
10671067
autobuildExecutor := autobuild.NewExecutor(
1068-
ctx, options.Database, options.Pubsub, coderAPI.TemplateScheduleStore, &coderAPI.Auditor, coderAPI.AccessControlStore, logger, autobuildTicker.C)
1068+
ctx, options.Database, options.Pubsub, coderAPI.TemplateScheduleStore, &coderAPI.Auditor, coderAPI.AccessControlStore, logger, autobuildTicker.C, options.NotificationsEnqueuer)
10691069
autobuildExecutor.Run()
10701070

10711071
hangDetectorTicker := time.NewTicker(vals.JobHangDetectorInterval.Value())

coderd/autobuild/lifecycle_executor.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import (
1919
"github.com/coder/coder/v2/coderd/database/dbtime"
2020
"github.com/coder/coder/v2/coderd/database/provisionerjobs"
2121
"github.com/coder/coder/v2/coderd/database/pubsub"
22+
"github.com/coder/coder/v2/coderd/notifications"
2223
"github.com/coder/coder/v2/coderd/schedule"
2324
"github.com/coder/coder/v2/coderd/wsbuilder"
25+
"github.com/coder/coder/v2/enterprise/coderd/dormancy"
2426
)
2527

2628
// Executor automatically starts or stops workspaces.
@@ -34,6 +36,8 @@ type Executor struct {
3436
log slog.Logger
3537
tick <-chan time.Time
3638
statsCh chan<- Stats
39+
// NotificationsEnqueuer handles enqueueing notifications for delivery by SMTP, webhook, etc.
40+
notificationsEnqueuer notifications.Enqueuer
3741
}
3842

3943
// Stats contains information about one run of Executor.
@@ -44,7 +48,7 @@ type Stats struct {
4448
}
4549

4650
// New returns a new wsactions executor.
47-
func NewExecutor(ctx context.Context, db database.Store, ps pubsub.Pubsub, tss *atomic.Pointer[schedule.TemplateScheduleStore], auditor *atomic.Pointer[audit.Auditor], acs *atomic.Pointer[dbauthz.AccessControlStore], log slog.Logger, tick <-chan time.Time) *Executor {
51+
func NewExecutor(ctx context.Context, db database.Store, ps pubsub.Pubsub, tss *atomic.Pointer[schedule.TemplateScheduleStore], auditor *atomic.Pointer[audit.Auditor], acs *atomic.Pointer[dbauthz.AccessControlStore], log slog.Logger, tick <-chan time.Time, ntf notifications.Enqueuer) *Executor {
4852
le := &Executor{
4953
//nolint:gocritic // Autostart has a limited set of permissions.
5054
ctx: dbauthz.AsAutostart(ctx),
@@ -55,6 +59,7 @@ func NewExecutor(ctx context.Context, db database.Store, ps pubsub.Pubsub, tss *
5559
log: log.Named("autobuild"),
5660
auditor: auditor,
5761
accessControlStore: acs,
62+
notificationsEnqueuer: ntf,
5863
}
5964
return le
6065
}
@@ -215,6 +220,18 @@ func (e *Executor) runOnce(t time.Time) Stats {
215220
},
216221
})
217222

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+
218235
auditLog = &auditParams{
219236
Old: wsOld,
220237
New: ws,

coderd/coderdtest/coderdtest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
308308
accessControlStore,
309309
*options.Logger,
310310
options.AutobuildTicker,
311+
options.NotificationEnqueuer,
311312
).WithStatsChannel(options.AutobuildStats)
312313
lifecycleExecutor.Run()
313314

0 commit comments

Comments
 (0)