BigW Consortium Gitlab

repository_import_worker.rb 1.22 KB
Newer Older
1
class RepositoryImportWorker
2 3
  ImportError = Class.new(StandardError)

4
  include Sidekiq::Worker
5
  include DedicatedSidekiqQueue
6
  include ExceptionBacktrace
7

8
  sidekiq_options status_expiration: StuckImportJobsWorker::IMPORT_JOBS_EXPIRATION
9

10
  def perform(project_id)
11
    project = Project.find(project_id)
12

13
    return unless start_import(project)
14

15
    Gitlab::Metrics.add_event(:import_repository,
16 17
                              import_url: project.import_url,
                              path: project.full_path)
18

19
    result = Projects::ImportService.new(project, project.creator).execute
20
    raise ImportError, result[:message] if result[:status] == :error
21

22
    project.repository.after_import
23
    project.import_finish
24 25 26 27 28 29 30 31 32 33 34 35
  rescue ImportError => ex
    fail_import(project, ex.message)
    raise
  rescue => ex
    return unless project

    fail_import(project, ex.message)
    raise ImportError, "#{ex.class} #{ex.message}"
  end

  private

36 37 38 39 40 41 42
  def start_import(project)
    return true if project.import_start

    Rails.logger.info("Project #{project.full_path} was in inconsistent state (#{project.import_status}) while importing.")
    false
  end

43 44
  def fail_import(project, message)
    project.mark_import_as_failed(message)
45
  end
46
end