Commit e1f05b93 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Use explicit events to transition between states

parent 8acbc9e0
...@@ -20,6 +20,14 @@ module Ci ...@@ -20,6 +20,14 @@ module Ci
after_save :keep_around_commits after_save :keep_around_commits
state_machine :status, initial: :created do state_machine :status, initial: :created do
event :queue do
transition :created => :pending
end
event :run do
transition [:pending, :success, :failed, :canceled, :skipped] => :running
end
event :skip do event :skip do
transition any => :skipped transition any => :skipped
end end
...@@ -28,13 +36,12 @@ module Ci ...@@ -28,13 +36,12 @@ module Ci
transition any => :failed transition any => :failed
end end
event :update_status do event :succeed do
transition any => :pending, if: ->(pipeline) { pipeline.can_transition_to?('pending') } transition any => :success
transition any => :running, if: ->(pipeline) { pipeline.can_transition_to?('running') } end
transition any => :failed, if: ->(pipeline) { pipeline.can_transition_to?('failed') }
transition any => :success, if: ->(pipeline) { pipeline.can_transition_to?('success') } event :cancel do
transition any => :canceled, if: ->(pipeline) { pipeline.can_transition_to?('canceled') } transition any => :canceled
transition any => :skipped, if: ->(pipeline) { pipeline.can_transition_to?('skipped') }
end end
after_transition [:created, :pending] => :running do |pipeline| after_transition [:created, :pending] => :running do |pipeline|
...@@ -214,23 +221,36 @@ module Ci ...@@ -214,23 +221,36 @@ module Ci
Ci::ProcessPipelineService.new(project, user).execute(self) Ci::ProcessPipelineService.new(project, user).execute(self)
end end
def build_updated
case latest_builds_status
when 'pending'
queue
when 'running'
run
when 'success'
succeed
when 'failed'
drop
when 'canceled'
cancel
when 'skipped'
skip
end
end
def predefined_variables def predefined_variables
[ [
{ key: 'CI_PIPELINE_ID', value: id.to_s, public: true } { key: 'CI_PIPELINE_ID', value: id.to_s, public: true }
] ]
end end
def can_transition_to?(expected_status)
latest_status == expected_status
end
def update_duration def update_duration
update(duration: statuses.latest.duration) update(duration: statuses.latest.duration)
end end
private private
def latest_status def latest_builds_status
return 'failed' unless yaml_errors.blank? return 'failed' unless yaml_errors.blank?
statuses.latest.status || 'skipped' statuses.latest.status || 'skipped'
......
...@@ -70,7 +70,7 @@ class CommitStatus < ActiveRecord::Base ...@@ -70,7 +70,7 @@ class CommitStatus < ActiveRecord::Base
end end
after_transition do |commit_status, transition| after_transition do |commit_status, transition|
commit_status.pipeline.try(:update_status) unless transition.loopback? commit_status.pipeline.try(:build_updated) unless transition.loopback?
end end
after_transition [:created, :pending, :running] => :success do |commit_status| after_transition [:created, :pending, :running] => :success do |commit_status|
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment