@@ -19,7 +19,6 @@ import (
19
19
"github.com/coder/coder/v2/coderd/database/dbtime"
20
20
"github.com/coder/coder/v2/coderd/database/provisionerjobs"
21
21
"github.com/coder/coder/v2/coderd/database/pubsub"
22
- "github.com/coder/coder/v2/coderd/dormancy"
23
22
"github.com/coder/coder/v2/coderd/notifications"
24
23
"github.com/coder/coder/v2/coderd/schedule"
25
24
"github.com/coder/coder/v2/coderd/wsbuilder"
@@ -145,10 +144,11 @@ func (e *Executor) runOnce(t time.Time) Stats {
145
144
var (
146
145
job * database.ProvisionerJob
147
146
auditLog * auditParams
148
- dormantNotification * dormancy. WorkspaceDormantNotification
147
+ shouldNotifyDormancy bool
149
148
nextBuild * database.WorkspaceBuild
150
149
activeTemplateVersion database.TemplateVersion
151
150
ws database.Workspace
151
+ tmpl database.Template
152
152
didAutoUpdate bool
153
153
)
154
154
err := e .db .InTx (func (tx database.Store ) error {
@@ -182,17 +182,17 @@ func (e *Executor) runOnce(t time.Time) Stats {
182
182
return xerrors .Errorf ("get template scheduling options: %w" , err )
183
183
}
184
184
185
- template , err : = tx .GetTemplateByID (e .ctx , ws .TemplateID )
185
+ tmpl , err = tx .GetTemplateByID (e .ctx , ws .TemplateID )
186
186
if err != nil {
187
187
return xerrors .Errorf ("get template by ID: %w" , err )
188
188
}
189
189
190
- activeTemplateVersion , err = tx .GetTemplateVersionByID (e .ctx , template .ActiveVersionID )
190
+ activeTemplateVersion , err = tx .GetTemplateVersionByID (e .ctx , tmpl .ActiveVersionID )
191
191
if err != nil {
192
192
return xerrors .Errorf ("get active template version by ID: %w" , err )
193
193
}
194
194
195
- accessControl := (* (e .accessControlStore .Load ())).GetTemplateAccessControl (template )
195
+ accessControl := (* (e .accessControlStore .Load ())).GetTemplateAccessControl (tmpl )
196
196
197
197
nextTransition , reason , err := getNextTransition (user , ws , latestBuild , latestJob , templateSchedule , currentTick )
198
198
if err != nil {
@@ -215,7 +215,7 @@ func (e *Executor) runOnce(t time.Time) Stats {
215
215
log .Debug (e .ctx , "autostarting with active version" )
216
216
builder = builder .ActiveVersion ()
217
217
218
- if latestBuild .TemplateVersionID != template .ActiveVersionID {
218
+ if latestBuild .TemplateVersionID != tmpl .ActiveVersionID {
219
219
// control flag to know if the workspace was auto-updated,
220
220
// so the lifecycle executor can notify the user
221
221
didAutoUpdate = true
@@ -248,12 +248,7 @@ func (e *Executor) runOnce(t time.Time) Stats {
248
248
return xerrors .Errorf ("update workspace dormant deleting at: %w" , err )
249
249
}
250
250
251
- dormantNotification = & dormancy.WorkspaceDormantNotification {
252
- Workspace : ws ,
253
- Initiator : "autobuild" ,
254
- Reason : "breached the template's threshold for inactivity" ,
255
- CreatedBy : "lifecycleexecutor" ,
256
- }
251
+ shouldNotifyDormancy = true
257
252
258
253
log .Info (e .ctx , "dormant workspace" ,
259
254
slog .F ("last_used_at" , ws .LastUsedAt ),
@@ -325,14 +320,24 @@ func (e *Executor) runOnce(t time.Time) Stats {
325
320
return xerrors .Errorf ("post provisioner job to pubsub: %w" , err )
326
321
}
327
322
}
328
- if dormantNotification != nil {
329
- _ , err = dormancy . NotifyWorkspaceDormant (
323
+ if shouldNotifyDormancy {
324
+ _ , err = e . notificationsEnqueuer . Enqueue (
330
325
e .ctx ,
331
- e .notificationsEnqueuer ,
332
- * dormantNotification ,
326
+ ws .OwnerID ,
327
+ notifications .TemplateWorkspaceDormant ,
328
+ map [string ]string {
329
+ "name" : ws .Name ,
330
+ "reason" : "inactivity exceeded the dormancy threshold" ,
331
+ "timeTilDormant" : time .Duration (tmpl .TimeTilDormant ).String (),
332
+ },
333
+ "lifecycle_executor" ,
334
+ ws .ID ,
335
+ ws .OwnerID ,
336
+ ws .TemplateID ,
337
+ ws .OrganizationID ,
333
338
)
334
339
if err != nil {
335
- log .Warn (e .ctx , "failed to notify of workspace marked as dormant" , slog .Error (err ), slog .F ("workspace_id" , dormantNotification . Workspace .ID ))
340
+ log .Warn (e .ctx , "failed to notify of workspace marked as dormant" , slog .Error (err ), slog .F ("workspace_id" , ws .ID ))
336
341
}
337
342
}
338
343
return nil
0 commit comments