2015-09-01 07:56:40 +00:00
|
|
|
class RepositoryForkWorker
|
2017-06-01 14:27:35 +00:00
|
|
|
ForkError = Class.new(StandardError)
|
|
|
|
|
2015-09-01 07:56:40 +00:00
|
|
|
include Sidekiq::Worker
|
|
|
|
include Gitlab::ShellAdapter
|
2016-10-21 16:13:41 +00:00
|
|
|
include DedicatedSidekiqQueue
|
2017-10-10 09:09:49 +00:00
|
|
|
include ProjectStartImport
|
2015-09-01 07:56:40 +00:00
|
|
|
|
2017-08-17 09:00:31 +00:00
|
|
|
sidekiq_options status_expiration: StuckImportJobsWorker::IMPORT_JOBS_EXPIRATION
|
|
|
|
|
2016-07-26 21:22:13 +00:00
|
|
|
def perform(project_id, forked_from_repository_storage_path, source_path, target_path)
|
2017-08-17 09:00:31 +00:00
|
|
|
project = Project.find(project_id)
|
|
|
|
|
|
|
|
return unless start_fork(project)
|
|
|
|
|
2016-08-16 14:18:48 +00:00
|
|
|
Gitlab::Metrics.add_event(:fork_repository,
|
|
|
|
source_path: source_path,
|
|
|
|
target_path: target_path)
|
|
|
|
|
2016-07-26 21:22:13 +00:00
|
|
|
result = gitlab_shell.fork_repository(forked_from_repository_storage_path, source_path,
|
|
|
|
project.repository_storage_path, target_path)
|
2017-06-01 14:27:35 +00:00
|
|
|
raise ForkError, "Unable to fork project #{project_id} for repository #{source_path} -> #{target_path}" unless result
|
2015-09-01 07:56:40 +00:00
|
|
|
|
2016-03-18 14:31:19 +00:00
|
|
|
project.repository.after_import
|
2017-06-01 14:27:35 +00:00
|
|
|
raise ForkError, "Project #{project_id} had an invalid repository after fork" unless project.valid_repo?
|
2015-09-01 07:56:40 +00:00
|
|
|
|
2015-11-11 15:22:51 +00:00
|
|
|
project.import_finish
|
2017-06-01 14:27:35 +00:00
|
|
|
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
|
|
|
|
|
2017-08-17 09:00:31 +00:00
|
|
|
def start_fork(project)
|
2017-10-10 09:09:49 +00:00
|
|
|
return true if start(project)
|
2017-08-17 09:00:31 +00:00
|
|
|
|
|
|
|
Rails.logger.info("Project #{project.full_path} was in inconsistent state (#{project.import_status}) while forking.")
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-06-01 14:27:35 +00:00
|
|
|
def fail_fork(project, message)
|
|
|
|
Rails.logger.error(message)
|
|
|
|
project.mark_import_as_failed(message)
|
2015-09-01 07:56:40 +00:00
|
|
|
end
|
|
|
|
end
|