diff --git a/coderd/autobuild/lifecycle_executor.go b/coderd/autobuild/lifecycle_executor.go index 8da233d6d610f..5bd8efe2b9fcf 100644 --- a/coderd/autobuild/lifecycle_executor.go +++ b/coderd/autobuild/lifecycle_executor.go @@ -8,6 +8,7 @@ import ( "sync/atomic" "time" + "github.com/dustin/go-humanize" "github.com/google/uuid" "golang.org/x/sync/errgroup" "golang.org/x/xerrors" @@ -323,6 +324,7 @@ func (e *Executor) runOnce(t time.Time) Stats { } } if shouldNotifyDormancy { + dormantTime := dbtime.Now().Add(time.Duration(tmpl.TimeTilDormant)) _, err = e.notificationsEnqueuer.Enqueue( e.ctx, ws.OwnerID, @@ -330,7 +332,7 @@ func (e *Executor) runOnce(t time.Time) Stats { map[string]string{ "name": ws.Name, "reason": "inactivity exceeded the dormancy threshold", - "timeTilDormant": time.Duration(tmpl.TimeTilDormant).String(), + "timeTilDormant": humanize.Time(dormantTime), }, "lifecycle_executor", ws.ID, diff --git a/coderd/notifications/notifications_test.go b/coderd/notifications/notifications_test.go index e21dce4246f20..95b3a84026fad 100644 --- a/coderd/notifications/notifications_test.go +++ b/coderd/notifications/notifications_test.go @@ -691,7 +691,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) { "reason": "breached the template's threshold for inactivity", "initiator": "autobuild", "dormancyHours": "24", - "timeTilDormant": "24h", + "timeTilDormant": "24 hours", }, }, }, @@ -716,7 +716,20 @@ func TestNotificationTemplatesCanRender(t *testing.T) { "name": "bobby-workspace", "reason": "template updated to new dormancy policy", "dormancyHours": "24", - "timeTilDormant": "24h", + "timeTilDormant": "24 hours", + }, + }, + }, + { + name: "TemplateWorkspaceMarkedForDeletionInOneWeek", + id: notifications.TemplateWorkspaceMarkedForDeletion, + payload: types.MessagePayload{ + UserName: "bobby", + Labels: map[string]string{ + "name": "bobby-workspace", + "reason": "template updated to new dormancy policy", + "dormancyHours": "168", // 168 hours = 7 days = 1 week + "timeTilDormant": "1 week", }, }, }, diff --git a/coderd/workspaces.go b/coderd/workspaces.go index 901e3723964bd..bde8f1c917d39 100644 --- a/coderd/workspaces.go +++ b/coderd/workspaces.go @@ -11,6 +11,7 @@ import ( "strconv" "time" + "github.com/dustin/go-humanize" "github.com/go-chi/chi/v5" "github.com/google/uuid" "golang.org/x/xerrors" @@ -1055,6 +1056,7 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) { } if initiatorErr == nil && tmplErr == nil { + dormantTime := dbtime.Now().Add(time.Duration(tmpl.TimeTilDormant)) _, err = api.NotificationsEnqueuer.Enqueue( ctx, workspace.OwnerID, @@ -1062,7 +1064,7 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) { map[string]string{ "name": workspace.Name, "reason": "a " + initiator.Username + " request", - "timeTilDormant": time.Duration(tmpl.TimeTilDormant).String(), + "timeTilDormant": humanize.Time(dormantTime), }, "api", workspace.ID, diff --git a/enterprise/coderd/schedule/template.go b/enterprise/coderd/schedule/template.go index c38b8f509b5c3..6b148e8ef4708 100644 --- a/enterprise/coderd/schedule/template.go +++ b/enterprise/coderd/schedule/template.go @@ -8,6 +8,7 @@ import ( "cdr.dev/slog" + "github.com/dustin/go-humanize" "github.com/google/uuid" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" @@ -205,6 +206,7 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S } for _, ws := range markedForDeletion { + dormantTime := dbtime.Now().Add(opts.TimeTilDormantAutoDelete) _, err = s.enqueuer.Enqueue( ctx, ws.OwnerID, @@ -212,7 +214,7 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S map[string]string{ "name": ws.Name, "reason": "an update to the template's dormancy", - "timeTilDormant": opts.TimeTilDormantAutoDelete.String(), + "timeTilDormant": humanize.Time(dormantTime), }, "scheduletemplate", // Associate this notification with all the related entities.