Commit b7c83960 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Extract `BaseDownloader` and use it on other classes

`BaseDownloader` is now the base class for `FileDownloader`,
`JobArtifactDownloader`, and `LfsDownloader`
parent cc2fd3c7
# frozen_string_literal: true
module Gitlab
module Geo
module Replication
class BaseDownloader
attr_reader :object_type, :object_db_id
def initialize(object_type, object_db_id)
@object_type = object_type
@object_db_id = object_db_id
end
class Result
attr_reader :success, :bytes_downloaded, :primary_missing_file, :failed_before_transfer
def self.from_transfer_result(transfer_result)
Result.new(success: transfer_result.success,
primary_missing_file: transfer_result.primary_missing_file,
bytes_downloaded: transfer_result.bytes_downloaded)
end
def initialize(success:, bytes_downloaded:, primary_missing_file: false, failed_before_transfer: false)
@success = success
@bytes_downloaded = bytes_downloaded
@primary_missing_file = primary_missing_file
@failed_before_transfer = failed_before_transfer
end
end
private
def fail_before_transfer
Result.new(success: false, bytes_downloaded: 0, failed_before_transfer: true)
end
def missing_on_primary
Result.new(success: true, bytes_downloaded: 0, primary_missing_file: true)
end
end
end
end
end
...@@ -8,23 +8,13 @@ module Gitlab ...@@ -8,23 +8,13 @@ module Gitlab
# * Requesting and downloading the Upload's file from the primary # * Requesting and downloading the Upload's file from the primary
# * Returning a detailed Result # * Returning a detailed Result
# #
# TODO: Rearrange things so this class not inherited by JobArtifactDownloader and LfsDownloader class FileDownloader < BaseDownloader
# Maybe rename it so it doesn't seem generic. It only works with Upload records.
class FileDownloader
attr_reader :object_type, :object_db_id
def initialize(object_type, object_db_id)
@object_type = object_type
@object_db_id = object_db_id
end
# Executes the actual file download # Executes the actual file download
# #
# Subclasses should return the number of bytes downloaded, # Subclasses should return the number of bytes downloaded,
# or nil or -1 if a failure occurred. # or nil or -1 if a failure occurred.
# rubocop: disable CodeReuse/ActiveRecord
def execute def execute
upload = Upload.find_by(id: object_db_id) upload = find_resource
return fail_before_transfer unless upload.present? return fail_before_transfer unless upload.present?
return missing_on_primary if upload.model.nil? return missing_on_primary if upload.model.nil?
...@@ -32,34 +22,15 @@ module Gitlab ...@@ -32,34 +22,15 @@ module Gitlab
Result.from_transfer_result(transfer.download_from_primary) Result.from_transfer_result(transfer.download_from_primary)
end end
# rubocop: enable CodeReuse/ActiveRecord
class Result
attr_reader :success, :bytes_downloaded, :primary_missing_file, :failed_before_transfer
def self.from_transfer_result(transfer_result)
Result.new(success: transfer_result.success,
primary_missing_file: transfer_result.primary_missing_file,
bytes_downloaded: transfer_result.bytes_downloaded)
end
def initialize(success:, bytes_downloaded:, primary_missing_file: false, failed_before_transfer: false)
@success = success
@bytes_downloaded = bytes_downloaded
@primary_missing_file = primary_missing_file
@failed_before_transfer = failed_before_transfer
end
end
private private
def fail_before_transfer # rubocop: disable CodeReuse/ActiveRecord
Result.new(success: false, bytes_downloaded: 0, failed_before_transfer: true)
end
def missing_on_primary def find_resource
Result.new(success: true, bytes_downloaded: 0, primary_missing_file: true) Upload.find_by(id: object_db_id)
end end
# rubocop: enable CodeReuse/ActiveRecord
end end
end end
end end
......
...@@ -8,16 +8,23 @@ module Gitlab ...@@ -8,16 +8,23 @@ module Gitlab
# * Requesting and downloading the JobArtifact's file from the primary # * Requesting and downloading the JobArtifact's file from the primary
# * Returning a detailed Result # * Returning a detailed Result
# #
# TODO: Rearrange things so this class does not inherit FileDownloader class JobArtifactDownloader < BaseDownloader
class JobArtifactDownloader < FileDownloader
# rubocop: disable CodeReuse/ActiveRecord
def execute def execute
job_artifact = ::Ci::JobArtifact.find_by(id: object_db_id) job_artifact = find_resource
return fail_before_transfer unless job_artifact.present? return fail_before_transfer unless job_artifact.present?
transfer = ::Gitlab::Geo::Replication::JobArtifactTransfer.new(job_artifact) transfer = ::Gitlab::Geo::Replication::JobArtifactTransfer.new(job_artifact)
Result.from_transfer_result(transfer.download_from_primary) Result.from_transfer_result(transfer.download_from_primary)
end end
private
# rubocop: disable CodeReuse/ActiveRecord
def find_resource
::Ci::JobArtifact.find_by(id: object_db_id)
end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
end end
end end
......
...@@ -8,16 +8,23 @@ module Gitlab ...@@ -8,16 +8,23 @@ module Gitlab
# * Requesting and downloading the LfsObject's file from the primary # * Requesting and downloading the LfsObject's file from the primary
# * Returning a detailed Result # * Returning a detailed Result
# #
# TODO: Rearrange things so this class does not inherit FileDownloader class LfsDownloader < BaseDownloader
class LfsDownloader < FileDownloader
# rubocop: disable CodeReuse/ActiveRecord
def execute def execute
lfs_object = LfsObject.find_by(id: object_db_id) lfs_object = find_resource
return fail_before_transfer unless lfs_object.present? return fail_before_transfer unless lfs_object.present?
transfer = ::Gitlab::Geo::Replication::LfsTransfer.new(lfs_object) transfer = ::Gitlab::Geo::Replication::LfsTransfer.new(lfs_object)
Result.from_transfer_result(transfer.download_from_primary) Result.from_transfer_result(transfer.download_from_primary)
end end
private
# rubocop: disable CodeReuse/ActiveRecord
def find_resource
LfsObject.find_by(id: object_db_id)
end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment