2019-02-20 16:29:48 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-02-19 13:09:10 -05:00
|
|
|
class ChatNotificationWorker # rubocop:disable Scalability/IdempotentWorker
|
2019-02-20 16:29:48 -05:00
|
|
|
include ApplicationWorker
|
|
|
|
|
2021-07-21 08:09:35 -04:00
|
|
|
data_consistency :always
|
|
|
|
|
2020-01-07 19:07:43 -05:00
|
|
|
TimeoutExceeded = Class.new(StandardError)
|
|
|
|
|
|
|
|
sidekiq_options retry: false
|
2019-10-18 07:11:44 -04:00
|
|
|
feature_category :chatops
|
2020-03-17 17:09:16 -04:00
|
|
|
urgency :low # Can't be high as it has external dependencies
|
2020-01-24 13:09:00 -05:00
|
|
|
weight 2
|
2020-03-17 17:09:16 -04:00
|
|
|
worker_has_external_dependencies!
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2019-02-20 16:29:48 -05:00
|
|
|
RESCHEDULE_INTERVAL = 2.seconds
|
2020-01-07 19:07:43 -05:00
|
|
|
RESCHEDULE_TIMEOUT = 5.minutes
|
2019-02-20 16:29:48 -05:00
|
|
|
|
2020-01-07 19:07:43 -05:00
|
|
|
def perform(build_id, reschedule_count = 0)
|
2021-11-10 10:13:21 -05:00
|
|
|
Ci::Build.find_by_id(build_id).try do |build|
|
2019-02-20 16:29:48 -05:00
|
|
|
send_response(build)
|
|
|
|
end
|
|
|
|
rescue Gitlab::Chat::Output::MissingBuildSectionError
|
2020-01-07 19:07:43 -05:00
|
|
|
raise TimeoutExceeded if timeout_exceeded?(reschedule_count)
|
|
|
|
|
2019-02-20 16:29:48 -05:00
|
|
|
# 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.
|
2020-01-07 19:07:43 -05:00
|
|
|
self.class.perform_in(RESCHEDULE_INTERVAL, build_id, reschedule_count + 1)
|
2019-02-20 16:29:48 -05:00
|
|
|
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
|
2020-01-07 19:07:43 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def timeout_exceeded?(reschedule_count)
|
|
|
|
(reschedule_count * RESCHEDULE_INTERVAL) >= RESCHEDULE_TIMEOUT
|
|
|
|
end
|
2019-02-20 16:29:48 -05:00
|
|
|
end
|