Skip to content

Commit 467a797

Browse files
committed
Possible implementation simplification
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 68bcfb0 commit 467a797

13 files changed

+97
-69
lines changed

coderd/database/migrations/000226_dormancy_notification_template.up.sql

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
INSERT INTO notification_templates (id, name, title_template, body_template, "group", actions)
2+
VALUES ('123e4567-e89b-12d3-a456-426614174000', 'Workspace Marked as Dormant', E'Workspace "{{.Labels.name}}" marked as dormant',
3+
E'Hi {{.UserName}}\n\n' ||
4+
E'Your workspace **{{.Labels.name}}** has been marked as **dormant**.\n' ||
5+
E'The specified reason was "**{{.Labels.reason}}{{ if .Labels.initiator }} (initiated by: {{ .Labels.initiator }}){{end}}**\n\n' ||
6+
E'Dormancy refers to a workspace being unused for a defined length of time, and after it exceeds {{.Labels.dormancyHours}} hours of dormancy it will be deleted.\n' ||
7+
E'To prevent your workspace from being deleted, simply use it as normal.',
8+
'Workspace Events', '[
9+
{
10+
"label": "View workspace",
11+
"url": "{{ base_url }}/@{{.UserName}}/{{.Labels.name}}"
12+
}
13+
]'::jsonb);

coderd/schedule/mock.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import (
55

66
"github.com/google/uuid"
77

8-
"cdr.dev/slog"
9-
108
"github.com/coder/coder/v2/coderd/database"
11-
"github.com/coder/coder/v2/coderd/notifications"
129
)
1310

1411
type MockTemplateScheduleStore struct {
1512
GetFn func(ctx context.Context, db database.Store, templateID uuid.UUID) (TemplateScheduleOptions, error)
16-
SetFn func(ctx context.Context, db database.Store, template database.Template, options TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error)
13+
SetFn func(ctx context.Context, db database.Store, template database.Template, options TemplateScheduleOptions) (database.Template, error)
1714
}
1815

1916
var _ TemplateScheduleStore = MockTemplateScheduleStore{}
@@ -26,12 +23,12 @@ func (m MockTemplateScheduleStore) Get(ctx context.Context, db database.Store, t
2623
return NewAGPLTemplateScheduleStore().Get(ctx, db, templateID)
2724
}
2825

29-
func (m MockTemplateScheduleStore) Set(ctx context.Context, db database.Store, template database.Template, options TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error) {
26+
func (m MockTemplateScheduleStore) Set(ctx context.Context, db database.Store, template database.Template, opts TemplateScheduleOptions) (database.Template, error) {
3027
if m.SetFn != nil {
31-
return m.SetFn(ctx, db, template, options, enqueuer, logger)
28+
return m.SetFn(ctx, db, template, opts)
3229
}
3330

34-
return NewAGPLTemplateScheduleStore().Set(ctx, db, template, options, enqueuer, logger)
31+
return NewAGPLTemplateScheduleStore().Set(ctx, db, template, opts)
3532
}
3633

3734
type MockUserQuietHoursScheduleStore struct {

coderd/schedule/template.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import (
44
"context"
55
"time"
66

7-
"cdr.dev/slog"
8-
97
"github.com/google/uuid"
108
"golang.org/x/xerrors"
119

1210
"github.com/coder/coder/v2/coderd/database"
1311
"github.com/coder/coder/v2/coderd/database/dbtime"
14-
"github.com/coder/coder/v2/coderd/notifications"
1512
"github.com/coder/coder/v2/coderd/tracing"
1613
)
1714

@@ -155,7 +152,7 @@ type TemplateScheduleOptions struct {
155152
// scheduling options set by the template/site admin.
156153
type TemplateScheduleStore interface {
157154
Get(ctx context.Context, db database.Store, templateID uuid.UUID) (TemplateScheduleOptions, error)
158-
Set(ctx context.Context, db database.Store, template database.Template, opts TemplateScheduleOptions, enqueuer notifications.Enqueuer, log slog.Logger) (database.Template, error)
155+
Set(ctx context.Context, db database.Store, template database.Template, opts TemplateScheduleOptions) (database.Template, error)
159156
}
160157

161158
type agplTemplateScheduleStore struct{}
@@ -200,7 +197,7 @@ func (*agplTemplateScheduleStore) Get(ctx context.Context, db database.Store, te
200197
}, nil
201198
}
202199

203-
func (*agplTemplateScheduleStore) Set(ctx context.Context, db database.Store, tpl database.Template, opts TemplateScheduleOptions, enqueuer notifications.Enqueuer, log slog.Logger) (database.Template, error) {
200+
func (*agplTemplateScheduleStore) Set(ctx context.Context, db database.Store, tpl database.Template, opts TemplateScheduleOptions) (database.Template, error) {
204201
ctx, span := tracing.StartSpan(ctx)
205202
defer span.End()
206203

coderd/templates.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
383383
FailureTTL: failureTTL,
384384
TimeTilDormant: dormantTTL,
385385
TimeTilDormantAutoDelete: dormantAutoDeletionTTL,
386-
}, api.NotificationsEnqueuer, api.Logger)
386+
})
387387
if err != nil {
388388
return xerrors.Errorf("set template schedule options: %s", err)
389389
}
@@ -776,7 +776,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
776776
TimeTilDormantAutoDelete: timeTilDormantAutoDelete,
777777
UpdateWorkspaceLastUsedAt: updateWorkspaceLastUsedAt,
778778
UpdateWorkspaceDormantAt: req.UpdateWorkspaceDormantAt,
779-
}, api.NotificationsEnqueuer, api.Logger)
779+
})
780780
if err != nil {
781781
return xerrors.Errorf("set template schedule options: %w", err)
782782
}

coderd/templates_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
1313

14-
"cdr.dev/slog"
1514
"cdr.dev/slog/sloggers/slogtest"
15+
1616
"github.com/coder/coder/v2/agent/agenttest"
1717
"github.com/coder/coder/v2/coderd/audit"
1818
"github.com/coder/coder/v2/coderd/coderdtest"
1919
"github.com/coder/coder/v2/coderd/database"
2020
"github.com/coder/coder/v2/coderd/database/dbauthz"
2121
"github.com/coder/coder/v2/coderd/database/dbtime"
22-
"github.com/coder/coder/v2/coderd/notifications"
2322
"github.com/coder/coder/v2/coderd/rbac"
2423
"github.com/coder/coder/v2/coderd/schedule"
2524
"github.com/coder/coder/v2/coderd/util/ptr"
@@ -192,7 +191,7 @@ func TestPostTemplateByOrganization(t *testing.T) {
192191
var setCalled int64
193192
client := coderdtest.New(t, &coderdtest.Options{
194193
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
195-
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error) {
194+
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions) (database.Template, error) {
196195
atomic.AddInt64(&setCalled, 1)
197196
require.False(t, options.UserAutostartEnabled)
198197
require.False(t, options.UserAutostopEnabled)
@@ -269,7 +268,7 @@ func TestPostTemplateByOrganization(t *testing.T) {
269268
var setCalled int64
270269
client := coderdtest.New(t, &coderdtest.Options{
271270
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
272-
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error) {
271+
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions) (database.Template, error) {
273272
atomic.AddInt64(&setCalled, 1)
274273
assert.Zero(t, options.AutostopRequirement.DaysOfWeek)
275274
assert.Zero(t, options.AutostopRequirement.Weeks)
@@ -319,7 +318,7 @@ func TestPostTemplateByOrganization(t *testing.T) {
319318
var setCalled int64
320319
client := coderdtest.New(t, &coderdtest.Options{
321320
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
322-
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error) {
321+
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions) (database.Template, error) {
323322
atomic.AddInt64(&setCalled, 1)
324323
assert.EqualValues(t, 0b00110000, options.AutostopRequirement.DaysOfWeek)
325324
assert.EqualValues(t, 2, options.AutostopRequirement.Weeks)
@@ -759,7 +758,7 @@ func TestPatchTemplateMeta(t *testing.T) {
759758
var setCalled int64
760759
client := coderdtest.New(t, &coderdtest.Options{
761760
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
762-
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error) {
761+
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions) (database.Template, error) {
763762
if atomic.AddInt64(&setCalled, 1) == 2 {
764763
require.Equal(t, failureTTL, options.FailureTTL)
765764
require.Equal(t, inactivityTTL, options.TimeTilDormant)
@@ -854,7 +853,7 @@ func TestPatchTemplateMeta(t *testing.T) {
854853
allowAutostop.Store(true)
855854
client := coderdtest.New(t, &coderdtest.Options{
856855
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
857-
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error) {
856+
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions) (database.Template, error) {
858857
atomic.AddInt64(&setCalled, 1)
859858
assert.Equal(t, allowAutostart.Load(), options.UserAutostartEnabled)
860859
assert.Equal(t, allowAutostop.Load(), options.UserAutostopEnabled)
@@ -1024,7 +1023,7 @@ func TestPatchTemplateMeta(t *testing.T) {
10241023
var setCalled int64
10251024
client := coderdtest.New(t, &coderdtest.Options{
10261025
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
1027-
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error) {
1026+
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions) (database.Template, error) {
10281027
if atomic.AddInt64(&setCalled, 1) == 2 {
10291028
assert.EqualValues(t, 0b0110000, options.AutostopRequirement.DaysOfWeek)
10301029
assert.EqualValues(t, 2, options.AutostopRequirement.Weeks)
@@ -1095,7 +1094,7 @@ func TestPatchTemplateMeta(t *testing.T) {
10951094
var setCalled int64
10961095
client := coderdtest.New(t, &coderdtest.Options{
10971096
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
1098-
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error) {
1097+
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions) (database.Template, error) {
10991098
if atomic.AddInt64(&setCalled, 1) == 2 {
11001099
assert.EqualValues(t, 0, options.AutostopRequirement.DaysOfWeek)
11011100
assert.EqualValues(t, 1, options.AutostopRequirement.Weeks)

coderd/workspaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3547,7 +3547,7 @@ func TestNotifications(t *testing.T) {
35473547
require.Contains(t, notifyEnq.Sent[0].Targets, workspace.ID)
35483548
require.Contains(t, notifyEnq.Sent[0].Targets, workspace.OrganizationID)
35493549
require.Contains(t, notifyEnq.Sent[0].Targets, workspace.OwnerID)
3550-
require.Equal(t, notifyEnq.Sent[0].Labels["initiatedBy"], member.Username)
3550+
require.Equal(t, notifyEnq.Sent[0].Labels["initiator"], member.Username)
35513551
})
35523552

35533553
t.Run("InitiatorIsOwner", func(t *testing.T) {

enterprise/coderd/coderd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/prometheus/client_golang/prometheus"
2727

2828
"cdr.dev/slog"
29+
2930
"github.com/coder/coder/v2/coderd"
3031
agplaudit "github.com/coder/coder/v2/coderd/audit"
3132
agpldbauthz "github.com/coder/coder/v2/coderd/database/dbauthz"
@@ -632,7 +633,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
632633

633634
if initial, changed, enabled := featureChanged(codersdk.FeatureAdvancedTemplateScheduling); shouldUpdate(initial, changed, enabled) {
634635
if enabled {
635-
templateStore := schedule.NewEnterpriseTemplateScheduleStore(api.AGPL.UserQuietHoursScheduleStore)
636+
templateStore := schedule.NewEnterpriseTemplateScheduleStore(api.AGPL.UserQuietHoursScheduleStore, api.NotificationsEnqueuer, api.Logger.Named("template.schedule-store"))
636637
templateStoreInterface := agplschedule.TemplateScheduleStore(templateStore)
637638
api.AGPL.TemplateScheduleStore.Store(&templateStoreInterface)
638639

enterprise/coderd/schedule/template.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ type EnterpriseTemplateScheduleStore struct {
3333
// Custom time.Now() function to use in tests. Defaults to dbtime.Now().
3434
TimeNowFn func() time.Time
3535

36-
// NotificationsEnqueuer handles enqueueing notifications for delivery by SMTP, webhook, etc.
37-
NotificationsEnqueuer notifications.Enqueuer
36+
enqueuer notifications.Enqueuer
37+
logger slog.Logger
3838
}
3939

4040
var _ agpl.TemplateScheduleStore = &EnterpriseTemplateScheduleStore{}
4141

42-
func NewEnterpriseTemplateScheduleStore(userQuietHoursStore *atomic.Pointer[agpl.UserQuietHoursScheduleStore]) *EnterpriseTemplateScheduleStore {
42+
func NewEnterpriseTemplateScheduleStore(userQuietHoursStore *atomic.Pointer[agpl.UserQuietHoursScheduleStore], enqueuer notifications.Enqueuer, logger slog.Logger) *EnterpriseTemplateScheduleStore {
4343
return &EnterpriseTemplateScheduleStore{
4444
UserQuietHoursScheduleStore: userQuietHoursStore,
45+
enqueuer: enqueuer,
46+
logger: logger,
4547
}
4648
}
4749

@@ -97,7 +99,7 @@ func (*EnterpriseTemplateScheduleStore) Get(ctx context.Context, db database.Sto
9799
}
98100

99101
// Set implements agpl.TemplateScheduleStore.
100-
func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.Store, tpl database.Template, opts agpl.TemplateScheduleOptions, enqueuer notifications.Enqueuer, logger slog.Logger) (database.Template, error) {
102+
func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.Store, tpl database.Template, opts agpl.TemplateScheduleOptions) (database.Template, error) {
101103
ctx, span := tracing.StartSpan(ctx)
102104
defer span.End()
103105

@@ -206,7 +208,7 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
206208
for _, workspace := range dormantWorkspaces {
207209
_, err = dormancy.NotifyWorkspaceDormant(
208210
ctx,
209-
enqueuer,
211+
s.enqueuer,
210212
dormancy.WorkspaceDormantNotification{
211213
Workspace: workspace,
212214
Initiator: "system",
@@ -215,7 +217,7 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
215217
},
216218
)
217219
if err != nil {
218-
logger.Warn(ctx, "failed to notify of workspace marked as dormant", slog.Error(err))
220+
s.logger.Warn(ctx, "failed to notify of workspace marked as dormant", slog.Error(err))
219221
}
220222
}
221223

0 commit comments

Comments
 (0)