Skip to content

Commit 6ca7844

Browse files
feat: allow disabling notifications entirely
1 parent 27fa5bf commit 6ca7844

File tree

11 files changed

+89
-35
lines changed

11 files changed

+89
-35
lines changed

cli/server.go

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -897,31 +897,37 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
897897
}
898898

899899
// Manage notifications.
900-
cfg := options.DeploymentValues.Notifications
901-
metrics := notifications.NewMetrics(options.PrometheusRegistry)
902-
helpers := templateHelpers(options)
900+
var (
901+
cfg = options.DeploymentValues.Notifications
902+
notificationsManager *notifications.Manager
903+
)
903904

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

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

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

922-
// Run report generator to distribute periodic reports.
923-
notificationReportGenerator := reports.NewReportGenerator(ctx, logger.Named("notifications.report_generator"), options.Database, options.NotificationsEnqueuer, quartz.NewReal())
924-
defer notificationReportGenerator.Close()
927+
// Run report generator to distribute periodic reports.
928+
notificationReportGenerator := reports.NewReportGenerator(ctx, logger.Named("notifications.report_generator"), options.Database, options.NotificationsEnqueuer, quartz.NewReal())
929+
defer notificationReportGenerator.Close()
930+
}
925931

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

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

11141122
// Shut down provisioners before waiting for WebSockets

cli/testdata/coder_server_--help.golden

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Use a YAML configuration file when your server launch become unwieldy.
109109
EMAIL OPTIONS:
110110
Configure how emails are sent.
111111

112-
--email-force-tls bool, $CODER_EMAIL_FORCE_TLS
112+
--email-force-tls bool, $CODER_EMAIL_FORCE_TLS (default: false)
113113
Force a TLS connection to the configured SMTP smarthost.
114114

115115
--email-from string, $CODER_EMAIL_FROM
@@ -392,6 +392,9 @@ Configure how notifications are processed and delivered.
392392
--notifications-dispatch-timeout duration, $CODER_NOTIFICATIONS_DISPATCH_TIMEOUT (default: 1m0s)
393393
How long to wait while a notification is being sent before giving up.
394394

395+
--notifications-enabled bool, $CODER_NOTIFICATIONS_ENABLED (default: true)
396+
Controls if notifications are enabled.
397+
395398
--notifications-max-send-attempts int, $CODER_NOTIFICATIONS_MAX_SEND_ATTEMPTS (default: 5)
396399
The upper limit of attempts to send a notification.
397400

cli/testdata/server-config.yaml.golden

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,9 @@ userQuietHoursSchedule:
520520
allowWorkspaceRenames: false
521521
# Configure how notifications are processed and delivered.
522522
notifications:
523+
# Controls if notifications are enabled.
524+
# (default: true, type: bool)
525+
enabled: true
523526
# Which delivery method to use (available options: 'smtp', 'webhook').
524527
# (default: smtp, type: string)
525528
method: smtp
@@ -621,7 +624,7 @@ email:
621624
# (default: localhost, type: string)
622625
hello: localhost
623626
# Force a TLS connection to the configured SMTP smarthost.
624-
# (default: <unset>, type: bool)
627+
# (default: false, type: bool)
625628
forceTLS: false
626629
# Configure SMTP authentication options.
627630
emailAuth:

coderd/apidoc/docs.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codersdk/deployment.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ type HealthcheckConfig struct {
649649
}
650650

651651
type NotificationsConfig struct {
652+
Enabled serpent.Bool `json:"enabled" typescript:",notnull"`
653+
652654
// The upper limit of attempts to send a notification.
653655
MaxSendAttempts serpent.Int64 `json:"max_send_attempts" typescript:",notnull"`
654656
// The minimum time between retries.
@@ -1047,6 +1049,7 @@ when required by your organization's security policy.`,
10471049
Description: "Force a TLS connection to the configured SMTP smarthost.",
10481050
Flag: "email-force-tls",
10491051
Env: "CODER_EMAIL_FORCE_TLS",
1052+
Default: "false",
10501053
Value: &c.Notifications.SMTP.ForceTLS,
10511054
Group: &deploymentGroupEmail,
10521055
YAML: "forceTLS",
@@ -2580,6 +2583,16 @@ Write out the current server config as YAML to stdout.`,
25802583
Annotations: serpent.Annotations{}.Mark(annotationFormatDuration, "true"),
25812584
},
25822585
// Notifications Options
2586+
{
2587+
Name: "Notifications: Enabled",
2588+
Description: "Controls if notifications are enabled.",
2589+
Flag: "notifications-enabled",
2590+
Env: "CODER_NOTIFICATIONS_ENABLED",
2591+
Default: "true",
2592+
Value: &c.Notifications.Enabled,
2593+
Group: &deploymentGroupNotifications,
2594+
YAML: "enabled",
2595+
},
25832596
{
25842597
Name: "Notifications: Method",
25852598
Description: "Which delivery method to use (available options: 'smtp', 'webhook').",

docs/reference/api/general.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/api/schemas.md

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/cli/server.md

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/cli/testdata/coder_server_--help.golden

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Use a YAML configuration file when your server launch become unwieldy.
110110
EMAIL OPTIONS:
111111
Configure how emails are sent.
112112

113-
--email-force-tls bool, $CODER_EMAIL_FORCE_TLS
113+
--email-force-tls bool, $CODER_EMAIL_FORCE_TLS (default: false)
114114
Force a TLS connection to the configured SMTP smarthost.
115115

116116
--email-from string, $CODER_EMAIL_FROM
@@ -393,6 +393,9 @@ Configure how notifications are processed and delivered.
393393
--notifications-dispatch-timeout duration, $CODER_NOTIFICATIONS_DISPATCH_TIMEOUT (default: 1m0s)
394394
How long to wait while a notification is being sent before giving up.
395395

396+
--notifications-enabled bool, $CODER_NOTIFICATIONS_ENABLED (default: true)
397+
Controls if notifications are enabled.
398+
396399
--notifications-max-send-attempts int, $CODER_NOTIFICATIONS_MAX_SEND_ATTEMPTS (default: 5)
397400
The upper limit of attempts to send a notification.
398401

site/src/api/typesGenerated.ts

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)