class AlertNotifier
Sends an alert to a specific destination: a Slack #channel and/or email recipients, using the global transport (Slack webhook + SMTP) from AlertSetting. Callers pass their own destination so alerts go exactly where they are configured to go.
Public Class Methods
# File app/services/alert_notifier.rb, line 27 def initialize(subject:, message:, severity:, slack_channel: nil, email_to: nil, context: {}) @subject = subject @message = message @severity = severity.to_s @slack_channel = slack_channel @email_to = email_to @context = context end
Creates the notifier for an alert.
@param subject [String] the alert subject @param message [String] the alert message @param severity [String] the alert severity @param slack_channel [String, nil] a Slack #channel to post to @param email_to [String, nil] comma-separated email recipients @param context [Hash] extra keys for message templates
# File app/services/alert_notifier.rb, line 15 def self.notify(subject:, message:, severity:, slack_channel: nil, email_to: nil, context: {}) new(subject:, message:, severity:, slack_channel:, email_to:, context:).call end
Sends an alert to the given destinations.
@param subject [String] the alert subject (short, e.g. โEngine downโ) @param message [String] the alert message (rich in context) @param severity [String] the alert severity (warning/severe/critical) @param slack_channel [String, nil] a Slack #channel to post to @param email_to [String, nil] comma-separated email recipients @param context [Hash] extra keys for message templates @return [String, nil] an error message when every transport failed
Public Instance Methods
Source
# File app/services/alert_notifier.rb, line 39 def call errors = [] errors << send_slack if @slack_channel.present? errors << send_email if @email_to.present? errors.compact.first end
Delivers over Slack and/or Email and returns the first error, if any.
@return [String, nil] an error message when a transport failed