summaryrefslogtreecommitdiff
path: root/app/workers/chat_notification_worker.rb
blob: 42a23cd472a9a06efcdd9a75f7aa351f489d47da (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
# frozen_string_literal: true

class ChatNotificationWorker
  include ApplicationWorker

  feature_category :chatops
  latency_sensitive_worker!
  # TODO: break this into multiple jobs
  # as the `responder` uses external dependencies
  # See https://gitlab.com/gitlab-com/gl-infra/scalability/issues/34
  # worker_has_external_dependencies!

  RESCHEDULE_INTERVAL = 2.seconds

  # rubocop: disable CodeReuse/ActiveRecord
  def perform(build_id)
    Ci::Build.find_by(id: build_id).try do |build|
      send_response(build)
    end
  rescue Gitlab::Chat::Output::MissingBuildSectionError
    # The creation of traces and sections appears to be eventually consistent.
    # As a result it's possible for us to run the above code before the trace
    # sections are present. To better handle such cases we'll just reschedule
    # the job instead of producing an error.
    self.class.perform_in(RESCHEDULE_INTERVAL, build_id)
  end
  # rubocop: enable CodeReuse/ActiveRecord

  def send_response(build)
    Gitlab::Chat::Responder.responder_for(build).try do |responder|
      if build.success?
        output = Gitlab::Chat::Output.new(build)

        responder.success(output.to_s)
      else
        responder.failure
      end
    end
  end
end