debian-mirror-gitlab/app/workers/chat_notification_worker.rb

51 lines
1.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2020-04-08 14:13:33 +05:30
class ChatNotificationWorker # rubocop:disable Scalability/IdempotentWorker
2019-07-07 11:18:12 +05:30
include ApplicationWorker
2020-03-13 15:44:24 +05:30
TimeoutExceeded = Class.new(StandardError)
sidekiq_options retry: false
2019-12-21 20:55:43 +05:30
feature_category :chatops
2020-04-08 14:13:33 +05:30
urgency :low # Can't be high as it has external dependencies
2020-03-13 15:44:24 +05:30
weight 2
2020-04-08 14:13:33 +05:30
worker_has_external_dependencies!
2019-12-21 20:55:43 +05:30
2019-07-07 11:18:12 +05:30
RESCHEDULE_INTERVAL = 2.seconds
2020-03-13 15:44:24 +05:30
RESCHEDULE_TIMEOUT = 5.minutes
2019-07-07 11:18:12 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2020-03-13 15:44:24 +05:30
def perform(build_id, reschedule_count = 0)
2019-07-07 11:18:12 +05:30
Ci::Build.find_by(id: build_id).try do |build|
send_response(build)
end
rescue Gitlab::Chat::Output::MissingBuildSectionError
2020-03-13 15:44:24 +05:30
raise TimeoutExceeded if timeout_exceeded?(reschedule_count)
2019-07-07 11:18:12 +05:30
# 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-03-13 15:44:24 +05:30
self.class.perform_in(RESCHEDULE_INTERVAL, build_id, reschedule_count + 1)
2019-07-07 11:18:12 +05:30
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
2020-03-13 15:44:24 +05:30
private
def timeout_exceeded?(reschedule_count)
(reschedule_count * RESCHEDULE_INTERVAL) >= RESCHEDULE_TIMEOUT
end
2019-07-07 11:18:12 +05:30
end