diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-08-11 21:54:02 +0300 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-08-11 22:32:16 +0300 |
commit | 6a6a69f4afbe0107a75df018b662f02b5ec0166a (patch) | |
tree | b7a49ab9b0913d90251245648a885de9442708c9 /app | |
parent | d983c5bd4671d989edf3741d0db0a54dcef9c3b6 (diff) |
Use state machine for pipeline event processing
Diffstat (limited to 'app')
-rw-r--r-- | app/models/ci/pipeline.rb | 61 | ||||
-rw-r--r-- | app/models/commit_status.rb | 8 | ||||
-rw-r--r-- | app/services/ci/create_pipeline_service.rb | 5 |
3 files changed, 50 insertions, 24 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 8de799d6088..7a0430f277a 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -19,6 +19,37 @@ module Ci after_save :keep_around_commits + state_machine :status, initial: :created do + event :skip do + transition any => :skipped + end + + event :drop do + 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') } + end + + after_transition [:created, :pending] => :running do |pipeline| + pipeline.update(started_at: Time.now) + end + + after_transition any => [:success, :failed, :canceled] do |pipeline| + pipeline.update(finished_at: Time.now) + end + + after_transition do |pipeline| + pipeline.update_duration + end + end + # ref can't be HEAD or SHA, can only be branch/tag name scope :latest_successful_for, ->(ref = default_branch) do where(ref: ref).success.order(id: :desc).limit(1) @@ -89,16 +120,12 @@ module Ci def cancel_running builds.running_or_pending.each(&:cancel) - - reload_status! end def retry_failed(user) builds.latest.failed.select(&:retryable?).each do |build| Ci::Build.retry(build, user) end - - reload_status! end def latest? @@ -185,8 +212,6 @@ module Ci def process! Ci::ProcessPipelineService.new(project, user).execute(self) - - reload_status! end def predefined_variables @@ -195,22 +220,22 @@ module Ci ] end - def reload_status! - reload - self.status = - if yaml_errors.blank? - statuses.latest.status || 'skipped' - else - 'failed' - end - self.started_at = statuses.started_at - self.finished_at = statuses.finished_at - self.duration = statuses.latest.duration - save + def can_transition_to?(expected_status) + latest_status == expected_status + end + + def update_duration + update(duration: statuses.latest.duration) end private + def latest_status + return 'failed' unless yaml_errors.blank? + + statuses.latest.status || 'skipped' + end + def keep_around_commits return unless project diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb index 3ab44461179..64ce5431d63 100644 --- a/app/models/commit_status.rb +++ b/app/models/commit_status.rb @@ -74,13 +74,13 @@ class CommitStatus < ActiveRecord::Base around_transition any => [:success, :failed, :canceled] do |commit_status, block| block.call - commit_status.pipeline.process! if commit_status.pipeline + commit_status.pipeline.try(:process!) end - around_transition any => [:pending, :running] do |commit_status, block| - block.call + # Try to update the pipeline status - commit_status.pipeline.reload_status! if commit_status.pipeline + after_transition do |commit_status, transition| + commit_status.pipeline.try(:update_status) unless transition.loopback? end end diff --git a/app/services/ci/create_pipeline_service.rb b/app/services/ci/create_pipeline_service.rb index 7398fd8e10a..cde856b0186 100644 --- a/app/services/ci/create_pipeline_service.rb +++ b/app/services/ci/create_pipeline_service.rb @@ -37,7 +37,8 @@ module Ci end if !ignore_skip_ci && skip_ci? - return error('Creation of pipeline is skipped', save: save_on_errors) + pipeline.skip if save_on_errors + return pipeline end unless pipeline.config_builds_attributes.present? @@ -93,7 +94,7 @@ module Ci def error(message, save: false) pipeline.errors.add(:base, message) - pipeline.reload_status! if save + pipeline.drop if save pipeline end end |