Commit 0ebcc60a33f5aba9e7740f43a108b96461dd3fbf

Authored by Riyad Preukschas
1 parent 0e9d4f30

Move locking from Satellite::Action to Satellite and add checks

lib/gitlab/satellite/action.rb
@@ -18,18 +18,8 @@ module Gitlab @@ -18,18 +18,8 @@ module Gitlab
18 # * Yields the prepared satellite repo 18 # * Yields the prepared satellite repo
19 def in_locked_and_timed_satellite 19 def in_locked_and_timed_satellite
20 Grit::Git.with_timeout(options[:git_timeout]) do 20 Grit::Git.with_timeout(options[:git_timeout]) do
21 - File.open(lock_file, "w+") do |f|  
22 - f.flock(File::LOCK_EX)  
23 -  
24 - unless project.satellite.exists?  
25 - raise "Satellite doesn't exist"  
26 - end  
27 -  
28 - Dir.chdir(project.satellite.path) do  
29 - repo = Grit::Repo.new('.')  
30 -  
31 - return yield repo  
32 - end 21 + project.satellite.lock do
  22 + return yield project.satellite.repo
33 end 23 end
34 end 24 end
35 rescue Errno::ENOMEM => ex 25 rescue Errno::ENOMEM => ex
@@ -40,10 +30,6 @@ module Gitlab @@ -40,10 +30,6 @@ module Gitlab
40 return false 30 return false
41 end 31 end
42 32
43 - def lock_file  
44 - Rails.root.join("tmp", "#{project.path}.lock")  
45 - end  
46 -  
47 # * Clears the satellite 33 # * Clears the satellite
48 # * Updates the satellite from Gitolite 34 # * Updates the satellite from Gitolite
49 # * Sets up Git variables for the user 35 # * Sets up Git variables for the user
lib/gitlab/satellite/satellite.rb
@@ -10,6 +10,8 @@ module Gitlab @@ -10,6 +10,8 @@ module Gitlab
10 end 10 end
11 11
12 def clear_and_update! 12 def clear_and_update!
  13 + raise "Satellite doesn't exist" unless exists?
  14 +
13 delete_heads! 15 delete_heads!
14 clear_working_dir! 16 clear_working_dir!
15 update_from_source! 17 update_from_source!
@@ -23,10 +25,31 @@ module Gitlab @@ -23,10 +25,31 @@ module Gitlab
23 File.exists? path 25 File.exists? path
24 end 26 end
25 27
  28 + # Locks the satellite and yields
  29 + def lock
  30 + raise "Satellite doesn't exist" unless exists?
  31 +
  32 + File.open(lock_file, "w+") do |f|
  33 + f.flock(File::LOCK_EX)
  34 +
  35 + return yield
  36 + end
  37 + end
  38 +
  39 + def lock_file
  40 + Rails.root.join("tmp", "#{project.path}.lock")
  41 + end
  42 +
26 def path 43 def path
27 Rails.root.join("tmp", "repo_satellites", project.path) 44 Rails.root.join("tmp", "repo_satellites", project.path)
28 end 45 end
29 46
  47 + def repo
  48 + raise "Satellite doesn't exist" unless exists?
  49 +
  50 + @repo ||= Grit::Repo.new(path)
  51 + end
  52 +
30 private 53 private
31 54
32 # Clear the working directory 55 # Clear the working directory
@@ -39,7 +62,7 @@ module Gitlab @@ -39,7 +62,7 @@ module Gitlab
39 # This ensures we have no name clashes or issues updating branches when 62 # This ensures we have no name clashes or issues updating branches when
40 # working with the satellite. 63 # working with the satellite.
41 def delete_heads! 64 def delete_heads!
42 - heads = repo.heads.map{|head| head.name} 65 + heads = repo.heads.map(&:name)
43 66
44 # update or create the parking branch 67 # update or create the parking branch
45 if heads.include? PARKING_BRANCH 68 if heads.include? PARKING_BRANCH
@@ -54,15 +77,11 @@ module Gitlab @@ -54,15 +77,11 @@ module Gitlab
54 heads.each { |head| repo.git.branch({D: true}, head) } 77 heads.each { |head| repo.git.branch({D: true}, head) }
55 end 78 end
56 79
57 - def repo  
58 - @repo ||= Grit::Repo.new(path)  
59 - end  
60 -  
61 # Updates the satellite from Gitolite 80 # Updates the satellite from Gitolite
62 # 81 #
63 # Note: this will only update remote branches (i.e. origin/*) 82 # Note: this will only update remote branches (i.e. origin/*)
64 def update_from_source! 83 def update_from_source!
65 - repo.git.fetch({}, :origin) 84 + repo.git.fetch({timeout: true}, :origin)
66 end 85 end
67 end 86 end
68 end 87 end