Extract an abstract class FileService

parent 317a6ada
module Geo
class FileDownloadService
attr_reader :object_type, :object_db_id
class FileDownloadService < FileService
LEASE_TIMEOUT = 8.hours.freeze
DEFAULT_OBJECT_TYPES = %w[attachment avatar file].freeze
def initialize(object_type, object_db_id)
@object_type = object_type.to_s
@object_db_id = object_db_id
end
def execute
try_obtain_lease do |lease|
......@@ -21,24 +13,13 @@ module Geo
private
def downloader
klass = downloader_klass_name.constantize
klass = "Gitlab::Geo::#{service_klass_name}Downloader".constantize
klass.new(object_type, object_db_id)
rescue NameError
log("Unknown file type: #{object_type}")
raise
end
def downloader_klass_name
klass_name =
if DEFAULT_OBJECT_TYPES.include?(object_type)
'file'
else
object_type
end
"Gitlab::Geo::#{klass_name.camelize}Downloader"
end
def try_obtain_lease
uuid = Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT).try_obtain
......@@ -64,9 +45,5 @@ module Geo
def lease_key
"file_download_service:#{object_type}:#{object_db_id}"
end
def log(message)
Rails.logger.info "#{self.class.name}: #{message}"
end
end
end
module Geo
class FileService
attr_reader :object_type, :object_db_id
DEFAULT_OBJECT_TYPES = %w[attachment avatar file].freeze
DEFAULT_SERVICE_TYPE = 'file'.freeze
def initialize(object_type, object_db_id)
@object_type = object_type.to_s
@object_db_id = object_db_id
end
def execute
raise NotImplementedError
end
private
def service_klass_name
klass_name =
if DEFAULT_OBJECT_TYPES.include?(object_type)
DEFAULT_SERVICE_TYPE
else
object_type
end
klass_name.camelize
end
def log(message)
Rails.logger.info "#{self.class.name}: #{message}"
end
end
end
module Geo
class FileUploadService
DEFAULT_OBJECT_TYPES = %w[attachment avatar file].freeze
class FileUploadService < FileService
IAT_LEEWAY = 60.seconds.to_i
attr_reader :object_type, :object_db_id, :auth_header
attr_reader :auth_header
def initialize(params, auth_header)
@object_type = params[:type]
@object_db_id = params[:id]
super(params[:type], params[:id])
@auth_header = auth_header
end
......@@ -22,25 +20,10 @@ module Geo
private
def uploader_klass
uploader_klass_name.constantize
"Gitlab::Geo::#{service_klass_name}Uploader".constantize
rescue NameError
log("Unknown file type: #{object_type}")
raise
end
def uploader_klass_name
klass_name =
if DEFAULT_OBJECT_TYPES.include?(object_type)
'file'
else
object_type
end
"Gitlab::Geo::#{klass_name.camelize}Uploader"
end
def log(message)
Rails.logger.info "#{self.class.name}: #{message}"
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