Skip to content

feat!: allow disabling notifications #15509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5c36c93
fix: remove defaults for CODER_EMAIL_ options
DanielleMaywood Nov 12, 2024
196f538
fix: failing tests
DanielleMaywood Nov 12, 2024
b670009
fix: tests (again)
DanielleMaywood Nov 12, 2024
19398cc
fix: run 'make gen'
DanielleMaywood Nov 12, 2024
4175bc8
fix: consistency
DanielleMaywood Nov 13, 2024
66d1a69
nit: trim smarthost
DanielleMaywood Nov 13, 2024
4689e5b
nit: re-add default for CODER_EMAIL_HELLO
DanielleMaywood Nov 13, 2024
01d4957
fix: run 'make {update-golden-files,gen}'
DanielleMaywood Nov 13, 2024
27fa5bf
fix: run 'make fmt'
DanielleMaywood Nov 13, 2024
6ca7844
feat: allow disabling notifications entirely
DanielleMaywood Nov 13, 2024
fb54e45
Merge branch 'main' into dm-fix-defaults-notifications
DanielleMaywood Nov 14, 2024
d5aff28
chore: re-add default for smarthost
DanielleMaywood Nov 14, 2024
01cb999
fix: remove default for deprecated option
DanielleMaywood Nov 14, 2024
c513113
chore: revert change
DanielleMaywood Nov 14, 2024
a8b5a61
chore: remove unrelated test changes
DanielleMaywood Nov 14, 2024
1711e68
revert: more changes
DanielleMaywood Nov 14, 2024
2e03ed1
chore: simplify description
DanielleMaywood Nov 14, 2024
28ec1b7
Merge branch 'main' into dm-fix-defaults-notifications
DanielleMaywood Nov 14, 2024
7ba3a6c
chore: make {gen,update-golden-files}
DanielleMaywood Nov 14, 2024
4a72e4f
chore: rename cfg to notificationsCfg
DanielleMaywood Nov 14, 2024
9c02842
chore: drop enabled flag
DanielleMaywood Nov 18, 2024
dd527f9
fix: invalid test
DanielleMaywood Nov 18, 2024
77d1803
chore: improve info message and add to docs
DanielleMaywood Nov 19, 2024
5fd03b3
test: notifications.Enabled()
DanielleMaywood Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 40 additions & 32 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,31 +897,37 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
}

// Manage notifications.
cfg := options.DeploymentValues.Notifications
metrics := notifications.NewMetrics(options.PrometheusRegistry)
helpers := templateHelpers(options)
var (
cfg = options.DeploymentValues.Notifications
notificationsManager *notifications.Manager
)

// The enqueuer is responsible for enqueueing notifications to the given store.
enqueuer, err := notifications.NewStoreEnqueuer(cfg, options.Database, helpers, logger.Named("notifications.enqueuer"), quartz.NewReal())
if err != nil {
return xerrors.Errorf("failed to instantiate notification store enqueuer: %w", err)
}
options.NotificationsEnqueuer = enqueuer
if cfg.Enabled {
metrics := notifications.NewMetrics(options.PrometheusRegistry)
helpers := templateHelpers(options)

// The notification manager is responsible for:
// - creating notifiers and managing their lifecycles (notifiers are responsible for dequeueing/sending notifications)
// - keeping the store updated with status updates
notificationsManager, err := notifications.NewManager(cfg, options.Database, helpers, metrics, logger.Named("notifications.manager"))
if err != nil {
return xerrors.Errorf("failed to instantiate notification manager: %w", err)
}
// The enqueuer is responsible for enqueueing notifications to the given store.
enqueuer, err := notifications.NewStoreEnqueuer(cfg, options.Database, helpers, logger.Named("notifications.enqueuer"), quartz.NewReal())
if err != nil {
return xerrors.Errorf("failed to instantiate notification store enqueuer: %w", err)
}
options.NotificationsEnqueuer = enqueuer

// nolint:gocritic // We need to run the manager in a notifier context.
notificationsManager.Run(dbauthz.AsNotifier(ctx))
// The notification manager is responsible for:
// - creating notifiers and managing their lifecycles (notifiers are responsible for dequeueing/sending notifications)
// - keeping the store updated with status updates
notificationsManager, err = notifications.NewManager(cfg, options.Database, helpers, metrics, logger.Named("notifications.manager"))
if err != nil {
return xerrors.Errorf("failed to instantiate notification manager: %w", err)
}

// nolint:gocritic // We need to run the manager in a notifier context.
notificationsManager.Run(dbauthz.AsNotifier(ctx))

// Run report generator to distribute periodic reports.
notificationReportGenerator := reports.NewReportGenerator(ctx, logger.Named("notifications.report_generator"), options.Database, options.NotificationsEnqueuer, quartz.NewReal())
defer notificationReportGenerator.Close()
// Run report generator to distribute periodic reports.
notificationReportGenerator := reports.NewReportGenerator(ctx, logger.Named("notifications.report_generator"), options.Database, options.NotificationsEnqueuer, quartz.NewReal())
defer notificationReportGenerator.Close()
}

// Since errCh only has one buffered slot, all routines
// sending on it must be wrapped in a select/default to
Expand Down Expand Up @@ -1098,17 +1104,19 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
// Cancel any remaining in-flight requests.
shutdownConns()

// Stop the notification manager, which will cause any buffered updates to the store to be flushed.
// If the Stop() call times out, messages that were sent but not reflected as such in the store will have
// their leases expire after a period of time and will be re-queued for sending.
// See CODER_NOTIFICATIONS_LEASE_PERIOD.
cliui.Info(inv.Stdout, "Shutting down notifications manager..."+"\n")
err = shutdownWithTimeout(notificationsManager.Stop, 5*time.Second)
if err != nil {
cliui.Warnf(inv.Stderr, "Notifications manager shutdown took longer than 5s, "+
"this may result in duplicate notifications being sent: %s\n", err)
} else {
cliui.Info(inv.Stdout, "Gracefully shut down notifications manager\n")
if notificationsManager != nil {
// Stop the notification manager, which will cause any buffered updates to the store to be flushed.
// If the Stop() call times out, messages that were sent but not reflected as such in the store will have
// their leases expire after a period of time and will be re-queued for sending.
// See CODER_NOTIFICATIONS_LEASE_PERIOD.
cliui.Info(inv.Stdout, "Shutting down notifications manager..."+"\n")
err = shutdownWithTimeout(notificationsManager.Stop, 5*time.Second)
if err != nil {
cliui.Warnf(inv.Stderr, "Notifications manager shutdown took longer than 5s, "+
"this may result in duplicate notifications being sent: %s\n", err)
} else {
cliui.Info(inv.Stdout, "Gracefully shut down notifications manager\n")
}
}

// Shut down provisioners before waiting for WebSockets
Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ Configure how notifications are processed and delivered.
--notifications-dispatch-timeout duration, $CODER_NOTIFICATIONS_DISPATCH_TIMEOUT (default: 1m0s)
How long to wait while a notification is being sent before giving up.

--notifications-enabled bool, $CODER_NOTIFICATIONS_ENABLED (default: true)
Controls if notifications are enabled.

--notifications-max-send-attempts int, $CODER_NOTIFICATIONS_MAX_SEND_ATTEMPTS (default: 5)
The upper limit of attempts to send a notification.

Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/server-config.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@ email:
certKeyFile: ""
# Configure how notifications are processed and delivered.
notifications:
# Controls if notifications are enabled.
# (default: true, type: bool)
enabled: true
# Which delivery method to use (available options: 'smtp', 'webhook').
# (default: smtp, type: string)
method: smtp
Expand Down
3 changes: 3 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions codersdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ type HealthcheckConfig struct {
}

type NotificationsConfig struct {
Enabled serpent.Bool `json:"enabled" typescript:",notnull"`

// The upper limit of attempts to send a notification.
MaxSendAttempts serpent.Int64 `json:"max_send_attempts" typescript:",notnull"`
// The minimum time between retries.
Expand Down Expand Up @@ -2611,6 +2613,16 @@ Write out the current server config as YAML to stdout.`,
emailTLSCertFile,
emailTLSCertKeyFile,
// Notifications Options
{
Name: "Notifications: Enabled",
Description: "Controls if notifications are enabled.",
Flag: "notifications-enabled",
Env: "CODER_NOTIFICATIONS_ENABLED",
Default: "true",
Value: &c.Notifications.Enabled,
Group: &deploymentGroupNotifications,
YAML: "enabled",
},
{
Name: "Notifications: Method",
Description: "Which delivery method to use (available options: 'smtp', 'webhook').",
Expand Down
1 change: 1 addition & 0 deletions docs/reference/api/general.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions docs/reference/api/schemas.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions docs/reference/cli/server.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions enterprise/cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ Configure how notifications are processed and delivered.
--notifications-dispatch-timeout duration, $CODER_NOTIFICATIONS_DISPATCH_TIMEOUT (default: 1m0s)
How long to wait while a notification is being sent before giving up.

--notifications-enabled bool, $CODER_NOTIFICATIONS_ENABLED (default: true)
Controls if notifications are enabled.

--notifications-max-send-attempts int, $CODER_NOTIFICATIONS_MAX_SEND_ATTEMPTS (default: 5)
The upper limit of attempts to send a notification.

Expand Down
1 change: 1 addition & 0 deletions site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading