Commit 847bba926929c4d64dcc5065b9b2762a62a87c87
1 parent
643ed9cb
Exists in
master
and in
4 other branches
Add Gitlab::Satellite::Action
Showing
1 changed file
with
47 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,47 @@ | @@ -0,0 +1,47 @@ | ||
| 1 | +module Gitlab | ||
| 2 | + module Satellite | ||
| 3 | + class Action | ||
| 4 | + DEFAULT_OPTIONS = { git_timeout: 30.seconds } | ||
| 5 | + | ||
| 6 | + attr_accessor :options, :project | ||
| 7 | + | ||
| 8 | + def initialize(project, options = {}) | ||
| 9 | + @project = project | ||
| 10 | + @options = DEFAULT_OPTIONS.merge(options) | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + protected | ||
| 14 | + | ||
| 15 | + # * Sets a 30s timeout for Git | ||
| 16 | + # * Locks the satellite repo | ||
| 17 | + # * Yields the prepared satellite repo | ||
| 18 | + def in_locked_and_timed_satellite | ||
| 19 | + Grit::Git.with_timeout(options[:git_timeout]) do | ||
| 20 | + File.open(lock_file, "w+") do |f| | ||
| 21 | + f.flock(File::LOCK_EX) | ||
| 22 | + | ||
| 23 | + unless project.satellite.exists? | ||
| 24 | + raise "Satellite doesn't exist" | ||
| 25 | + end | ||
| 26 | + | ||
| 27 | + Dir.chdir(project.satellite.path) do | ||
| 28 | + repo = Grit::Repo.new('.') | ||
| 29 | + | ||
| 30 | + return yield repo | ||
| 31 | + end | ||
| 32 | + end | ||
| 33 | + end | ||
| 34 | + rescue Errno::ENOMEM => ex | ||
| 35 | + Gitlab::GitLogger.error(ex.message) | ||
| 36 | + return false | ||
| 37 | + rescue Grit::Git::GitTimeout => ex | ||
| 38 | + Gitlab::GitLogger.error(ex.message) | ||
| 39 | + return false | ||
| 40 | + end | ||
| 41 | + | ||
| 42 | + def lock_file | ||
| 43 | + Rails.root.join("tmp", "#{project.path}.lock") | ||
| 44 | + end | ||
| 45 | + end | ||
| 46 | + end | ||
| 47 | +end |