Repository import during project creation often return timeout for medium and large repos.

So lets do it async. First create project, then import repo and create
satellite with Sidekiq
This commit is contained in:
Dmitriy Zaporozhets 2013-08-12 18:21:47 +03:00
parent e2f946fddb
commit 7ecebdd02d
7 changed files with 78 additions and 53 deletions

View file

@ -45,20 +45,6 @@ module Projects
@project.creator = current_user
# Import project from cloneable resource
if @project.valid? && @project.import_url.present?
shell = Gitlab::Shell.new
if shell.import_repository(@project.path_with_namespace, @project.import_url)
# We should create satellite for imported repo
@project.satellite.create unless @project.satellite.exists?
@project.imported = true
true
else
@project.errors.add(:import_url, 'cannot clone repo')
end
end
if @project.save
unless @project.group
@project.users_projects.create(project_access: UsersProject::MASTER, user: current_user)

View file

@ -37,8 +37,6 @@ class Project < ActiveRecord::Base
acts_as_taggable_on :labels, :issues_default_labels
attr_accessor :import_url
# Relations
belongs_to :creator, foreign_key: "creator_id", class_name: "User"
belongs_to :group, foreign_key: "namespace_id", conditions: "type = 'Group'"
@ -157,6 +155,10 @@ class Project < ActiveRecord::Base
import_url.present?
end
def imported?
imported
end
def check_limit
unless creator.can_create_project?
errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it")
@ -411,10 +413,6 @@ class Project < ActiveRecord::Base
!(forked_project_link.nil? || forked_project_link.forked_from_project.nil?)
end
def imported?
imported
end
def personal?
!group
end

View file

@ -1,7 +1,10 @@
class ProjectObserver < BaseObserver
def after_create(project)
return true if project.forked? || project.imported?
return true if project.forked?
if project.import?
RepositoryImportWorker.perform_in(5.seconds, project.id)
else
GitlabShellWorker.perform_async(
:add_repository,
project.path_with_namespace
@ -9,6 +12,7 @@ class ProjectObserver < BaseObserver
log_info("#{project.owner.name} created a new project \"#{project.name_with_namespace}\"")
end
end
def after_update(project)
project.send_move_instructions if project.namespace_id_changed?

View file

@ -1,6 +1,15 @@
= render 'clone_panel'
%div.git-empty
- if @project.import? && !@project.imported
.save-project-loader
%center
= image_tag "ajax_loader.gif"
%h3 Importing repository.
%p.monospace git clone --bare #{@project.import_url}
%p Please wait until we import repository for you. Refresh at will.
- else
%div.git-empty
%fieldset
%legend Git global setup:
%pre.dark

View file

@ -0,0 +1,22 @@
class RepositoryImportWorker
include Sidekiq::Worker
include Gitlab::ShellAdapter
sidekiq_options queue: :gitlab_shell
def perform(project_id)
project = Project.find(project_id)
result = gitlab_shell.send(:import_repository,
project.path_with_namespace,
project.import_url)
if result
project.imported = true
project.save
project.satellite.create unless project.satellite.exists?
project.discover_default_branch
else
project.imported = false
end
end
end

View file

@ -0,0 +1,5 @@
class AddImportUrlToProject < ActiveRecord::Migration
def change
add_column :projects, :import_url, :string
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130804151314) do
ActiveRecord::Schema.define(:version => 20130812143708) do
create_table "deploy_keys_projects", :force => true do |t|
t.integer "deploy_key_id", :null => false
@ -178,6 +178,7 @@ ActiveRecord::Schema.define(:version => 20130804151314) do
t.boolean "snippets_enabled", :default => true, :null => false
t.datetime "last_activity_at"
t.boolean "imported", :default => false, :null => false
t.string "import_url"
end
add_index "projects", ["creator_id"], :name => "index_projects_on_owner_id"