Use explicit events to transition between states
This commit is contained in:
parent
8acbc9e085
commit
e1f05b932d
2 changed files with 33 additions and 13 deletions
|
@ -20,6 +20,14 @@ module Ci
|
|||
after_save :keep_around_commits
|
||||
|
||||
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
|
||||
transition any => :skipped
|
||||
end
|
||||
|
@ -28,13 +36,12 @@ module Ci
|
|||
transition any => :failed
|
||||
end
|
||||
|
||||
event :update_status do
|
||||
transition any => :pending, if: ->(pipeline) { pipeline.can_transition_to?('pending') }
|
||||
transition any => :running, if: ->(pipeline) { pipeline.can_transition_to?('running') }
|
||||
transition any => :failed, if: ->(pipeline) { pipeline.can_transition_to?('failed') }
|
||||
transition any => :success, if: ->(pipeline) { pipeline.can_transition_to?('success') }
|
||||
transition any => :canceled, if: ->(pipeline) { pipeline.can_transition_to?('canceled') }
|
||||
transition any => :skipped, if: ->(pipeline) { pipeline.can_transition_to?('skipped') }
|
||||
event :succeed do
|
||||
transition any => :success
|
||||
end
|
||||
|
||||
event :cancel do
|
||||
transition any => :canceled
|
||||
end
|
||||
|
||||
after_transition [:created, :pending] => :running do |pipeline|
|
||||
|
@ -214,23 +221,36 @@ module Ci
|
|||
Ci::ProcessPipelineService.new(project, user).execute(self)
|
||||
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
|
||||
[
|
||||
{ key: 'CI_PIPELINE_ID', value: id.to_s, public: true }
|
||||
]
|
||||
end
|
||||
|
||||
def can_transition_to?(expected_status)
|
||||
latest_status == expected_status
|
||||
end
|
||||
|
||||
def update_duration
|
||||
update(duration: statuses.latest.duration)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def latest_status
|
||||
def latest_builds_status
|
||||
return 'failed' unless yaml_errors.blank?
|
||||
|
||||
statuses.latest.status || 'skipped'
|
||||
|
|
|
@ -70,7 +70,7 @@ class CommitStatus < ActiveRecord::Base
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
after_transition [:created, :pending, :running] => :success do |commit_status|
|
||||
|
|
Loading…
Reference in a new issue