Extract an abstract class FileService

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