File: chat_notification_worker.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (50 lines) | stat: -rw-r--r-- 1,472 bytes parent folder | download
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
48
49
50
# frozen_string_literal: true

class ChatNotificationWorker # rubocop:disable Scalability/IdempotentWorker
  include ApplicationWorker

  data_consistency :sticky

  TimeoutExceeded = Class.new(StandardError)

  sidekiq_options retry: false
  feature_category :integrations
  urgency :low # Can't be high as it has external dependencies
  weight 2
  worker_has_external_dependencies!

  RESCHEDULE_INTERVAL = 2.seconds
  RESCHEDULE_TIMEOUT = 5.minutes

  def perform(build_id, reschedule_count = 0)
    Ci::Build.find_by_id(build_id).try do |build|
      send_response(build)
    end
  rescue Gitlab::Chat::Output::MissingBuildSectionError
    raise TimeoutExceeded if timeout_exceeded?(reschedule_count)

    # 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, reschedule_count + 1)
  end

  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

  private

  def timeout_exceeded?(reschedule_count)
    (reschedule_count * RESCHEDULE_INTERVAL) >= RESCHEDULE_TIMEOUT
  end
end