summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2012-11-08 20:23:33 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2012-11-08 20:23:33 +0200
commitaf26839ae19ff8b6d1c6874eb7b6097511ee6d98 (patch)
tree37099d23f5e91122c2fe3616ba60619d109f1607
parent42b0763f454490e024a8fd16b922025aa337b80b (diff)
downloadgitlab-ci-af26839ae19ff8b6d1c6874eb7b6097511ee6d98.tar.gz
State machine added
-rw-r--r--Gemfile3
-rw-r--r--Gemfile.lock2
-rw-r--r--app/helpers/projects_helper.rb4
-rw-r--r--app/models/build.rb42
-rw-r--r--app/models/project.rb5
-rw-r--r--lib/runner.rb11
6 files changed, 34 insertions, 33 deletions
diff --git a/Gemfile b/Gemfile
index 9ba7989..8555a78 100644
--- a/Gemfile
+++ b/Gemfile
@@ -37,6 +37,9 @@ gem 'grit'
# Pagination
gem 'will_paginate', '~> 3.0'
+# State machine
+gem 'state_machine'
+
# Other
gem 'rake'
gem 'foreman'
diff --git a/Gemfile.lock b/Gemfile.lock
index 6f46738..4937bee 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -189,6 +189,7 @@ GEM
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
stamp (0.3.0)
+ state_machine (1.1.2)
therubyracer (0.10.2)
libv8 (~> 3.3.10)
thin (1.5.0)
@@ -242,6 +243,7 @@ DEPENDENCIES
settingslogic
shoulda-matchers
stamp
+ state_machine
therubyracer
thin
uglifier (>= 1.0.3)
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index fbae2d8..56fdd09 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -1,8 +1,8 @@
module ProjectsHelper
def project_statuc_class(project)
- if project.status == 'success'
+ if project.last_build.try :success?
'alert-success'
- elsif project.status == 'fail'
+ elsif project.last_build.try :failed?
'alert-error'
else
''
diff --git a/app/models/build.rb b/app/models/build.rb
index e40d03c..24a9769 100644
--- a/app/models/build.rb
+++ b/app/models/build.rb
@@ -8,34 +8,34 @@ class Build < ActiveRecord::Base
validates :ref, presence: true
validates :status, presence: true
- def failed?
- status == 'fail'
- end
- def success?
- status == 'success'
- end
+ state_machine :status, initial: :pending do
+ event :run do
+ transition pending: :running
+ end
- def git_author_name
- project.last_commit(self.sha).author.name
- rescue
- nil
- end
+ event :drop do
+ transition running: :failed
+ end
- def running?
- status == 'running'
- end
+ event :success do
+ transition running: :success
+ end
- def success!
- update_status 'success'
- end
+ after_transition :pending => :running do |build, transition|
+ build.started_at = Time.now
+ end
- def fail!
- update_status 'fail'
+ state :pending, value: 'pending'
+ state :running, value: 'running'
+ state :failed, value: 'failed'
+ state :success, value: 'success'
end
- def running!
- update_status 'running'
+ def git_author_name
+ project.last_commit(self.sha).author.name
+ rescue
+ nil
end
def update_status status
diff --git a/app/models/project.rb b/app/models/project.rb
index b959b3c..ec9460f 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -20,7 +20,6 @@ class Project < ActiveRecord::Base
data = {
project_id: self.id,
- status: 'waiting',
ref: ref,
sha: sha
}
@@ -47,9 +46,9 @@ class Project < ActiveRecord::Base
end
def status_image
- if status == 'success'
+ if status.success?
'success.png'
- elsif status == 'fail'
+ elsif status.failed?
'failed.png'
else
'unknown.png'
diff --git a/lib/runner.rb b/lib/runner.rb
index dfea63f..09f5338 100644
--- a/lib/runner.rb
+++ b/lib/runner.rb
@@ -24,10 +24,7 @@ class Runner
path = project.path
commands = project.scripts
- build.update_attributes(
- started_at: Time.now,
- status: 'running'
- )
+ build.run!
Dir.chdir(path) do
commands.each_line do |line|
@@ -35,7 +32,7 @@ class Runner
build.write_trace(@output)
unless status
- build.fail!
+ build.drop!
return
end
end
@@ -44,10 +41,10 @@ class Runner
build.success!
rescue Errno::ENOENT
@output << "INVALID PROJECT PATH"
- build.fail!
+ build.drop!
rescue Timeout::Error
@output << "TIMEOUT"
- build.fail!
+ build.drop!
ensure
build.write_trace(@output)
end