Using SlackService to Send a Job Alert - department-of-veterans-affairs/caseflow GitHub Wiki

Using the SlackService to Send a Job Alert

The SlackService (slack_service.rb) is designed to send notifications to Slack channels based on the environment (e.g., development, test, production). This service helps in monitoring and alerting by sending messages about various events, errors, and statuses.

Configuration:

The service is configured to use a Slack webhook URL, set through the environment variable SLACK_DISPATCH_ALERT_URL. The default Slack channel is determined based on the environment:

  • prod: #appeals-job-alerts
  • prodtest: #appeals-prodtest-alerts
  • uat: #appeals-uat-alerts
  • local/demo: Slack integration is not currently set up for local/demo alerts. For these environments, the slack message pretty prints in Rails logs

Method:

send_notification(msg, title = "", channel = DEFAULT_CHANNEL)

Parameters:

  • msg: The main content of the message
  • title: Optional title of the message
  • channel: Optional Slack channel to override the default

Usage:

slack_service.send_notification("Job completed successfully", "Job Status")

Example:

class ExampleJob < ApplicationJob
  def perform
    begin
      # Job logic...
      SlackService.new.send_notification("Job completed successfully", self.class.name)
    rescue StandardError => error
      SlackService.new.send_notification("Job failed with error: #{error.message}", "Error in #{self.class.name}")
    end
  end
end

Relevant links:

slack_service.rb