OSDN Git Service

Add Gitlab::Satellite::Action
authorRiyad Preukschas <riyad@informatik.uni-bremen.de>
Thu, 25 Oct 2012 22:10:40 +0000 (00:10 +0200)
committerRiyad Preukschas <riyad@informatik.uni-bremen.de>
Thu, 25 Oct 2012 22:10:40 +0000 (00:10 +0200)
lib/gitlab/satellite/action.rb [new file with mode: 0644]

diff --git a/lib/gitlab/satellite/action.rb b/lib/gitlab/satellite/action.rb
new file mode 100644 (file)
index 0000000..23f8185
--- /dev/null
@@ -0,0 +1,47 @@
+module Gitlab
+  module Satellite
+    class Action
+      DEFAULT_OPTIONS = { git_timeout: 30.seconds }
+
+      attr_accessor :options, :project
+
+      def initialize(project, options = {})
+        @project = project
+        @options = DEFAULT_OPTIONS.merge(options)
+      end
+
+      protected
+
+      # * Sets a 30s timeout for Git
+      # * Locks the satellite repo
+      # * Yields the prepared satellite repo
+      def in_locked_and_timed_satellite
+        Grit::Git.with_timeout(options[:git_timeout]) do
+          File.open(lock_file, "w+") do |f|
+            f.flock(File::LOCK_EX)
+
+            unless project.satellite.exists?
+              raise "Satellite doesn't exist"
+            end
+
+            Dir.chdir(project.satellite.path) do
+              repo = Grit::Repo.new('.')
+
+              return yield repo
+            end
+          end
+        end
+      rescue Errno::ENOMEM => ex
+        Gitlab::GitLogger.error(ex.message)
+        return false
+      rescue Grit::Git::GitTimeout => ex
+        Gitlab::GitLogger.error(ex.message)
+        return false
+      end
+
+      def lock_file
+        Rails.root.join("tmp", "#{project.path}.lock")
+      end
+    end
+  end
+end