prepare_untracked_uploads.rb 3.31 KB
Newer Older
1
2
module Gitlab
  module BackgroundMigration
3
    class PrepareUntrackedUploads
4
5
6
      # For bulk_queue_background_migration_jobs_by_range
      include Database::MigrationHelpers

7
      FILE_PATH_BATCH_SIZE = 500
8
9
      RELATIVE_UPLOAD_DIR = "uploads".freeze
      ABSOLUTE_UPLOAD_DIR = "#{CarrierWave.root}/#{RELATIVE_UPLOAD_DIR}".freeze
Michael Kozono's avatar
Michael Kozono committed
10
      FOLLOW_UP_MIGRATION = 'PopulateUntrackedUploads'.freeze
11
12
13
      START_WITH_CARRIERWAVE_ROOT_REGEX = %r{\A#{CarrierWave.root}/}
      EXCLUDED_HASHED_UPLOADS_PATH = "#{ABSOLUTE_UPLOAD_DIR}/@hashed/*".freeze
      EXCLUDED_TMP_UPLOADS_PATH = "#{ABSOLUTE_UPLOAD_DIR}/tmp/*".freeze
14

15
      class UntrackedFile < ActiveRecord::Base
16
17
        include EachBatch

18
        self.table_name = 'untracked_files_for_uploads'
19
20
21
22
23
      end

      def perform
        return unless migrate?

24
        store_untracked_file_paths
25
26
27
28
29
30
        schedule_populate_untracked_uploads_jobs
      end

      private

      def migrate?
31
        UntrackedFile.table_exists?
32
33
      end

34
      def store_untracked_file_paths
35
        return unless Dir.exist?(ABSOLUTE_UPLOAD_DIR)
36

37
        each_file_batch(ABSOLUTE_UPLOAD_DIR, FILE_PATH_BATCH_SIZE) do |file_paths|
38
          insert_file_paths(file_paths)
39
40
41
        end
      end

42
      def each_file_batch(search_dir, batch_size, &block)
43
        cmd = build_find_command(search_dir)
44

45
        Open3.popen2(*cmd) do |stdin, stdout, status_thread|
46
47
          yield_paths_in_batches(stdout, batch_size, &block)

48
49
50
51
          raise "Find command failed" unless status_thread.value.success?
        end
      end

52
53
54
55
      def yield_paths_in_batches(stdout, batch_size, &block)
        paths = []

        stdout.each_line("\0") do |line|
56
          paths << line.chomp("\0").sub(START_WITH_CARRIERWAVE_ROOT_REGEX, '')
57
58
59
60
61
62
63
64
65
66

          if paths.size >= batch_size
            yield(paths)
            paths = []
          end
        end

        yield(paths)
      end

67
      def build_find_command(search_dir)
68
        cmd = %W[find #{search_dir} -type f ! ( -path #{EXCLUDED_HASHED_UPLOADS_PATH} -prune ) ! ( -path #{EXCLUDED_TMP_UPLOADS_PATH} -prune ) -print0]
69

Michael Kozono's avatar
Michael Kozono committed
70
        cmd = %w[ionice -c Idle] + cmd if ionice_is_available?
71
72
73
74
75
76
77
78
79
80
81
82

        cmd
      end

      def ionice_is_available?
        Gitlab::Utils.which('ionice')
      rescue StandardError
        # In this case, returning false is relatively safe, even though it isn't very nice
        false
      end

      def insert_file_paths(file_paths)
Michael Kozono's avatar
Michael Kozono committed
83
84
85
86
        ActiveRecord::Base.transaction do
          file_paths.each do |file_path|
            insert_file_path(file_path)
          end
87
        end
88
89
      end

90
      def insert_file_path(file_path)
91
        table_columns_and_values = 'untracked_files_for_uploads (path, created_at, updated_at) VALUES (?, ?, ?)'
92
93

        sql = if Gitlab::Database.postgresql?
Michael Kozono's avatar
Michael Kozono committed
94
95
96
97
                "INSERT INTO #{table_columns_and_values} ON CONFLICT DO NOTHING;"
              else
                "INSERT IGNORE INTO #{table_columns_and_values};"
              end
98
99

        timestamp = Time.now.utc.iso8601
Michael Kozono's avatar
Michael Kozono committed
100
        sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql, file_path, timestamp, timestamp]) # rubocop:disable GitlabSecurity/PublicSend
101
102
103
        ActiveRecord::Base.connection.execute(sql)
      end

104
      def schedule_populate_untracked_uploads_jobs
105
        bulk_queue_background_migration_jobs_by_range(UntrackedFile, FOLLOW_UP_MIGRATION)
106
107
108
109
      end
    end
  end
end