Welcome to mirror list, hosted at ThFree Co, Russian Federation.

relation_batch_export_service.rb « bulk_imports « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c7164d7c304914e3bbe3ba843e590847d35b94aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module BulkImports
  class RelationBatchExportService
    include Gitlab::ImportExport::CommandLineUtil

    def initialize(user_id, batch_id)
      @user = User.find(user_id)
      @batch = BulkImports::ExportBatch.find(batch_id)
      @config = FileTransfer.config_for(portable)
    end

    def execute
      start_batch!

      export_service.export_batch(relation_batch_ids)
      ensure_export_file_exists!
      compress_exported_relation
      upload_compressed_file

      finish_batch!
    rescue StandardError => e
      fail_batch!(e)
    ensure
      FileUtils.remove_entry(export_path)
    end

    private

    attr_reader :user, :batch, :config

    delegate :export_path, to: :config
    delegate :batch_number, :export, to: :batch
    delegate :portable, :relation, to: :export
    delegate :exported_filename, :exported_objects_count, to: :export_service

    def export_service
      @export_service ||= config.export_service_for(relation).new(portable, export_path, relation, user)
    end

    def compress_exported_relation
      gzip(dir: export_path, filename: exported_filename)
    end

    def upload_compressed_file
      File.open(compressed_filename) { |file| batch_upload.export_file = file }

      batch_upload.save!
    end

    def batch_upload
      @batch_upload ||= ::BulkImports::ExportUpload.find_or_initialize_by(export_id: export.id, batch_id: batch.id) # rubocop: disable CodeReuse/ActiveRecord
    end

    def compressed_filename
      File.join(export_path, "#{exported_filename}.gz")
    end

    def relation_batch_ids
      Gitlab::Cache::Import::Caching.values_from_set(cache_key).map(&:to_i)
    end

    def cache_key
      BulkImports::BatchedRelationExportService.cache_key(export.id, batch.id)
    end

    def start_batch!
      batch.update!(status_event: 'start', objects_count: 0, error: nil)
    end

    def finish_batch!
      batch.update!(status_event: 'finish', objects_count: exported_objects_count, error: nil)
    end

    def fail_batch!(exception)
      Gitlab::ErrorTracking.track_exception(exception, portable_id: portable.id, portable_type: portable.class.name)

      batch.update!(status_event: 'fail_op', error: exception.message.truncate(255))
    end

    def exported_filepath
      File.join(export_path, exported_filename)
    end

    # Create empty file on disk
    # if relation is empty and nothing was exported
    def ensure_export_file_exists!
      FileUtils.touch(exported_filepath)
    end
  end
end