summaryrefslogtreecommitdiff
path: root/app/workers/incident_management/process_alert_worker_v2.rb
blob: 04bf6970578be5b7c5e2c147c865cd7cb794b1e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

module IncidentManagement
  class ProcessAlertWorkerV2 # rubocop:disable Scalability/IdempotentWorker
    include ApplicationWorker

    queue_namespace :incident_management
    feature_category :incident_management

    idempotent!

    def perform(alert_id)
      return unless alert_id

      alert = find_alert(alert_id)
      return unless alert

      result = create_issue_for(alert)
      return if result.success?

      log_warning(alert, result)
    end

    private

    def find_alert(alert_id)
      AlertManagement::Alert.find_by_id(alert_id)
    end

    def create_issue_for(alert)
      AlertManagement::CreateAlertIssueService
        .new(alert, User.alert_bot)
        .execute
    end

    def log_warning(alert, result)
      issue_id = result.payload[:issue]&.id

      Gitlab::AppLogger.warn(
        message: 'Cannot process an Incident',
        issue_id: issue_id,
        alert_id: alert.id,
        errors: result.message
      )
    end
  end
end