Normalize default object types to file in the file download service

parent 84fee50e
...@@ -3,6 +3,7 @@ module Geo ...@@ -3,6 +3,7 @@ module Geo
attr_reader :object_type, :object_db_id attr_reader :object_type, :object_db_id
LEASE_TIMEOUT = 8.hours.freeze LEASE_TIMEOUT = 8.hours.freeze
DEFAULT_OBJECT_TYPES = [:attachment, :avatar, :file].freeze
def initialize(object_type, object_db_id) def initialize(object_type, object_db_id)
@object_type = object_type @object_type = object_type
...@@ -20,10 +21,22 @@ module Geo ...@@ -20,10 +21,22 @@ module Geo
private private
def downloader def downloader
klass = "Gitlab::Geo::#{object_type.to_s.camelize}Downloader".constantize klass = downloader_klass_name.constantize
klass.new(object_type, object_db_id) klass.new(object_type, object_db_id)
rescue NameError rescue NameError
Gitlab::Geo::FileDownloader.new(object_type, object_db_id) log("Unknown file type: #{object_type}")
raise
end
def downloader_klass_name
klass_name =
if DEFAULT_OBJECT_TYPES.include?(object_type.to_sym)
:file
else
object_type
end
"Gitlab::Geo::#{klass_name.to_s.camelize}Downloader"
end end
def try_obtain_lease def try_obtain_lease
...@@ -51,5 +64,9 @@ module Geo ...@@ -51,5 +64,9 @@ module Geo
def lease_key def lease_key
"file_download_service:#{object_type}:#{object_db_id}" "file_download_service:#{object_type}:#{object_db_id}"
end end
def log(message)
Rails.logger.info "#{self.class.name}: #{message}"
end
end end
end end
...@@ -57,6 +57,42 @@ describe Geo::FileDownloadService, services: true do ...@@ -57,6 +57,42 @@ describe Geo::FileDownloadService, services: true do
end end
end end
context 'with an attachment' do
let(:note) { create(:note, :with_attachment) }
let(:upload) { Upload.find_by(model: note, uploader: 'AttachmentUploader') }
subject { described_class.new(:attachment, upload.id) }
it 'downloads the attachment' do
allow_any_instance_of(Gitlab::ExclusiveLease)
.to receive(:try_obtain).and_return(true)
allow_any_instance_of(Gitlab::Geo::FileTransfer)
.to receive(:download_from_primary).and_return(100)
expect{ subject.execute }.to change { Geo::FileRegistry.count }.by(1)
end
end
context 'with file upload' do
let(:project) { create(:empty_project) }
let(:upload) { Upload.find_by(model: project, uploader: 'FileUploader') }
subject { described_class.new(:file, upload.id) }
before do
FileUploader.new(project).store!(fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png'))
end
it 'downloads the file' do
allow_any_instance_of(Gitlab::ExclusiveLease)
.to receive(:try_obtain).and_return(true)
allow_any_instance_of(Gitlab::Geo::FileTransfer)
.to receive(:download_from_primary).and_return(100)
expect{ subject.execute }.to change { Geo::FileRegistry.count }.by(1)
end
end
context 'LFS object' do context 'LFS object' do
let(:lfs_object) { create(:lfs_object) } let(:lfs_object) { create(:lfs_object) }
...@@ -73,8 +109,8 @@ describe Geo::FileDownloadService, services: true do ...@@ -73,8 +109,8 @@ describe Geo::FileDownloadService, services: true do
end end
context 'bad object type' do context 'bad object type' do
it 'does not track transfer' do it 'raises an error' do
expect{ described_class.new(:bad, 1).execute }.not_to change(Geo::FileRegistry, :count) expect{ described_class.new(:bad, 1).execute }.to raise_error(NameError)
end end
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