2018-07-05 10:18:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-02 09:32:28 +00:00
|
|
|
class ImportExportCleanUpService
|
|
|
|
LAST_MODIFIED_TIME_IN_MINUTES = 1440
|
|
|
|
|
|
|
|
attr_reader :mmin, :path
|
|
|
|
|
|
|
|
def initialize(mmin = LAST_MODIFIED_TIME_IN_MINUTES)
|
|
|
|
@mmin = mmin
|
|
|
|
@path = Gitlab::ImportExport.storage_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
Gitlab::Metrics.measure(:import_export_clean_up) do
|
2019-09-19 03:06:10 +00:00
|
|
|
execute_cleanup
|
2016-08-02 09:32:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-09-19 03:06:10 +00:00
|
|
|
def execute_cleanup
|
|
|
|
clean_up_export_object_files
|
|
|
|
ensure
|
|
|
|
# We don't want a failure in cleaning up object storage from
|
|
|
|
# blocking us from cleaning up temporary storage.
|
|
|
|
clean_up_export_files if File.directory?(path)
|
|
|
|
end
|
|
|
|
|
2016-08-02 09:32:28 +00:00
|
|
|
def clean_up_export_files
|
|
|
|
Gitlab::Popen.popen(%W(find #{path} -not -path #{path} -mmin +#{mmin} -delete))
|
|
|
|
end
|
2018-06-25 13:10:26 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-06-25 13:10:26 +00:00
|
|
|
def clean_up_export_object_files
|
|
|
|
ImportExportUpload.where('updated_at < ?', mmin.minutes.ago).each do |upload|
|
|
|
|
upload.remove_export_file!
|
|
|
|
upload.save!
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-08-02 09:32:28 +00:00
|
|
|
end
|