2021-09-04 01:27:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
class ExportStatus
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
def initialize(pipeline_tracker, relation)
|
|
|
|
@pipeline_tracker = pipeline_tracker
|
|
|
|
@relation = relation
|
|
|
|
@entity = @pipeline_tracker.entity
|
|
|
|
@configuration = @entity.bulk_import.configuration
|
2021-09-30 23:02:18 +05:30
|
|
|
@client = Clients::HTTP.new(url: @configuration.url, token: @configuration.access_token)
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
2022-07-23 23:45:48 +05:30
|
|
|
!empty? && export_status['status'] == Export::STARTED
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def failed?
|
2022-07-23 23:45:48 +05:30
|
|
|
!empty? && export_status['status'] == Export::FAILED
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
export_status.nil?
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def error
|
|
|
|
export_status['error']
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
attr_reader :client, :entity, :relation, :pipeline_tracker
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
def export_status
|
|
|
|
strong_memoize(:export_status) do
|
2022-07-23 23:45:48 +05:30
|
|
|
fetch_export_status&.find { |item| item['relation'] == relation }
|
2022-11-25 23:54:43 +05:30
|
|
|
rescue BulkImports::NetworkError => e
|
|
|
|
raise BulkImports::RetryPipelineError.new(e.message, 2.seconds) if e.retriable?(pipeline_tracker)
|
|
|
|
|
|
|
|
default_error_response(e.message)
|
|
|
|
rescue StandardError => e
|
|
|
|
default_error_response(e.message)
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_export_status
|
|
|
|
client.get(status_endpoint).parsed_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_endpoint
|
2021-11-18 22:05:49 +05:30
|
|
|
File.join(entity.export_relations_url_path, 'status')
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
def default_error_response(message)
|
|
|
|
{ 'status' => Export::FAILED, 'error' => message }
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|