summaryrefslogtreecommitdiff
path: root/app/workers/project_export_worker.rb
blob: e3f8c4bcd9d54025c70c67660363243e54df319c (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
# frozen_string_literal: true

class ProjectExportWorker # rubocop:disable Scalability/IdempotentWorker
  include ApplicationWorker

  data_consistency :always
  include ExceptionBacktrace

  feature_category :importers
  worker_resource_boundary :memory
  urgency :throttled
  loggable_arguments 2, 3
  sidekiq_options retry: false, dead: false
  sidekiq_options status_expiration: StuckExportJobsWorker::EXPORT_JOBS_EXPIRATION

  def perform(current_user_id, project_id, after_export_strategy = {}, params = {})
    current_user = User.find(current_user_id)
    project = Project.find(project_id)
    export_job = project.export_jobs.safe_find_or_create_by(jid: self.jid)
    after_export = build!(after_export_strategy)

    export_job&.start

    ::Projects::ImportExport::ExportService.new(project, current_user, params).execute(after_export)

    export_job&.finish
  rescue ActiveRecord::RecordNotFound => e
    log_failure(project_id, e)
  rescue Gitlab::ImportExport::AfterExportStrategyBuilder::StrategyNotFoundError => e
    log_failure(project_id, e)
    export_job&.finish
  rescue StandardError => e
    log_failure(project_id, e)
    export_job&.fail_op
    raise
  end

  private

  def build!(after_export_strategy)
    strategy_klass = after_export_strategy&.delete('klass')

    Gitlab::ImportExport::AfterExportStrategyBuilder.build!(strategy_klass, after_export_strategy)
  end

  def log_failure(project_id, ex)
    logger.error("Failed to export project #{project_id}: #{ex.message}")
  end
end