1# frozen_string_literal: true
2
3class ImportExportCleanUpService
4  LAST_MODIFIED_TIME_IN_MINUTES = 1440
5  DIR_DEPTH = 5
6
7  attr_reader :mmin, :path
8
9  def initialize(mmin = LAST_MODIFIED_TIME_IN_MINUTES)
10    @mmin = mmin
11    @path = Gitlab::ImportExport.storage_path
12  end
13
14  def execute
15    Gitlab::Metrics.measure(:import_export_clean_up) do
16      execute_cleanup
17    end
18  end
19
20  private
21
22  def execute_cleanup
23    clean_up_export_object_files
24  ensure
25    # We don't want a failure in cleaning up object storage from
26    # blocking us from cleaning up temporary storage.
27    clean_up_export_files if File.directory?(path)
28  end
29
30  def clean_up_export_files
31    old_directories do |dir|
32      FileUtils.remove_entry(dir)
33
34      logger.info(
35        message: 'Removed Import/Export tmp directory',
36        dir_path: dir
37      )
38    end
39  end
40
41  def clean_up_export_object_files
42    ImportExportUpload.with_export_file.updated_before(mmin.minutes.ago).each do |upload|
43      upload.remove_export_file!
44      upload.save!
45
46      logger.info(
47        message: 'Removed Import/Export export_file',
48        project_id: upload.project_id,
49        group_id: upload.group_id
50      )
51    end
52  end
53
54  def old_directories
55    IO.popen(directories_cmd) do |find|
56      find.each_line(chomp: true) do |directory|
57        yield directory
58      end
59    end
60  end
61
62  def directories_cmd
63    %W(find #{path} -mindepth #{DIR_DEPTH} -maxdepth #{DIR_DEPTH} -type d -not -path #{path} -mmin +#{mmin})
64  end
65
66  def logger
67    @logger ||= Gitlab::Import::Logger.build
68  end
69end
70