BigW Consortium Gitlab

repository_fork_worker.rb 1.57 KB
Newer Older
1
class RepositoryForkWorker
2 3
  ForkError = Class.new(StandardError)

4 5
  include Sidekiq::Worker
  include Gitlab::ShellAdapter
6
  include DedicatedSidekiqQueue
7
  include ProjectStartImport
8

9 10
  sidekiq_options status_expiration: StuckImportJobsWorker::IMPORT_JOBS_EXPIRATION

11
  def perform(project_id, forked_from_repository_storage_path, source_path, target_path)
12 13 14 15
    project = Project.find(project_id)

    return unless start_fork(project)

16 17 18 19
    Gitlab::Metrics.add_event(:fork_repository,
                              source_path: source_path,
                              target_path: target_path)

20 21
    result = gitlab_shell.fork_repository(forked_from_repository_storage_path, source_path,
                                          project.repository_storage_path, target_path)
22
    raise ForkError, "Unable to fork project #{project_id} for repository #{source_path} -> #{target_path}" unless result
23

24
    project.repository.after_import
25
    raise ForkError, "Project #{project_id} had an invalid repository after fork" unless project.valid_repo?
26

27
    project.import_finish
28 29 30 31 32 33 34 35 36 37 38 39
  rescue ForkError => ex
    fail_fork(project, ex.message)
    raise
  rescue => ex
    return unless project

    fail_fork(project, ex.message)
    raise ForkError, "#{ex.class} #{ex.message}"
  end

  private

40
  def start_fork(project)
41
    return true if start(project)
42 43 44 45 46

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

47 48 49
  def fail_fork(project, message)
    Rails.logger.error(message)
    project.mark_import_as_failed(message)
50 51
  end
end